Gabor Szita
Gabor Szita

Reputation: 329

JDA - How to get the user ID of the bot itself

I'm making a JDA (Java Discord API) program that needs to check if a message was sent by the bot itself. How can I achieve this? I thought about checking if the user ID of the message sender equals the bot's user ID, but how can I get the user ID of the bot itself in the program?

Upvotes: 1

Views: 4536

Answers (1)

Nightloewe
Nightloewe

Reputation: 1098

If you use the MessageReceivedEvent, you can just check if the sender is a bot by using: event.getAuthor.isBot().

You can access the user of the bot itself by accessing JDA and calling getSelfUser() as follows with the MessageReceivedEvent in mind: event.getJDA().getSelfUser()

On the SelfUser you can call SelfUser#getId() or SelfUser#getIdLong() to access the ID.

Example Code

public class Listener extends ListenerAdapter {
  @Override
  public void onMessageReceived(MessageReceivedEvent event) {
    boolean isBot = event.getAuthor().isBot() //Check if the Message Sender is a bot
    
    long id = event.getJDA().getSelfUser().getIdLong() //the bot ID
  }
}

Upvotes: 1

Related Questions