Discord测试机器人踢人功能异常:成员在服务器内却提示‘That member was not found’求助
Hey there, let's troubleshoot why your kick command is returning that error even when the target member is definitely in the server. I spotted two main issues in your code that are causing this problem:
1. Missing the GUILD_MEMBERS Intent
Discord.js requires explicit intents to access certain server data, and you're missing the critical GUILD_MEMBERS intent. This intent allows your bot to fetch and cache server member information. Without it, your bot can't access the full list of members in the server.
Fix Steps:
- Add
GUILD_MEMBERSto your client intents array - Don't forget to enable the "Server Members Intent" in your Discord Developer Portal's Bot settings page (under the "Privileged Gateway Intents" section)
2. Relying Only on Member Cache
Using message.guild.members.cache.get() only works if the member is already in your bot's local cache. If the member joined recently, or your bot just restarted, they might not be cached yet. Instead, use members.fetch() to actively retrieve the member from Discord's servers, regardless of cache status.
Modified Working Code
Here's your updated code with both fixes applied:
require('dotenv').config(); const { Client } = require('discord.js'); // Added GUILD_MEMBERS to intents const client = new Client({ intents: ["GUILDS", "GUILD_MESSAGES", "GUILD_MEMBERS"] }); const PREFIX = "y!"; client.on('ready', () => { console.log(`${client.user.tag} has logged in.`); }); client.on('messageCreate', (message) => { if (message.author.bot) return; console.log(`[${message.author.tag}]: ${message.content}`); if (message.content === 'hello') { message.reply('konnichiwa'); message.channel.send('mata konnichiwa'); message.channel.send('konnichiwa'); } if (message.content.startsWith(PREFIX)) { const [CMD_NAME, ...args] = message.content .trim() .substring(PREFIX.length) .split(/\s+/); console.log(CMD_NAME); console.log(args); if (CMD_NAME === 'kick') { if (!message.member.permissions.has('KICK_MEMBERS')) return message.reply('You do not have permission to use that command'); if (args.length === 0) return message.reply('Please provide an ID.'); // Use fetch instead of cache.get to retrieve members reliably message.guild.members.fetch(args[0]) .then(member => { member.kick() .then(() => message.channel.send(`${member} was kicked`)) .catch(err => message.channel.send('I do not have permissions to kick this member...')); }) .catch(err => message.channel.send('That member was not found...')); } else if (CMD_NAME === 'ban') { if (!message.member.permissions.has('BAN_MEMBERS')) return message.reply('You do not have permission to use that command'); if (args.length === 0) return message.reply('Please provide an ID.'); // Updated ban command to use fetch for consistency message.guild.members.fetch(args[0]) .then(member => { message.guild.members.ban(member) .then(() => message.channel.send(`${member} was banned`)) .catch(err => message.channel.send('I do not have permissions to ban this member...')); }) .catch(err => message.channel.send('That member was not found...')); } } }) client.login(process.env.DISCORDJS_BOT_TOKEN);
Additional Checks
- Make sure your bot has the Kick Members permission enabled in the server role settings
- Double-check that you're inputting the correct user ID (not a username or nickname) when running the
y!kickcommand
内容的提问来源于stack exchange,提问作者GohanHango

