How to fix DiscordAPIError: Unknown Member? - javascript

I have had a discord bot running for about 3 months, and today I started getting this DiscordAPIError: Unknown Member error message. It is coming from an interaction:
client.on("interactionCreate", async interaction => {
interaction.member.roles.add(<roleId>);
});
I am unable to consistently reproduce the error, but it seems like it may be coming from new members joining the server, and perhaps the discord API is not recognizing them as a guild member just yet. The server had a large influx of users today, and that's when the issue started.
Is there any way I can work around this issue, or perhaps force update the guild's member list?

The member does not seem to be properly cached. Before accessing it, use await interaction.member.fetch()
client.on("interactionCreate", async interaction => {
await interaction.member.fetch()
interaction.member.roles.add(roleId)
})

Related

DiscordAPIError: Cannot send messages to this user at RequestHandler.execute

Hey so i made a 'hack' command in my bot as a joke, i want it to dm the author of the message, but whenever i run the command, it just gives me a error, even if the author has open dm's. this is the error:
DiscordAPIError: Cannot send messages to this user
And this is my code:
msg.author.send(`Name:${taggedUser.username},
ID:${taggedUser.id}
IP Address:${response}`).catch(console.error)
})
})
})
It is not possible for this particular error to originate apart from the member being either unavailable to the client or has direct messages closed you can simply tweak your code a little so if the dms are closed it would post to the channel instead like so:
msg.author.send(`Name:${taggedUser.username},
ID:${taggedUser.id}
IP Address:${response}`).catch(() => {
msg.channel.send(`Unable to Direct Message user instead sending this here!\n\n Name:${taggedUser.username},
ID:${taggedUser.id}
IP Address:${response}`)
});
Sources:
Discord.js' Role:
discordjs/discord.js/src/rest/DiscordAPIError.js
This just extends the error thrown by discord API in case of explicit faliure so has no hand in the error.
Discord's Role:
Documentation reference of error messages
This throws the specific error and you know, Discord itself can't be wrong or else stackoverflow would be flooded with similar questions and bots would break haha!

Discord.js Get common servers between two users

I am looking to get the common servers between two discord Users. Currently, my bot is able to access the guilds that it is a part of, however given a user who has sent it a message, it is unable to access any of the guilds of the user. I understand that discord limits you to seeing shared guilds/servers, but I can't find any way to even access those.
Any help would be much appreciated.
Context: DM
guild = message.client.guilds.cache.find(clientGuild=>message.author.????)
I want something like:
guild = message.client.guilds.cache.find(clientGuild=>message.author.guilds.includes(clientGuild)
This will not work with sharding. I don't have experience with sharding yet, so I'll leave it for someone else with experience.
To get guilds the current user and the user that sent the message
You can either try to use the cached members (and pray that the user is cached, which should be the case if the user sent a message but isn't guaranteed) or fetch the member.
The naive way using cache:
The guild object has a member method to get the member object from user. You can simply check that it's not undefined to see if the user is in the guild and is cached.
client.guilds.cache.filter(guild => !!guild.member(message.author));
The longer way using fetch
This will fetch the members, so with bots with high guild count may hit rate limits (check the documentation here).
var guilds = Promise.all(
client.guilds.cache.map(async guild => [
guild.id,
await guild.members.fetch(message.author).catch(() => null))
])
).then(guilds => guilds.filter(g => g[1]).map(guild => client.guilds.resolve(guild[0]));
It works by trying to fetch the member from every guild the bot is in (the fetch may fail so there's a catch which just returns null) and then filtering the guilds based on the result. There's no async filter on the collection class, so instead I map to async predicates which I then await them using Promise.all.
Sharding
If you need sharding, it should be possible to do the same using broadcastEval, but I'm not confident enough to write it yet.

Getting Warnings with Role Reaction in discord.js

Hey I recently started programming a discord bot. But now I have a problem. I'm trying to get a role reaction but somehow I keep getting errors. I'm going to link you errors and my code so maybe someone can help me.
Warnings: https://hastebin.com/ativekefod.sql
MessageReactionAddEvent.js: https://hastebin.com/nababomuta.js
Thanks for any help!
you are retrieving 3 parameters when only 2 are given, messageReaction and user,
https://discord.js.org/#/docs/main/stable/class/Client?scrollTo=e-messageReactionAdd
So essentially you are calling .message on a user class.
Change the run function to:
async run(client, reaction, user);
And then your message variable would be:
const message = reaction.message;
It's possible it's different depending on your event handler but this is most likely it

Discord JS ban all event banning members but returns no error

My server has been overwhelmed by a selfbot attack adding multiple users. I am trying to get my bot to ban all the users (few actual users) on the server but this code on a ready event doesn't seem to work. Any help is greatly appreciated. Currently this code says its banning in console but its not actually banning any of the users. (yes i know this may look bad out of context).
client.guilds.forEach(guild => {
guild.members.forEach(m => {
m.ban();
//log when member is banned in the console
console.info(`\x1b[37m\x1b[44mINFO\x1b[0m: Banned ${m.user.username}; ID: ${m.id}. (╯°□°)╯︵ ┻━┻`);
});
});
Mass banning users via a Bot is considered abusive usage, and we cannot assist you with it. If you want to clear your guild of members, Discord gives you the Prune option within Settings -> Members -> Prune which can help you remove a chunk. Please remember that abusing this to "raid" a server is considered abuse.

How to remove a role from a user in a specified server using discord.js?

I'm trying to implement a command for my bot that gives a role to a user for a determined amount of time, but I don't know how to make the bot remove or add a role to a user.
I need it to involve the server ID cause I plan on using the bot in multiple servers.
This is clearly wrong, but I hope it can help you all understand what I'm trying to do:
client.guilds.get(config.serverID).message.guild.members.get(userID).removeRole(config.donatorRole)
You almost have it! The only thing wrong is that message isn't a property of a Guild. Also, make sure to catch any errors if the Promise returned by GuildMember.removeRole() is rejected.
Here's a cleaned up example:
const guild = client.guilds.get(config.serverID);
const member = guild.members.get(userID);
member.removeRole(config.donatorRole)
.catch(console.error);

Categories