【发布时间】:2020-02-21 15:44:54
【问题描述】:
有人可以帮助我不知道如何解决它 这是我得到的错误代码低于我试图运行一个命令处理程序我认为你们聪明的人可以帮助提前谢谢
错误:index.js:33 let commandfile = client.commands.get(command.slice(prefix.length)); ^
ReferenceError: 命令未定义
const Discord = require('discord.js');
const { prefix, token} = require ('./config.json');
const client = new Discord.Client();
const fs = require("fs");
client.commands = new Discord.Collection();
fs.readdir("./commands/", (err, files) => {
if(err) console.log(err);
let jsfile = files.filter(f => f.split(".").pop() === "js")
if(jsfile.length <= 0){
console.log("Couldn't find commands.");
return;
}
jsfile.forEach((f, i) => {
let props = require(`./commands/${f}`);
console.log(`${f} loaded!`);
client.commands.set(props.help.name, props);
});
});
// The ready event is vital, it will tell you if your bot is on it the console.
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
})
client.on('message', message => {
//Ignore messages that aren't from a guild
if (!message.guild) return;
let commandfile = client.commands.get(command.slice(prefix.length));
if(commandfile) commandfile.run(bot,message,args);
// // KICK COMMAND
// // If the message content starts with "!kick"
// if (message.content.startsWith('!kick')) {
// // Assuming we mention someone in the message, this will return the user
// // Read more about mentions over at https://discord.js.org/#/docs/main/stable/class/MessageMentions
// const user = message.mentions.users.first();
// // If we have a user mentioned
// if (user) {
// // Now we get the member from the user
// const member = message.guild.member(user);
// // If the member is in the guild
// if (member) {
// /**
// * Kick the member
// * Make sure you run this on a member, not a user!
// * There are big differences between a user and a member
// */
// member.kick('Optional reason that will display in the audit logs').then(() => {
// // We let the message author know we were able to kick the person
// message.reply(`Successfully kicked ${user.tag}`);
// }).catch(err => {
// // An error happened
// // This is generally due to the bot not being able to kick the member,
// // either due to missing permissions or role hierarchy
// message.reply('I was unable to kick the member');
// // Log the error
// console.error(err);
// });
// } else {
// // The mentioned user isn't in this guild
// message.reply('That user isn\'t in this guild!');
// }
// // Otherwise, if no user was mentioned
// } else {
// message.reply('You didn\'t mention the user to kick!');
// }
// }
// KICK COMMAND
// If the message content starts with "!ban"
if (message.content.startsWith('!ban')) {
// Assuming we mention someone in the message, this will return the user
// Read more about mentions over at https://discord.js.org/#/docs/main/stable/class/MessageMentions
const user = message.mentions.users.first();
// If we have a user mentioned
if (user) {
// Now we get the member from the user
const member = message.guild.member(user);
// If the member is in the guild
if (member) {
/**
* ban the member
* Make sure you run this on a member, not a user!
* There are big differences between a user and a member
*/
member.ban('Optional reason that will display in the audit logs').then(() => {
// We let the message author know we were able to kick the person
message.reply(`Successfully banned ${user.tag}`);
}).catch(err => {
// An error happened
// This is generally due to the bot not being able to kick the member,
// either due to missing permissions or role hierarchy
message.reply('I was unable to ban the member');
// Log the error
console.error(err);
});
} else {
// The mentioned user isn't in this guild
message.reply('That user isn\'t in this guild!');
}
// Otherwise, if no user was mentioned
} else {
message.reply('You didn\'t mention the user to ban!');
}
}
});
client.login(token);
【问题讨论】:
-
command应该是这里的字符串吗?因为我在该代码的其他任何地方都看不到command。因此你的错误。 -
cmd 是在里面,只是无法识别
标签: discord.js