So atm I'm working on a Discord.js bot and have a command to ban people. The issue is if I turn the bot off and on again all the setTimeouts reset. So say, if I were to ban someone for a week, turn the bot off and on in between there, they will never automatically be unbanned.
setTimeout(function () {
memberTarget.roles.remove(bannedRole);
}, ms(args[1]));
What other way could I use for it to remember to unban everyone after the bot has been turned on again?
You need to use database or data persist
Store bans data to database/data persist then set worker to check for unban (expiry).
Database https://www.tutorialsteacher.com/nodejs/data-access-in-nodejs
Data Persist https://www.npmjs.com/package/node-persist
Related
How are you?
I'm coding a Discord bot and I'm making a message leaderboard command, which displays up to 15 members with most messages (quick.db package to store data). Basically, It's alright when I run the code, but when someone who has so much messages and should appear in the leaderboard leave the server, it returns an error when I try to get the members tag and undefined in the message.
Is it possible to hide the user or delete them from the database?
What have I tried to solve the problem?
I tried to read Discord docs. and quick.db package to see if I can delete the user when they leave (using guildMemberRemove event), but what if they get back to the server?
οΎ
A better way is to delete user(lefted)* data from database once user left the server in guildMemberRemove event
I have simple code, that checks if a user has an specific role:
message.member.roles.cache.some(role => role.id === 'role_id')
The problem is now, that when I change the roles, so remove the specific role while the bot is running, the user still has the rights. So in the Bot Cache, the user still has the specific role.
Is there any way to renew the bot cache when user roles change?
Thanks in advance
GuildMemberManager.fetch updates the cache. Simply do this above that statement:
await message.guild.members.fetch();
//rest of code with cached members
I have a working slash command, which is great. I can use the command with one input parameter eg /command string and it will execute the command.
However I am looking to set up a Discord bot that uses that command in a channel every 5 or so minutes. I can't seem to get the bot to use the command, any ideas on how to get it to work?
It just displays the string in the channel but the bot doesn't execute the command.
bot.on('messageCreate', async (msg) => {
if (msg.content === "!loop") {
interval = setInterval (function () {
msg.channel.send("/command string")
}, 3 * 1000);
}
})
Bots can only receive Application Command Interactions, not create them. This means your bot won't be able to run other bot's slash commands, click their buttons or use their dropdown menus.
If you have control over the other bot's code, though, you can set it up to listen for messages (not interactions) from your second bot, and run code accordingly.
But be aware: as MegaMix mentioned in their comment, if the bot you want to control isn't yours, you probably won't be able to do that, as it is a best practice to ignore messages from other bots to prevent abuse and infinite loops.
I've currently got reaction roles set up in my code and they work, however I have to rerun the command every time I restart the bot, which isn't optimal.
Is it possible at all to cache the data so that there is only one reaction role message and the roles are given out regardless of how many times the bot has rebooted?
Any help with this is greatly appreciated!
in order to do this you need to have a database setup and store the message's id.
Then when your bot starts you can fetch that message inside messageReactionAdd/messageReactionRemove client event(s) and track for reactions on the message id you have previously stored.
I have a Discord bot that has a punish command, and I want to make the bot remove the punishment after 24 hours/1 day. Don't want to use setTimeout() because the bot can go down between that period and setTimeout() consumes resources. How to do that? Thank you.
upon punishment, a variable can be assigned as Date.now()+86400000. When they log in and the current Date is greater than this value, the ban will end.
You can do this without timers. When you ban the user, just mark their user object (in whatever persistent store you use for user accounts) with a date/time that the ban ends. Then, log them out.
Then, on every login request, check the user object to see if they have a ban time and if the current time has passed the ban time. If not, fail their login (don't let them in). If so, remove the ban time from the user object as the ban is now over and they can now login in.