Fetch a message (by ID) and edit it

浪子不回头ぞ 提交于 2021-02-10 14:16:28

问题


I'm trying to edit a message the bot sent, in a different function.

const msg = message.channel.fetchMessage(msgId);
msg.edit(embed);

Didn't work because msg.edit is not a function.

message.channel.messages.fetch({around: "352292052538753025", limit: 1})
  .then(messages => {
    messages.first().edit("test");
  });

Didn't work because .fetch is not a function.

function update(msgId, time, channelid, prize, winnersInt, message) {

    setTimeout(function(){ 

        let gtime = time/3600000 + " hours remaining!";
        if(time < 3600000) {
            gtime = time/60000 + " minuets remaining!";
        }

        console.log(gtime + "p: " + prize);

        let embed = new Discord.RichEmbed()
            .setColor("#7289da")
                        .setTitle("Giveaway!")
            .addField('Prize: ', prize)
            .addField('Amount of winners: ', winnersInt)
                       .addField('Time: ', gtime)
        const msg = message.channel.fetchMessage(msgId);
        msg.edit(embed);

        time - 60000;

        if(time > 0) {
                 update(msgId, time, channel, prize, winnersInt, message);
        }

    }, 60000);


}

I expect the message to be edited.


回答1:


Got it working.

Used this:

message.channel.fetchMessages({around: msgId, limit: 1})
    .then(msg => {
        const fetchedMsg = msg.first();
        fetchedMsg.edit(embed);
    });


来源:https://stackoverflow.com/questions/55773999/fetch-a-message-by-id-and-edit-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!