'My bot keeps saying "User not found" though the user is on my server
this is my code. I been only able to mute some bots and myself and a few users but not all, I'm not sure if I'm doing anything wrong but any help would be appreciated :)
else if (isValidCommand(message, 'mute')) {
message.delete()
if (!message.member.hasPermission(['KICK_MEMBERS', 'BAN_MEMBERS'])){
message.channel.send("You don't have permission to use this command.");
}
else {
let memberId = message.content.substring(message.content.indexOf(' ') + 1);
let member = message.guild.members.cache.get(memberId);
if (member) {
if (member.hasPermission(['KICK_MEMBERS', 'BAN_MEMBERS']) && !message.member.hasPermission('ADMINISTRATOR')) {
message.channel.send("You cannot mute that person!");
}
else {
let mutedRole = message.guild.roles.cache.get('956241047753744414');
if (mutedRole) {
member.roles.add(mutedRole);
message.channel.send("User was muted.");
}
else {
message.channel.send("Muted role not found.");
}
}
}
else {
message.channel.send("Member not found.");
}
}
}
Solution 1:[1]
Maybe the user is offline? Maybe the guild member count is large enough not for the bot to load all of the members at once? Well, then there are two things I must then tell.
- Use intents that allow your bot to cache guild members
- Instead of using
cache.geton member list, usefetchto fetch a uncached member. (more info at djs guide: https://discord.js.org/#/docs/discord.js/stable/class/GuildMemberManager?scrollTo=fetch )
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rovel Stars |
