Discord机器人踢人日志功能报错:Cannot read property 'channels' of undefined
That error you're seeing—Cannot read property 'channels' of undefined—is telling you that the client variable in your code doesn't actually point to your Discord bot instance right now. Let's break down how to fix this, plus a few other small issues in your code:
1. Make sure client is accessible in your code scope
First off, double-check where your logging code lives:
- If this is inside a
messageevent handler in your main bot file, you need to have already definedclientcorrectly at the top (likeconst client = new Discord.Client();), and the event callback should have access to it. - If you're using a separate command file (modular setup), make sure your command function receives the
clientparameter. For example, your command export should look something like this:module.exports = { name: 'kick', execute(client, message, args) { // Your kick/logging code goes here } };
If client isn't being passed into the scope where your embed code runs, it'll be undefined—hence the error.
2. Fix the channel lookup (Discord.js v12+ compatible)
Your original channel-finding code uses outdated Discord.js syntax, and you also had a bug where you tried to send the original message instead of your embed. Here's the corrected way:
- Using channel ID (more reliable than name, since names can change):
// Use cache for already-known channels const logChannel = client.channels.cache.get("735829837306003456"); - If the channel is new or not in the bot's cache yet, use
fetch(needsawaitsince it's asynchronous):const logChannel = await client.channels.fetch("735829837306003456").catch(err => console.log("Couldn't fetch log channel:", err));
And make sure you send your embed, not the original message: logChannel.send(embed) instead of logChannel.send(message).
3. Full corrected example code
Here's a cleaned-up version of your code that should work, assuming it's in a properly scoped environment with access to client:
const Discord = require('discord.js'); // Inside your kick command/event logic: if (!message.member.hasPermission('KICK_MEMBERS')) return message.reply("You don't have permission to kick users!"); const member = message.mentions.members.first(); if (!member) return message.reply("Please mention a user to kick!"); const reason = args.slice(1).join(' ') || "No reason provided"; try { // Kick the user first await member.kick(reason); // Create the log embed const embed = new Discord.MessageEmbed() .setColor("RANDOM") .setAuthor(message.author.tag, message.author.avatarURL()) .setFooter("Made by frenchee1607") .addField("Moderation Action:", "Kick") .addField("Kicked User:", member.user.username) .addField("Moderator:", message.author.username) .addField("Reason:", reason) .addField("Date:", message.createdAt.toLocaleString()); // Get the log channel const logChannel = client.channels.cache.get("735829837306003456"); if (logChannel) { await logChannel.send(embed); } else { await message.channel.send("Error sending log embed: Could not find the <#735829837306003456> channel."); } } catch (err) { console.error("Error during kick/logging:", err); message.channel.send("Something went wrong while kicking the user or sending the log."); }
4. Quick extra checks
- Make sure your bot has permissions in the log channel: It needs
Send MessagesandEmbed Linksto post the embed correctly. - If you're using a very old version of Discord.js, consider upgrading to v13/v14 (syntax changed a bit, but most guides cover this).
内容的提问来源于stack exchange,提问作者frenchee1607

