PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, July 11, 2022

[FIXED] How to make a bot edit its own message on Discord

 July 11, 2022     discord, discord.js, edit, javascript, message     No comments   

Issue

My friend wrote this amazing code for me but it doesn't seem to work. It's meant to send a message on a command then edit the message over and over again. But when I run the code my terminal says

DiscordAPIError: Cannot edit a message authored by another user method: 'patch', path: '/channels/808300406073065483/messages/811398346853318668', code: 50005, httpStatus: 403

Is there a way to fix this problem?

client.on('message', userMessage => 
{
    if (userMessage.content === 'hi') 
    {
        botMessage = userMessage.channel.send('hi there')
        botMessage.edit("hello");
        botMessage.edit("what up");
        botMessage.edit("sup");
        botMessage.react(":clap:")
    }
});

Solution

The Channel#send() method returns a promise, meaning you must wait for the action to finish before being able to define it. This can be done using either .then() or async and await. From personal preference, I regularly use the second option, although I have laid both options out for you.

Final Code

client.on('message', async userMessage => {
  if (userMessage.content === 'hi') 
    {
        /*
          botMessage = await userMessage.channel.send('hi there')
        */
        userMessage.channel.send('hi there').then(botMessage => {
          await botMessage.edit("hello");
          await botMessage.edit("what up");
          botMessage.edit("sup");
          botMessage.react(":clap:")
        })
    }
});


Answered By - Itamar S
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing