Javascript 命令处理程序-未定义消息
最近,作为一种学习体验,我一直在基于旧代码启动一个新的bot。你可以想象,这是一个故障排除。我的命令处理程序基于三个方面:functions.js、app.js和包含命令的文件。我在使用“帮助”命令时遇到问题 我的命令处理程序和其他内容:Javascript 命令处理程序-未定义消息,javascript,node.js,discord,discord.js,Javascript,Node.js,Discord,Discord.js,最近,作为一种学习体验,我一直在基于旧代码启动一个新的bot。你可以想象,这是一个故障排除。我的命令处理程序基于三个方面:functions.js、app.js和包含命令的文件。我在使用“帮助”命令时遇到问题 我的命令处理程序和其他内容: // Calling Packages const Discord = require('discord.js'); const bot = new Discord.Client(); const client = new Discord.Client();
// 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
,而不是msg
消息未定义的原因是您尚未导入引用消息的文件。例如,这是消息
文件底部的模块.导出
或导出默认值
。查看函数
文件的顶部,消息没有传入,只有频道
传入
只是一点建议,通过
standard.js
运行您的文件,它是一个NPM模块:)-可以通过执行NPM i standard
来安装。让我知道你进展如何 functions.js
和commands/help.js
之间代码重复的原因是什么?看起来您在以两种不同的方式处理命令,functions.js
中的帮助函数不接受message
参数,因此它将被取消定义为`message.author.send({embed})`和here`message.channel.send(`您试图获取消息,但无法从任何地方获取消息,只需在函数help:function(channel,message)
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}!**`
}})
}
[serve] message is not defined
[serve] CharleDarwins ran the command: help