Discord js 通过 broadcastEval 发送消息

Discord js Send Messages via broadcastEval

我在谷歌上搜索了一下,但我找不到真正的答案,因为分片似乎不是一个很受欢迎的论点,那么我们就在这里。

我正在尝试处理我的机器人中的分片,但我遇到了问题。我有接受消息作为参数的自定义事件,我非常想让它们与 broadcastEval

一起工作

我已经尝试了 ${message} 但无法正常工作,因为它仅序列化文本,而我想要对象

我已经尝试了 JSON.stringify,但由于同样的原因也无法正常工作

我有点卡住了。我正在使用 Discord JS

这是调用函数

  function test2(_guild_id, _args, _message, gameInfo, _originalMessage, reaction_id){

   let msg = JSON.stringify(_message);
   let cmd = "command";
   let args = "args";
   let rights = 0;

   let stringToEval = `
   this.emit("SHARE_DATA", "${_message}", {
     guild_id: ${_guild_id},
     cmd: ${cmd},
     args: ${args},
     rights: ${rights}
   });
   `;
   log.debug(stringToEval);
/*
   // this is not working. I receive any sort of exceptions related to the fact
   // that it is serializing the text of the message only
   discordClient.shard.broadcastEval(stringToEval);
/*/
// this is working as intended
discordClient.emit("SHARE_DATA", _message, {
    guild_id: _guild_id,
    cmd: cmd,
    args: args,
    rights: rights
  });
//*/

}

这是监听器

client.on("SHARE_DATA", (message, support) => {
  log.debug("Let's see if this shard id can be found .... it should be "+registered_shard_id);
  log.debug(`received message ${message} with support ${support}`);
  log.debug("we are on shard "+JSON.stringify(client.shard));
});

使用常规事件和单个分片应用程序,就像现在事件正确触发完全没有问题,我只需添加 discordClient.emit,一切都很好。 但是 broadcastEval 需要一个字符串,所以我需要序列化消息

编辑: 使用 msg 变量广播 eval (JSON.stringified) 产生此错误

[DEBUG] [2020-12-14_09:58:35] [1|GodCommands]: 
   this.emit("SHARE_DATA", "{"channelID":"752335384037294160","deleted":false,"id":"787981855374442526","type":"DEFAULT","content":",test2","authorID":"323030704348987404","pinned":false,"tts":false,"nonce":"787981857693106176","system":false,"embeds":[],"attachments":[],"createdTimestamp":1607939914316,"editedTimestamp":null,"webhookID":null,"applicationID":null,"activity":null,"flags":0,"reference":null,"guildID":"511487725849804804","cleanContent":",test2"}", {
     guild_id: 511487725849804804,
     cmd: command,
     args: args,
     rights: 0
   });
   
[ERROR] [2020-12-14_09:58:35] [1|RotBot]: SyntaxError: missing ) after argument list
    at Client._eval (E:\Sviluppo\Swgoh\Bot\RotBot2\node_modules\discord.js\src\client\Client.js:391:17)
    at ShardClientUtil._handleMessage (E:\Sviluppo\Swgoh\Bot\RotBot2\node_modules\discord.js\src\sharding\ShardClientUtil.js:185:82)
    at process.emit (events.js:326:22)
    at emit (internal/child_process.js:906:12)
    at processTicksAndRejections (internal/process/task_queues.js:81:21)

我结束时使用了不同的方法 我实际上将序列化对象保存在数据库中,只有数据,没有方法。

然后我只需发送 DB id,事件处理程序就会像这样重建消息

    message = new Discord.Message(discordClient, {
      id: msg.id,
      type: msg.type,
      content: msg.content,
      author: usr,
      pinned: msg.pinned,
      tts: msg.tts,
      embeds: msg.embeds,
      attachments: msg.attachments,
      nonce: "123" // idfk
  }, channel
  );

序列化只需

let serialMessage = JSON.stringify(message);

编辑(TNX 等离子鸡):解决方案的另一个版本:

let serialMessage = JSON.stringify(message);
// other code
message = new Discord.Message(discordClient, serialMessage, channel);