抱歉打扰你们了。我写了一段代码,但我认为这段代码有问题,因为我是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)发布于 2021-01-12 07:20:20
正如评论者所指出的那样,在函数中接收的消息没有通道属性。
您可以调试应用程序并查看参数在执行时的值,也可以对它们执行console.log()并对其进行测试,以便更好地了解正在发生的事情。
如果你正在遵循Discord.js文档中的指南,你可能想看看这个repository,它展示了一个动态执行命令的工作示例。
出于stackoverflow的目的,我将该代码库中的相关代码粘贴到下面:
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"
}https://stackoverflow.com/questions/65675462
复制相似问题