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.
Related
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
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 am trying to get all the members with a certain role. For example, there is role of gurdian in my discord server with a id of 872029521484873779. I want a list of all the users Name in a array who have gurdian as a role in my server. My code is as below
let nameList= msg.guild.roles.cache.get('role_id').members.map(m=>m.user.tag);
However, In result it only returns one user in the nameList whereas as there are 9 users with the role assigned to them. What am I doing wrong here which is bringing me only 1 user not the rest of 9 users in a list in array. I am new to discord.js
This is happening because the members aren't cached. So you only see 1 person logging because only 1 person is in the cache. To fix this, you can fetch all members by doing msg.guild.members.fetch(), then use msg.guild.roles.cache.get("roleid").members.map(m => m.user.tag) to get the real output. Please do note that you will need the GUILD_MEMBERS intent if you are on v13.
Fetching members from the guild will store them in cache. So when you do do role.members, you get the members who have this role.
If you don't fetch members, then the info won't be accurate. Discord.js stores users in cache. This happens whenever someone sends a message, updates their profile or anything about themselves WHILE the bot is online. So if you restart your bot, no one will be in the cache.
Why is fetch different from cache?
Fetch gets the info directly from discord, while cache is checking if the user is stored locally. When you fetch members, they are automatically stored in the cache as well.
Your mistake might be a simple confusion with the id numbers. The number in your code is different then the number in your question
I know you can do it like this
bot.on('guildMemberAdd', member => {
member.guild.channels.get('channelID').send("Welcome"); });
But then you have to specify a channelID.
Welcome Channel
All members join through this link onto the welcome channel. Is there a way that discordjs sends a join message where ever the user got invited? Example: A join link for general so the welcome message is in general.
Thanks for the help :)
The guildMemberAdd event only returns a GuildMember object which doesn't seem to include a reference to the invite used. You can fetch all the generated invite links and guess which one was used, but I wouldn't recommend this option.
Here are three solutions I can think of:
Use the System Message Channel
This channel can be defined in the server settings without enabling the "Random welcome message". You can find it in Guild.systemChannelID.
https://discord.js.org/#/docs/main/stable/class/Guild?scrollTo=systemChannelID
With public bots, make this setting configurable by an admin
You can make your bot respond to a command that will allow an admin to select a channel, either by selecting the channel where the command was sent, or prompting a list of channels. This is not the most straightforward solution to implement, but this is the best in terms of UX for your users.
With private bots, hardcode the channel ID
This one will be the easiest to implement if your using your bot privately in one server or a limited number of servers.
Go into Discord settings
Appearance
Toggle "Developer mode" in the Avanced section
Then right click on the desired channel, and Copy ID
there is a event in the Client() class that called guildMemberAdd. It will return a GuildMember class that will store all informations about the newly joined member!
If you bot is private bot
then you can simple hard code your bot, by enable the developer mode and right-click on the specific channel that you want your bot send welcome messages to, chose get ID and then paste the copied ID to member.guild.channels.cache.get("THE_ID") and then simple send the message.
client.on('guildMemberAdd', member => {
const channel = member.guild.channels.cache.get("CHANNEL_ID"); // Getting the channel
if(channel){ // Checking if the channel exist
channel.send(`Welcome ${member} to ${member.guild.name}`); //Send the message
}
Else if your bot is public bot
Then you will need a setup command to helps others server owners setup their welcome message function with your bot.
You should need a database (google it, if you don't know) to store channel's IDs, then get the channel's ID and do the same thing that I mentioned before!
Trying to find a way to add into my bot, if someone types for example +info
it will list all the channels on the server with a certain role.
So if the role is VIP, doing +info would list every channel on the server which has the role VIP.
Have been trying to read the doc's but can't find anything that shows what I want. I'm guessing it would have to use a different module.
Can any one help with this?
Cheers
You could loop through all the channels in the guild and check whether the User has the VIEW_CHANNEL permission, to check if they have access to the channel. Perhaps something like this:
const listedChannels = [];
message.guild.channels.forEach(channel => {
if(channel.permissionsFor(message.author).has('VIEW_CHANNEL')) listedChannels.push(channel.name);
});
message.channel.send(`You have access to: ${listedChannels.join(',')}`);
What this does, is it iterates through the collection of channels in the guild and if the author of the message has permissions to actually view the channel, then it will add that channel name to the array. Then, it sends a message saying a list of channels the user has access to.