Search code examples
javascriptdiscord.jssharding

Discord js Send Messages via broadcastEval


i'm googling since a bit and i can't find a real answer since sharding seems not a very popular argument, then here we are.

I'm trying to handle sharding in my bot and i have a problem. I have custom events that accept messages as paremeter and i'm going crazy to let them work with broadcastEval

i have tryed ${message} and is not working since it serialize the text only and i want the object

i have tryed JSON.stringify and is not working either for the same reason

i'm a bit stuck. i'm using Discord JS

this is the caller function

  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
  });
//*/

}

and this is the listener

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));
});

with regular events and a single shard application like it is now that event fire correctly withno problem at all, i just add discordClient.emit and all is fine. but broadcastEval wants a string so i need to serialize the message

EDIT: broadcast eval using msg variabe (JSON.stringified) produzce this error

[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)

Solution

  • I ended using a difrferent approach I actually save the serialized object on database, with data only and no methods.

    then i simply send the DB id and the event handler reconstruct the message like this

        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
      );
    

    the serialization is done simply by

    let serialMessage = JSON.stringify(message);
    

    EDIT(TNX PLASMA Chicken): another version of the solution:

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