javascript - Discord bot 在命令处理程序问题中嵌入消息
问题描述
当我的代码开始失控时,我正在玩不和谐机器人,因为代码太多。所以我决定做一些命令处理。
首先,我尝试使用我的 ping 命令执行此操作,但它似乎不起作用。
main.js
:_
const config = require("./config.json");
const Discord = require('discord.js');
const { clientUser } = require("discord.js");
const clients = new Discord.Client({ intents: 14087});
const prefix = "-scp ";
var version = '1.6.6';
const packageJSON = require("./package.json");
const fs = require(`fs`);
const commandFiles= fs.readdirSync('./commands/')
.filter(file => file.endsWith('.js'));
for(const file of commandFiles){
const command = require(`./commands/${file}`);
clients.commands.set(command.name,command)
}
clients.on("messageCreate", (message) => {
try{
if( message.content == prefix + "ping" ){
clients.commands.get('ping').execute(message,args);
}
“ping”的文件是 ping.js
const Discord = require("discord.js");
module.exports = {
name:'ping',
description:"ping command",
execute(message,args){
const embed = new Discord.MessageEmbed()
.setTitle("Pong! ")
.setDescription("clients's latency: " +
`**${message.createdTimestamp - Date.now()}ms**` +
"\n" + `API's Latency: **${Math.round(clients.ws.ping)}ms**`)
.setColor('RANDOM');
channel.send({embeds: [embed]});
}
}
错误:
C:\Users\1love\Dropbox\My PC (DESKTOP-MEST1TS)\Desktop\Bot Core Assets\SCP bot assets\node_modules\discord.js\src\rest\RequestHandler.js:298
throw new DiscordAPIError(data, res.status, request);
^
> DiscordAPIError: Cannot send an empty message
at RequestHandler.execute (C:\Users\1love\Dropbox\My PC (DESKTOP-MEST1TS)\Desktop\Bot Core Assets\SCP bot assets\node_modules\discord.js\src\rest\RequestHandler.js:298:13)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async RequestHandler.push (C:\Users\1love\Dropbox\My PC (DESKTOP-MEST1TS)\Desktop\Bot Core Assets\SCP bot assets\node_modules\discord.js\src\rest\RequestHandler.js:50:14)
at async TextChannel.send (C:\Users\1love\Dropbox\My PC (DESKTOP-MEST1TS)\Desktop\Bot Core Assets\SCP bot assets\node_modules\discord.js\src\structures\interfaces\TextBasedChannel.js:171:15) {
method: 'post',
path: '/channels/417093499167440896/messages',
code: 50006,
httpStatus: 400,
requestData: {
json: {
content: undefined,
tts: false,
nonce: undefined,
embeds: undefined,
components: undefined,
username: undefined,
avatar_url: undefined,
allowed_mentions: undefined,
flags: undefined,
message_reference: undefined,
attachments: undefined,
sticker_ids: undefined
},
files: []
}
}
解决方案
菜鸟错误,您embed
在定义之前尝试发送:
const embed = new Discord.MessageEmbed()
.setTitle("Pong! ")
.setDescription("clients's latency: "
+ `**${message.createdTimestamp - Date.now()}ms**`
+ "\n" + `API's Latency: **${Math.round(clients.ws.ping)}ms**`)
.setColor('RANDOM');
channel.send({embeds: [embed]});
推荐阅读
- javascript - 如何在按键上获取当前值
- can-bus - Automatic Node ID assignment for LSS Slaves without LSS Master
- android - Appcompat 工具栏 showAsAction 无法正常工作
- python - Module loading behavior in virtualenv differs depending on caller
- reactjs - 在 xstate 中模拟一个调用的 Promise
- file - sys_file_metadata 表扩展但字段未显示
- android - 是否需要再次创建拦截器来更新标头?
- wordpress - 如何翻译contact form7报警语言?
- swift - 通过推送通知发送图像在 iOS 中不起作用
- c++ - 为什么我不能返回向量
> (进程返回-1073741819 (0xC0000005))