我的不和谐机器人正在处理帮助命令。 帮助命令访问的我的命令列表文件是:
{
"Help": {
"name":"Help",
"group":"User",
"desc":"Displays a list of commands",
"usage":"help [group OR command]"
},
"Purge": {
"name":"Purge",
"group":"Admin",
"desc":"Deletes a specified number of messages",
"usage":"Purge <amount>"
}
}
这些只是定义命令的组,名称和用法。到目前为止,帮助命令的代码为:
const Discord = require('discord.js');
const bot = new Discord.Client();
const client = new Discord.Client();
const weather = require('weather-js');
const fs = require('fs');
const commands = JSON.parse(fs.readFileSync('Storage/commands.json', 'utf8'))
const token = "<my token>"
const prefix = 'cb!';
bot.on('message', message => {
// Variables
let msg = message.content.toUpperCase();
let sender = message.author;
let cont = message.content.slice(prefix.length).split(" ");
let args = cont.shift().toLowerCase();
if (message.content.startsWith(prefix+'help')) {
console.log('ok i hate this')
const embed = new Discord.RichEmbed()
.setColor(0x1D82B6)
let commandsFound = 0;
for (var cmd in commands) {
if (commands[cmd].group.toUpperCase() === 'USER') {
commandsFound++
embed.addField(`${commands[cmd].name}`, `**Description:** ${commands[cmd].desc}\n**Usage:** ${prefix + commands[cmd].usage}`);
}
}
embed.setFooter(`Currently showing user commands. To view another group do ${prefix}help [group / command]`)
embed.setDescription(`**${commandsFound} commands found** - <> means required, [] means optional`)
message.author.send({embed})
message.channel.send({embed: {
color: 0x1D82B6,
description: `**Check your DMs ${message.author}!**`
}})
} else {
// Variables
let groupFound = '';
for (var cmd in commands) {
if (args.join(" ").trim().toUpperCase() === commands[cmd].group.toUpperCase()) {
groupFound = commands[cmd].group.toUpperCase();
break;
}
}
if (groupFound != '') {
for (var cmd in commands) {
const embed = new Discord.RichEmbed()
.setColor(0x1D82B6)
let commandsFound = 0;
if (commands[cmd].group.toUpperCase() === groupFound) {
commandsFound++
embed.addField(`${commands[cmd].name}`, `**Description:** ${commands[cmd].desc}\n**Usage:** ${prefix + commands[cmd].usage}`);
}
}
embed.setFooter(`Currently showing ${groupFound} commands. To view another group do ${prefix}help [group / command]`)
embed.setDescription(`**${commandsFound} commands found** - <> means required, [] means optional`)
message.author.send({embed})
message.channel.send({embed: {
color: 0x1D82B6,
description: `**Check your DMs ${message.author}!**`
}})
}
}
});
如果我键入“ cb!help admin”,我将在控制台中收到此错误
if (args.join(" ").trim().toUpperCase() === commands[cmd].group.toUpperCase()) {
^
TypeError: args.join is not a function
该如何解决?我也尝试过if (args[0].join...
,但这没用。
一如既往,感谢您抽出宝贵的时间阅读本文。我基于过时的代码,因此可能还有其他错误。我正在尝试全部修复它们。
答案 0 :(得分:1)
args
在您的代码示例中有一个定义:
let args = cont.shift().toLowerCase();
这使args string
-string
没有方法join()
,因为join()
是Array原型链的一部分,字符串不从中继承。 shift()
将返回cont
数组的第一个元素,因此您可能只想调用args.toUpperCase()
,尽管我建议重命名变量以使args
的含义正确更清晰。
答案 1 :(得分:1)
您应该尝试在顶部交换两个变量,这样
let args = message.content.slice(prefix.length).split(' ');
let cont = args.shift().toLowerCase();
然后将args放入if语句中
if (args[0].toUpperCase === commands[cmd].group.toUpperCase) { /* Your code... */ }
您的args已经存储在数组中,因此不需要修剪它们,因此也不需要join函数。
希望这会有所帮助!