首页 > 解决方案 > 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: []
  }
}

标签: javascriptnode.jsdiscord.js

解决方案


菜鸟错误,您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]});

推荐阅读