很抱歉打扰你们。我写了一段代码,但我认为这段代码有问题,因为我是 js 和 discord.js 的新手。 感谢任何帮助 (我也看了一些其他问题,但我找不到像我这样的例子)
const Discord = require('discord.js');
module.exports = {
name: "ping",
description: "Returns latency and API ping",
execute:
async (client, message, args) =>
{
try
{
const msg = await message.channel.send(`? Pinging....`);
msg.edit(`? Pong!
latency ${Math.floor(msg.createdTimestap - message.createdTimestap)}ms
API latency ${Math.round(client.ping)}ms`);
}catch(e){console.log(e)}```
}
}
这是控制台:
at Object.execute (C:\Users\berke\Desktop\RedBot\commands\ping.js:10:43)
at Client.<anonymous> (C:\Users\berke\Desktop\RedBot\bot.js:77:35)
at Client.emit (events.js:327:22)
at MessageCreateAction.handle (C:\Users\berke\Desktop\RedBot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (C:\Users\berke\Desktop\RedBot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (C:\Users\berke\Desktop\RedBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
at WebSocketShard.onPacket (C:\Users\berke\Desktop\RedBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
at WebSocketShard.onMessage (C:\Users\berke\Desktop\RedBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
at WebSocket.onMessage (C:\Users\berke\Desktop\RedBot\node_modules\ws\lib\event-target.js:132:16)
at WebSocket.emit (events.js:315:20)
答案 0 :(得分:0)
正如评论者所指出的,您在函数中接收的消息没有频道属性。
您可以调试您的应用程序并查看执行时参数的值,或者您可以console.log()
并测试它以更好地了解发生的情况。
如果您遵循 Discord.js' 文档中的指南,您可能需要查看此 repository,其中显示了动态执行命令的工作示例。
为了 stackoverflow 的目的,我将粘贴下面那个 repo 中的相关代码:
index.js
const fs = require('fs');
const Discord = require('discord.js');
const { prefix, token } = require('./config.json');
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.once('ready', () => {
console.log('Ready!');
});
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!');
}
});
commands/ping.js
module.exports = {
name: 'ping',
description: 'Ping!',
execute(message) {
message.channel.send('Pong.');
}
config.json
{
"prefix": "!",
"token": "your-token-goes-here"
}