最近,我一直在基于旧代码启动一个新的bot作为学习经验。可以想象,这有点麻烦。我的命令处理程序基于三件事:functions.js,app.js和具有命令的文件。我在使用help命令时遇到问题 我的命令处理程序和其他内容:
// Calling Packages
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 {ping: func} = require("C:/Users/LeviB/Desktop/Folders/Downloads, Setup, and Uninstall Files/Code/Discord Bots/WelcomeGoodbyeQuickDB/functions.js")
const commands = JSON.parse(fs.readFileSync('Storage/commands.json', 'utf8'))
// Global Settings
const prefix = 'cb!';
// Listener Event One
bot.on('message', message => {
// Variables
let msg = message.content.toUpperCase();
let sender = message.author;
let cont = message.content.slice(prefix.length).trim().split(" ");
let args = message.content.slice(prefix.length).trim().split(" ");
let cmd = args.shift().toLowerCase();
if (sender.bot) return;
if (!message.content.startsWith(prefix)) return;
//Command Handler
try {
let commandFile = require(`./commands/${cmd}.js`);
commandFile.run(bot, message, args, func);
} catch(e) {
console.log(e.message);
} finally {
console.log(`${message.author.username} ran the command: ${cmd}`);
}
我的功能文件
help: function(channel) {
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}!**`
}})
},
}
还有我要运行的命令:
let msg = message.content.toUpperCase();
let sender = message.author;
let cont = message.content.slice(prefix.length).trim().split(" ");
let args = message.content.slice(prefix.length).trim().split(" ");
exports.run = (bot, message, args, func) => {
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`)
msg.author.send({embed})
msg.channel.send({embed: {
color: 0x1D82B6,
description: `**Check your DMs ${msg.author}!**`
}})
}
如果我要执行“ cb!ping”或“ cb!purge”操作,那么它们都会起作用。当我执行“ cb!help”时,虽然我在控制台中得到了此提示:
[serve] message is not defined
[serve] CharleDarwins ran the command: help
我已经做了一些修修补补或其他工作,但是我不知道它想要我做什么。这可能是一个悬而未决的问题,但是它要我放在哪里?和往常一样,我的术语可能不对,所以我深表歉意,并感谢您抽出宝贵时间阅读我的问题。
答案 0 :(得分:0)
我明白了。在您要运行的命令文件中,您执行let msg
,在文件中,您引用的是message
,而不是msg
。
消息未定义的原因是,您尚未从任何位置导入引用message
的文件。例如,这是module.exports
文件底部的export default
或message
。查看您的functions
文件的顶部,没有传递消息,只有channel
被传递。
仅需一点建议,即可通过standard.js
运行文件,它是一个NPM模块:)-可以通过执行npm i standard
进行安装。让我知道你过得怎么样!