Messages leaderboard issue discord.js - javascript

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

Related

is there a way to save reaction role data?

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.

Discordjs how to make a join message for any server?

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!

Get avatar of an user in discord.js that doesn't share the same server

I have a small problem with my bot. For developing my bot, I made a new dev-bot which shares the same code as my normal bot, but has it's own token.
However, I ran into a small issue while developing.
I use this code, to get someones' avatar:
client.users.get(event.user.uid).avatarURL
This works fine on my normal but, however on my Dev-Bot I get this error message:
Error getting documents TypeError: Cannot read property 'AvatarURL' of undefined
I think it's due to the fact, that my Bot can't access the avatar of the user, because it doesn't share the same server/guild as this user.
Is there any workaround I could use?
Due to Discord.js and their way of caching, not all users are going to be cached in the bot. While there is a small possibility of it actually not knowing anything about the user, there is a large chance that the Discord API will still allow you to get information form it.
To fix this issue with caching in the latest master, we have to use Client.users, which returns a UserStore. Within the UserStore, we can use a method called fetch to get information about a user.
To fix this issue in the latest stable, we have to use a method called Client.fetchUser, which does the same thing but returns a User instead of a UserStore.
Please note this is only available using a bot account. Here's an example of its usage:
// Using Discord.js Stable
bot.fetchUser(theUsersID).then(myUser => {
console.log(myUser.avatarURL); // My user's avatar is here!
});
// Using Discord.js Master
bot.users.fetch(theUsersID).then(myUser => {
console.log(myUser.avatarURL()); // My user's avatar is here!
});
If there is an error fetching the user (say, a DiscordAPI Permissions Error), this means that there is no way for your bot to get the user's avatar without knowing who the user is first (or sharing a Guild with the user). Happy coding!

How do I retrieve user information from discord.js events?

Right now I'm working on a Discord bot ignore command will ignore a channel's command inputs when this action is true. Right now my current js file is here.
What I need the bot to do is:
Detect when the message "GCMignore" is posted
Find if their role is "Moderator".
Right now I'm not able to get retrieve the user who sent the message to detect if they are a moderator. I've tried guildmember.roles and I understand how a map works, it's just that the code is not able to relate back to the person who sent the message.
See this link.
You can use User.hasRole() function to see what roles they have. Also to be able to create roles please see this link

How to store old messages

So I am new to discord API and I am currently trying to create a small simple discord bot.
The thing that I am trying to do is archive the messages that have been sent in a specific channel. Not only those deleted, or edited but all of them.
Any ideas?
You can try .fetchMessages([options]) .
Default messages get is 50.
Options object you can put as where you want to start collecting the messages from. Place the message inside options and it will start downloading starting from the message.

Categories

Resources