I'm coming to you because I'm trying to do a foreach loop on Discord.JS to detect changes in a JSON file. My file does change content, but my foreach loop keeps the old content in memory. I have no idea how to solve the problem...
My index.js:
const Discord = require('discord.js');
const low = require('lowdb')
const FileSync = require('lowdb/adapters/FileSync')
const fetch = require('node-fetch');
const client = new Discord.Client();
const config = require('./config.json');
const database = require('./db.json');
const adapter = new FileSync('./db.json')
const db = low(adapter)
const prefix = config.prefix;
let api = config.api;
client.once('ready', () => {
db.defaults({numbers: []})
.write()
setInterval(function() {
database.numbers.forEach(async element => {
let state = await fetch(`some-api-url`).then(response => response.json());
if(state[0].response != element.response){
db.get('numbers')
.find({number: element.number})
.assign({response: state[0].response, sms: state[0].msg})
.write();
let user = element.clientId;
try {
await client.users.cache.get(user).send(`Your message for number ${element.number} is ${element.sms}`);
} catch(error){
console.log(error)
}
}
});
}, 3000);
console.log('Ready!');
});
It all works, it just keeps the old file in memory.
To solve this problem, I passed my const database = require('./db.json'); into let. Then I integrated fs so that I could clear the cache:
setInterval(function() {
delete require.cache[require.resolve('./db.json')]
database = require('./db.json');
Problem solved!
Related
DiscordAPIError[50035]: Invalid Form Body
0[CONTENT_TYPE_INVALID]: Expected "Content-Type" header to be one of {'application/json'}.
Im getting this while starting my bot.
const fs = require("fs");
const colors = require('colors');
const { REST } = require("#discordjs/rest");
const { Routes } = require("discord-api-types/v10");
module.exports = (client) => {
client.handleCommands = async () => {
const commandFolders = fs.readdirSync("./src/commands");
for (const folder of commandFolders) {
const commandFiles = fs
.readdirSync(`./src/commands/${folder}`)
.filter((file) => file.endsWith(".js"));
const { commands, commandArray } = client;
for (const file of commandFiles) {
const command = require(`../../commands/${folder}/${file}`);
commands.set(command.data.name, command);
commandArray.push(command.data.toJSON());
}
}
const clientId = "0";
const guildId = "0";
const rest = new REST({ version: "10" }).setToken(process.env.TOKEN);
try {
console.log("Started refreshing application (/) commands.".yellow);
await rest.put(Routes.applicationGuildCommands(clientId, guildId), {
body: client.commandArray,
});
console.log("Successfully reloaded application (/) commands.".green);
} catch (error) {
console.error(`${error}`.red);
}
};
};
This is the code of handleCommands.js, and error above is getting by this script, anyway to fix?
(client id and guild id is blurred.)
Im beginner in the js so i dont really know much abt it.
I were trying to do slash command loader, but actually it gave me some kind of errors.
Im using discord js to make a multi-purpose discord bot for my server, but its giving me this error:
ValidationError: Expected a string primitive
It was working fine yesterday but i forgot to save something and i dont know what.
const fs = require('node:fs');
const path = require('node:path');
const {
Client,
GatewayIntentBits,
Partials,
Collection,
} = require("discord.js");
const { Player } = require('discord-player');
const { Routes } = require('discord-api-types/v10');
const { token, prefix, guildId, clientId } = require('./config.json');
const { REST } = require('#discordjs/rest');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildPresences,
GatewayIntentBits.GuildVoiceStates
],
partials: [
Partials.Channel,
Partials.Message,
Partials.User,
Partials.GuildMember,
],
});
client.player = new Player(client, {
ytdlOptions: {
quality: "highestaudio",
highWaterMark: 1 << 25
}
});
module.exports = client;
// command handler
//slash commands
const slashCommands = [];
client.slashCommands = new Collection();
const commandsPath = path.join(__dirname, "commands"); // E:\yt\discord bot\js\intro\commands
const slash_commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('S.js'));
for(const file of slash_commandFiles)
{
const filePath = path.join(commandsPath, file);
const command = require(filePath);
client.slashCommands.set(command.data.name, command);
slashCommands.push(command.toJSON());
}
console.log(slashCommands);
//message commands
client.messageCommands = new Collection();
const message_commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('M.js'));
for (const file of message_commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
// Set a new item in the Collection
// With the key as the command name and the value as the exported module
client.messageCommands.set(command.Name, command);
}
//event handler
const eventsPath = path.join(__dirname, 'events');
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'));
for (const file of eventFiles) {
const filePath = path.join(eventsPath, file);
const event = require(filePath);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args));
}
}
// messageCommand handler
client.on('messageCreate', (message) => {
const args = message.content.slice(prefix.length).split(' ');
const command = args[0];
if (client.messageCommands.get(command)) {
let Command = client.messageCommands.get(command);
Command.execute(message);
}
});
client.on('ready', () => {
const rest = new REST({ version: '9' }).setToken(token);
rest.put(Routes.applicationGuildCommands(clientId, guildId),
{ body: slashCommands })
.then(() => console.log('Successfully updated commands for guild ' + guildId))
.catch(console.error);
console.log('bot is online!');
client.user.setStatus('idle');
});
client.login(token);
im sure there's nothing wrong with any of the command files because it was working fine yesterday.
there's 100% an error in this line slashCommands.push(command.toJSON()); but I cant seem to fix it. The command.toJSON() console logs just fine but gives an error while trying to push into the list
Ran into the same issue, turns out options (i.e. addUserOption) require a description. Point is it's really confusing as this error shows up when doing command.data.toJSON(). If you are dynamically loading the command files as described in the guide and running into this issue, then try manually doing a require to trigger the validation beforehand.
Try using command.data.toJSON() as command is a nested object with a data and an execute key.
I've fixed it, there was a url in the json which seemed to be causing some issue, I removed the file with the url and its working now
Hey i made a xp system with discord-xp i wanted to make a slashcommand that can give a user xp. But everytime i give this user xp in the database there is a # behind the user id and i want it without because otherwise the bot will not work.
Here is my Code:
const { SlashCommandBuilder } = require("#discordjs/builders");
const Levels = require('discord-xp');
const { MessageEmbed } = require('discord.js');
const client = require("../index")
module.exports = {
data: new SlashCommandBuilder()
.setName("addxp")
.setDescription("add xp")
.addUserOption((option) => option.setName('user').setDescription('add user xp').setRequired(true))
.addNumberOption(option => option.setName('num').setDescription('Enter a number').setRequired(true)),
async execute(client, interaction) {
const user = interaction.options.user.id('target')
const number = interaction.options.getNumber('num');
var userID = interaction.user.id;
const levels = await Levels.fetch(userID, interaction.guildId);
Levels.appendXp(user, interaction.guild.id, number);
interaction.reply(`**${user.tag}** got added ${number} XP.`);
}
}
You can use this:
const user = interaction.options.getUser('user')
const id = user?.id
I everyone, I'm trying to call a function called 'safeMint' on an ERC721 contract deployed on Rinkeby testnet but I'm getting this error:
Error: resolver or addr is not configured for ENS name (argument="name", value="", code=INVALID_ARGUMENT, version=contracts/5.5.0)
This is the code I'm using to call the function
const mintNFT = async () => {
const {ethereum} = window;
if(isMetaMaskInstalled) {
try {
const abi = require('../contracts/Animals.json').abi;
console.log(abi);
const accounts = await ethereum.request({ method: 'eth_accounts' });
setAccount(accounts[0]);
const web3Provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = web3Provider.getSigner();
const contractWrite = new ethers.Contract('0x53Ea14980c8326E93a9F72889171c1e03d4aD6Ce', abi, signer);
let trx = await contractWrite.safeMint(account, props.cidOfJsonInIpfs);
console.log(trx);
} catch(err) {
console.log(err);
}
}
}
I've tried to print the parameters passed but they seem to be right, what am I doing wrong?
I solved it with the following code
const mintNFT = async () => {
const {ethereum} = window;
if(isMetaMaskInstalled) {
try {
const abi = require('../contracts/Animals.json').abi;
console.log(abi);
const accounts = await ethereum.request({ method: 'eth_accounts' });
const web3Provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = web3Provider.getSigner(accounts[0]);
console.log(signer._address)
const contractWrite = new ethers.Contract('0x53Ea14980c8326E93a9F72889171c1e03d4aD6Ce', abi, signer);
let trx = await contractWrite.safeMint(accounts[0], `https://gateway.pinata.cloud/ipfs/${props.cidOfFile}`);
let receipt = await trx.wait();
console.log(receipt);
} catch(err) {
console.log(err);
}
}
What I was missing: I was using setState function to set the 'account' state variable with the first account of metamask, instead, I started using account[0] directly and it worked!
I will accept this as solution in 2 days
I need to make a get calls request to an external server in a loop but the problem is that calling all in the loop can cause rate limits so I just wanna make the calls inside the loop every 5 min or 10 or so , is there a way that can help achieve this.
here is my code :
const util = require('util');
const StravaClientService = require("../../strava/client.service");
const StravaActivityService = require("../../strava/activity.service");
const _ = require("underscore");
const fs = require("fs");
const AWS = require("aws-sdk");
const ids = require("underscore");
const data = require("underscore");
AWS.config.update({
region: "us-east-1",
});
var docClient = new AWS.DynamoDB.DocumentClient();
module.exports = (router) => {
router.get("/streams/:id", async (req, res, done) => {
const userc = req.user;
const access_token = userc.access_token;
const ids = [4401422821,
4401416494,
4401413107,
]
const stravaClient = StravaClientService.getClient(access_token);
const activityService = StravaActivityService(stravaClient);
//
var params = {
TableName:"run-id",
Key: {
"id": userc.stravaId,
}
};
docClient.get(params, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data.Item.json);
ids.map(( id ) => setTimeout(activityService.streamActivity,5000,id))//data.Item.json
}
});
Basically, setInterval is enough in your scenario. However, you could try to use node-schedule which helps to manage your cron jobs.
Use the index to offset?
ids.map(( id, index ) => setInterval(()=>activityService.streamActivity(id),(5+index) * 60000))//data.Item.json
Use setInterval
setInterval(() => {
ids.map(( id ) => setTimeout(activityService.streamActivity,5000,id))
}, 5000); \\ for 5 minutes