Medraj
Medraj

Reputation: 173

Discord.js How can I edit previous bot's message?

I am trying to make a ROSTER command. The command is $roster (add|remove) @user RANK. This command basically should edit a previous bot's message (roster) and add a user to the roster to the RANK in the command... This is my code so far, but I haven't managed to make the roster message and the editing part of it and the RANK system. If someone could help that would be very amazing!

// ROOSTER COMMAND
client.on('message', async message => {
    if (message.content.startsWith(prefix + "roster")) {
        if (!message.member.hasPermission('ADMINISTRATOR')) return message.channel.send('You do not have that permission! :x:').then(message.react(':x:'))

        const args = message.content.slice(prefix.length + 7).split(/ +/)
        let uReply = args[0];
        const user = message.mentions.members.first()

        if(!uReply) message.channel.send("Please use `add` or `remove`.")
        if(uReply === 'add')  {
            if(!user) return message.channel.send("Please make sure to provide which user you would like to add...")
            message.channel.send(`you are adding **${user.displayName}** from the roster.`)
           
        } else if(uReply === 'remove') {
            if(!user) return message.channel.send("Please make sure to provide which user you would like to add...")
            message.channel.send(`you are removing **${user.displayName}** from the roster.`)
        }
        
    }})

Upvotes: 1

Views: 28698

Answers (2)

Venk
Venk

Reputation: 230

Sounds like the .edit() method is what you want.

Example from the docs:

// Update the content of a message
message.edit('This is my new content!')
  .then(msg => console.log(`Updated the content of a message to ${msg.content}`))
  .catch(console.error);

Upvotes: 6

Ashwin Bhargava
Ashwin Bhargava

Reputation: 59

To edit your bots previous message, you need to have a reference of the message you want to edit. The returned reference is a promise so do not forget to use await keyword. you can then use .edit() function on the reference to update you msg.

        const msgRef = await msg.channel.send("Hello");
        msgRef.edit("Bye");

Upvotes: 4

Related Questions