如何使用Discord.js编写机器人编辑嵌入式消息?

3

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

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中的消息本身,但不会编辑嵌入式消息。有没有办法解决这个问题?或者有没有更简单的方法来编辑嵌入式消息?
2个回答

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

1
这并不是代码的直接问题。问题在于Channel.send()可以将字符串或MessageEmbed(/RichEmbed)对象作为第一个参数,因为库会将传递的嵌入式对象内部移动到选项对象中。OP正在创建原始对象作为嵌入式对象,这些对象不能以这种方式传递,它们必须作为send({ embed: yourEmbedObject })传递。 - tipakA

0

这是一个编辑的示例

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

请告诉我这是否起作用


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接