【发布时间】:2021-06-01 14:40:18
【问题描述】:
所以我正在使用这个命令处理程序来使我的不和谐机器人更加坚固,但我完全不知道如何使用外部命令发送消息;特别定义味精。这是我的命令处理程序代码:
const Discord = require('discord.js');
const prefix = "%"
const fs = require('fs');
const client = new Discord.Client();
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
client.on("ready", () => {
console.log("Bot connected!")
client.user.setPresence({
status: 'online',
activity: {
name: 'Online',
type: 'PLAYING',
url: 'https://www.google.com/'
}
})
})
client.on('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
if (!client.commands.has(command)) return;
try {
client.commands.get(command).execute(message, args);
} catch (error) {
console.error(error);
message.reply('there was an error trying to execute that command!');
}
});
client.login("<insert my token here>")
而且我没有任何“ping.js”代码,因为我想尝试并在网上找到的所有东西都没有奏效。感谢您的帮助!
【问题讨论】:
-
看起来您的
index.js文件已根据Discord.js指南正确设置。您的客户端on message事件处理程序查看每条消息并检查消息前缀,在您的情况下为%。然后代码拆分消息并检查命令名称是否与集合匹配。如果找到匹配项,则将该消息转发给相应的命令。您能否提供有关错误的更多信息或更新您的问题以包含您尝试调用的命令文件? -
对不起,我没有很好地说明我的问题。在“ping.js”中,味精是未定义的,我不知道如何定义它。我可以用 u 函数定义它吗?任何帮助都会得到帮助,谢谢!
标签: node.js discord discord.js