最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Fetch a message (by ID) and edit it - Stack Overflow

matteradmin3PV0评论

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.

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.

Share asked Apr 20, 2019 at 13:28 DerockDerock 3232 gold badges5 silver badges10 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Old publication but may help those currently looking for it.

For V.13 it can be used this way:

<#Channel>.messages.fetch('messageID').then(msg => msg.edit('newMessage'))

I tested it that way and it worked perfectly.

Got it working.

Used this:

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

comment list (0)

  1. No comments so far