Silly Sock
Silly Sock

Reputation: 3

Discord JDA Bot sending messages infinitely

I'm trying to send messages using the Discord JDA API, however whenever I send one, it sends it infinitely.

JDA Version: 4.2.1_255

What I've Tried:

Research the issue Use GuildMessageReceivedEvent instead of MessageReceivedEvent

Pseudo-Code:

Guild server = e.getGuild();
    Role role = server.getRolesByName("Java", false).get(0);
    System.out.println(role);
    for(Member members : server.getMembers()) {
        if(members.getRoles().contains(role)) {
            sendPrivateMessage(members.getUser(), "Hello <@!" + members.getId() + ">"); // Sends infinitely
        }
    }

sendPrivateMessage():

public void sendPrivateMessage(User user, String content) {
    user.openPrivateChannel()
            .flatMap(channel -> channel.sendMessage(content))
            .queue();
}

Upvotes: 0

Views: 1269

Answers (2)

Thebotgame
Thebotgame

Reputation: 91

You can try this

public void sendPrivateMessage(User user, String content) {
    if (user.isBot()) return;

    user.openPrivateChannel()
        .flatMap(channel -> channel.sendMessage(content))
        .queue();
}

Upvotes: 0

Dude2345
Dude2345

Reputation: 67

if this code gets executed in the MessageReceivedEvent you may try adding

if(event.getAuthor().isBot()){
return;
}

so that it wont reply itself if you haven't already. (If you want it to answer to other bots, you can also use the JDA, to get the selfUser and check if this event got executed by itself) If thats not the case maybe you could tell us more about when it gets executed.

Upvotes: 0

Related Questions