【问题标题】:Discord.js Sharding, How do you send an embed message with broadcastEval?Discord.js 分片,如何使用 broadcastEval 发送嵌入消息?
【发布时间】:2020-11-04 06:19:08
【问题描述】:

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

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]”。

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

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

【问题讨论】:

    标签: discord.js sharding


    【解决方案1】:

    好的,我通过在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)
                 }
           })()
    

    【讨论】:

    • ahm 你现在可以发送工作代码吗,因为我也在为同样的问题而苦苦挣扎,但我无法真正关注你的解决方案文本
    • 你也可以这样做:```client.shard.broadcastEval(` (async () => { const Discord = require('discord.js'); let channel = await this.channels. get("683353482748756047"); if(channel){ channel.send(new Discord.RichEmbed(data)) } })() ``` 在我的示例中,这很有帮助,因为它有助于在父级中形成丰富的嵌入函数并将消息作为参数传递。
    【解决方案2】:

    为了使用 broadcastEval 发送嵌入消息,您需要对嵌入进行字符串化。在你的情况下:

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

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

    【讨论】:

      猜你喜欢
      • 2021-08-22
      • 2021-03-24
      • 2022-08-04
      • 2021-05-08
      • 1970-01-01
      • 2017-10-31
      • 2021-12-01
      • 2020-08-04
      相关资源
      最近更新 更多