【问题标题】:How to edit an embed with your bot Discord.js?如何使用您的机器人 Discord.js 编辑嵌入?
【发布时间】:2020-08-03 06:44:20
【问题描述】:

如何使用我的机器人编辑发送的嵌入?首先我构建了一个嵌入:

const countdownEmbed = {
color: 0x0099ff,
title:('Countdown'),
author: {
    name:`${user_name}`,
    icon_url: `${user_pfp}`,
},
description: 'Your countdown starts in **3 seconds**',
thumbnail: {
    url: `${client_pfp}`,
},
timestamp: new Date(),
footer: {
    text: `© ${client_name}`, 
    icon_url: `${client_pfp}`,
},
};

然后我做了一个新的嵌入:

const countdownEmbed2 = {
    title:("New title!"),
    description: 'Your countdown starts in **2 seconds**',
};

创建“更新”嵌入后,我尝试发送消息,然后在一秒钟后对其进行编辑:

message.channel.send({ embed: countdownEmbed })
.then((msg)=> {
    setTimeout(function(){
        msg.edit(countdownEmbed2);
    }, 1000)
});

我的代码只发送初始嵌入,不对其进行编辑。但是,如果我将 msg.edit(countdownEmbed2) 中的 CountEmbed2 更改为字符串,它将在 Discord 中编辑消息本身,而不是嵌入。有没有办法来解决这个问题?还是有更简单的方法来编辑嵌入?

【问题讨论】:

    标签: discord.js


    【解决方案1】:

    这是一个编辑示例

    const editEmbed = new Discord.MessageEmbed()
      .setDescription('this is the old description')
    message.channel.send(editEmbed).then((m) => 
    m.edit(editEmbed.setDescription('this is the new description')))
    

    让我知道这是否有效

    【讨论】:

      【解决方案2】:

      我不确定为什么,但经过测试,我得出结论,您的问题是由您编写嵌入的方式引起的。

      如果您使用MessageEmbed 构造函数(如果您使用的是discord.js v11,则为RichEmbed)它会起作用。

      这在测试时有效:

      const countdownEmbed = new MessageEmbed()
          .setDescription('test1')
      
      const countdownEmbed2 = new MessageEmbed()
          .setDescription('test2')
          .setColor('RED')
      
      message.channel.send({ embed: countdownEmbed }).then((msg) => {
          setTimeout(function () {
              msg.edit(countdownEmbed2);
          }, 1000)
      })
      

      【讨论】:

      • 这不是代码的直接问题。问题是Channel.send() 可以将字符串或 MessageEmbed(/RichEmbed) 对象作为第一个参数,因为库会将此类传递的嵌入移动到内部选项对象中。 OP 正在将嵌入创建为原始对象,而那些不能以这种方式传递,它们必须作为 send({ embed: yourEmbedObject }) 传递。
      猜你喜欢
      • 2020-09-08
      • 2021-05-08
      • 1970-01-01
      • 1970-01-01
      • 2020-11-02
      • 1970-01-01
      • 2020-09-15
      • 2020-07-18
      • 2020-04-20
      相关资源
      最近更新 更多