Discord.js 是否有人可以帮助cmd未定义
有人能帮忙吗?我不知道怎么修 这是我得到的错误代码如下,我试图运行一个命令处理程序,我想你们聪明的人可以帮助提前感谢 错误:index.js:33 let commandfile= client.commands.get(command.slice(prefix.length)); ^ ReferenceError:未定义命令Discord.js 是否有人可以帮助cmd未定义,discord.js,Discord.js,有人能帮忙吗?我不知道怎么修 这是我得到的错误代码如下,我试图运行一个命令处理程序,我想你们聪明的人可以帮助提前感谢 错误:index.js:33 let commandfile= client.commands.get(command.slice(prefix.length)); ^ ReferenceError:未定义命令 const Discord=require('Discord.js'); const{prefix,token}=require('./config.json'); co
const Discord=require('Discord.js');
const{prefix,token}=require('./config.json');
const client=new Discord.client();
常数fs=要求(“fs”);
client.commands=new Discord.Collection();
fs.readdir(“./commands/”,(错误,文件)=>{
if(err)console.log(err);
让jsfile=files.filter(f=>f.split(“.”.pop()==“js”)
if(jsfile.length){
让props=require(`./commands/${f}`);
log(`${f}已加载!`);
client.commands.set(props.help.name,props);
});
});
//ready事件非常重要,它会在控制台上告诉您是否有机器人。
client.on('ready',()=>{
log(`Logged as${client.user.tag}!`);
})
client.on('message',message=>{
//忽略不是来自公会的消息
如果(!message.guild)返回;
让commandfile=client.commands.get(command.slice(prefix.length));
if(commandfile)commandfile.run(bot、message、args);
////KICK命令
////如果消息内容以“!kick”开头
//if(message.content.startsWith(“!kick”)){
////假设我们在消息中提到某人,这将返回用户
////阅读更多关于提及的内容,请访问https://discord.js.org/#/docs/main/stable/class/MessageMentions
//const user=message.indications.users.first();
////如果我们提到一个用户
//如果(用户){
////现在我们从用户处获取成员
//const member=message.guild.member(用户);
////如果成员在公会中
//国际单项体育联合会(成员){
// /**
//*踢队员
//*确保您在成员而不是用户上运行此操作!
//*用户和会员之间存在很大差异
// */
//kick('将显示在审核日志中的可选原因')。然后(()=>{
////我们让消息作者知道我们可以踢那个人
//message.reply(`Successfully${user.tag}`);
//})。捕获(错误=>{
////发生了一个错误
////这通常是由于机器人无法踢该成员,
////由于缺少权限或角色层次结构
//message.reply('我无法踢该成员');
////记录错误
//控制台错误(err);
// });
//}其他{
////提到的用户不在该公会中
//message.reply('该用户不在此帮会中!');
// }
////否则,如果没有提到用户
//}其他{
//message.reply('您没有提到要踢的用户!');
// }
// }
//踢指令
//如果消息内容以“!ban”开头
if(message.content.startsWith(“!ban”)){
//假设我们在消息中提到某人,这将返回用户
//阅读更多关于提及的内容,请访问https://discord.js.org/#/docs/main/stable/class/MessageMentions
const user=message.indications.users.first();
//如果我们有一个用户提到
如果(用户){
//现在我们从用户那里获取成员
const member=message.guild.member(用户);
//如果会员是公会会员
国际单项体育联合会(成员){
/**
*禁止该成员
*确保您在成员而不是用户上运行此操作!
*用户和成员之间有很大的区别
*/
member.ban('将显示在审核日志中的可选原因')。然后(()=>{
//我们让消息作者知道我们可以踢那个人
message.reply(`Successfully banked${user.tag}`);
}).catch(错误=>{
//发生了一个错误
//这通常是由于机器人无法踢该成员,
//由于缺少权限或角色层次结构
message.reply('我无法禁止该成员');
//记录错误
控制台错误(err);
});
}否则{
//提到的用户不在此公会中
message.reply('该用户不在此帮会中!');
}
//否则,如果没有提到用户
}否则{
message.reply('您没有提到要禁止的用户!');
}
}
});
client.login(令牌);
在第33行,您尝试引用名为命令的属性,但它没有在任何地方定义。
您希望该命令来自何处/您希望它能保存什么?似乎缺少的步骤是检查消息
属性,对其进行处理,并将其用作命令,例如:
command
应该是字符串吗?因为我看不到该代码中的任何其他地方的命令。因此,您的error.cmd是一个不可识别的错误
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);
// Our standard argument/command name definition.
const args = message.content.slice(client.config.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
// Grab the command data from the client.commands Enmap
const cmd = client.commands.get(command);