javascript - 我用js做了setprefix,它有一个错误
问题描述
我有一个不和谐的音乐机器人,它有很多功能。我先做了一个setprefix
非常完美的命令,然后help
出现了错误。错误是:
TypeError: Cannot read property 'name' of undefined
我不知道该怎么做有人可以帮助我吗?
help.js
:
const { MessageEmbed } = require('discord.js')
module.exports = {
info: {
name: "help",
description: "To show all commands",
usage: "[command]",
aliases: ["commands", "help me", "pls help"]
},
execute(client, message, args){
var allcmds = "";
client.commands.forEach(cmd => {
let cmdinfo = cmd.info
allcmds+="``"+message.client.prefix+cmdinfo.name+" "+cmdinfo.usage+"`` ~ "+cmdinfo.description+"\n"
})
let embed = new MessageEmbed()
.setAuthor("Commands of "+client.user.username, "https://raw.githubusercontent.com/SudhanPlayz/Discord-MusicBot/master/assets/Music.gif")
.setColor("BLUE")
.setDescription(allcmds)
.setFooter(`To get info of each command you can do ${client.config.prefix}help [command] and my prefix is % | Hander by Lavaboy0192#2014`)
if(!args[0])return message.channel.send(embed)
else {
let cmd = args[0]
let command = client.commands.get(cmd)
if(!command)command = client.commands.find(x => x.info.aliases.includes(cmd))
if(!command)return message.channel.send("Unknown Command")
let commandinfo = new MessageEmbed()
.setTitle("Command: "+command.info.name+" info")
.setColor("YELLOW")
.setDescription(`
Name: ${command.info.name}
Description: ${command.info.description}
Usage: \`\`${client.config.prefix}${command.info.name} ${command.info.usage}\`\`
Aliases: ${command.info.aliases.join(", ")}
`)
message.channel.send(commandinfo)
}
}
}
设置前缀命令:
module.exports = {
name: "setprefix",
description: "Sets the prefix for the bot",
argsNumber: 1,
usage: "<prefix>",
execute(message, args){
const prefix = args.shift();
message.client.prefixes.set(message.guild.id, prefix);
message.reply(`Prefix set to ${prefix}`);
}
}
解决方案
这是因为您导出的模块上没有info
属性。从您help.js
文件中的导出来看,您的其他命令文件导出一个带有两个键的对象;info
和execute
。
在您的execute
方法中,您help.js
迭代client.commands
并尝试获取info
属性(let cmdinfo = cmd.info
)的每个命令。由于您的命令没有info
道具,因此将是未定义的。在下一行,您尝试获取, ,但由于它无法读取属性,它会抛出"TypeError: Cannot read property 'name' of undefined"。setprefix
cmdinfo
cmdinfo.name
cmdinfo.usage
cmdinfo.description
name
尝试运行下面的代码片段,它会抛出同样的错误:
let cmd = {
name: 'setprefix',
description: 'Sets the prefix for the bot',
argsNumber: 1,
usage: '<prefix>',
execute: {},
};
let cmdinfo = cmd.info;
console.log(cmdinfo.name);
要解决此问题,您需要添加一个新info
对象并移动除execute
其内部之外的所有内容,如下面的代码片段所示。尝试运行它,您可以看到它name
正确打印:
let cmd = {
info: {
name: 'setprefix',
description: 'Sets the prefix for the bot',
argsNumber: 1,
usage: '<prefix>',
},
execute: {},
};
let cmdinfo = cmd.info;
console.log(cmdinfo.name);
您还需要使用 key 添加一个数组aliases
,即使没有,因为上面的代码也试图找到它们。这是您的最终代码。
module.exports = {
info: {
name: 'setprefix',
description: 'Sets the prefix for the bot',
argsNumber: 1,
usage: '<prefix>',
aliases: [],
},
execute(message, args) {
const prefix = args.shift();
message.client.prefixes.set(message.guild.id, prefix);
message.reply(`Prefix set to ${prefix}`);
},
};
推荐阅读
- reactjs - 在本机反应中在右侧对齐文本行
- javascript - 在下拉选择中打印图像?
- python - 如何使用具有 root 权限的易受攻击的程序进行权限升级?
- laravel - 将第三方包设置为自定义服务提供商
- javascript - Firebase 错误 Firebase 存储:索引 0 处的“put”中的参数无效:预期的 Blob 或文件
- visual-studio-code - 无法解析时如何将包添加到.Net Core 控制台应用程序?
- php - Azure AD SSO = AADSTS50020 - 多租户,如何允许外部租户注册到我的应用程序?
- py2app - 在 hello word 程序上使用官方 py2app 指令使 python2.7 文件可执行时出错
- c# - 如何使用 Xamarin.Forms 的媒体插件在共享代码中实现 iOS 覆盖?
- java - 如何将流减少(计数)应用于 IntStream 并将返回类型更改为“int”