Discord.js分片,如何使用broadcastEval发送嵌入消息?

时间:2020-07-14 20:05:51

标签: discord.js sharding

我正尝试使用分片漫游器将嵌入消息发送到特定频道。 通过此代码,我已经成功发送了一条简单的消息:

client.shard.broadcastEval(`
      (async () => {
             let channel = await this.channels.get("683353482748756047"); 
             channel.send("Hello")
      })()
`)

当我要发送嵌入消息时,问题开始了。我试过像这样传递变量:

//exampleEmbed is created
client.shard.broadcastEval(`
      (async () => {
             let channel = await this.channels.get("683353482748756047"); 
             channel.send('${exampleEmbed}')
      })()
`)

但是消息的发送方式类似于“ [object Object]”。

我曾考虑过将channel对象返回到broadcastEval之外,然后发送我的变量,但是我读到这是不可能的,因为您无法返回完整的不和谐对象。

我应该如何发送嵌入消息?谢谢您的时间。

2 个答案:

答案 0 :(得分:2)

好的,我通过在broadcastEval内部创建嵌入消息并使用'$ {}'语法对其进行修饰来解决它。

示例:

client.shard.broadcastEval(`
      (async () => {
             const Discord = require('discord.js');
             let channel = await this.channels.get("683353482748756047"); 
             if(channel){
                  //if shard has this server, then continue.
                  let message = new Discord.RichEmbed()
                  .setThumbnail(this.user.displayAvatarURL)
                  .setTitle('Title')
                  .addField("Something useful:", '${useful}')
                  .addField("Another useful thing:", '${useful2}')
                  .setTimestamp()
                    
                  channel.send(message)
             }
       })()

答案 1 :(得分:1)

为了使用broadcastEval发送嵌入消息,您需要对嵌入进行字符串化。就您而言:

    client.shard.broadcastEval(`
        (async () => {
            const channel = await this.channels.cache.get('683353482748756047');
            if (channel) {
                channel.send({ embed: ${JSON.stringify(exampleEmbed)} });
            }
        })();
    `);

请记住,broadcastEval在所有分片中运行代码,因此您需要检查频道是否存在,否则会遇到错误。