FruthyzGang
FruthyzGang

Reputation: 63

JDA - Event if new user joined to Guild

I have a problem, I am trying to code a bot using Java Discord API (JDA). When a new user joins a server, the bot shall send a message, but my code is not working. Code:

    public class UserJoinModule extends ListenerAdapter {
        public void onGuildMemberJoined(GuildMemberJoinEvent event) throws LoginException {
            String user = event.getMember().getAsMention();
            JDA client = new JDABuilder("awesome token").build();
            final List<TextChannel> channels = client.getTextChannelsByName("awesome channel name", true);
            for (final TextChannel ch : channels) {
                ch.sendMessage("New member joined: " + user).queue();
            }
        }

    }

Can someone tell me what is wrong?

Upvotes: 0

Views: 7815

Answers (4)

Abdullah Ahmed
Abdullah Ahmed

Reputation: 294

In your Main.java or whatever the file is, there is a variable of type JDABuilder, on it's same line of code, there is your token, a .build() at the end etc...

Insert this code into that line:

.enableIntents(GatewayIntent.GUILD_MEMBERS)

So it looks like this:

jda = JDABuilder.createDefault("TOKEN").enableIntents(GatewayIntent.GUILD_MEMBERS).build();

For it to work, go to your Discord Developer Portal, click your bot, from the menu on the left, click Bot, then scroll down and enable:

  • Server Members Intent

There are still noticeable errors like registering a new client on every message and other issues, fix them, then start your bot and it shall work.

Upvotes: 2

Sepehr GH
Sepehr GH

Reputation: 1387

For me the issue was not from the listener and method I override. I believe you have to add GatewayIntent.GUILD_MEMBERS to your JDABuilder.

builder.enableIntents(GatewayIntent.GUILD_MEMBERS);

This fixed the same issue for me.

Upvotes: 2

solonovamax
solonovamax

Reputation: 138

You have 2 problems in your code.

  1. You are creating a new JDA client every time a member joins.
  2. You are sending messages to every channel with that name, in every guild. Not just the guild that the user joined.

Here is what you want to do:

public class UserJoinModule extends ListenerAdapter {
    @Override
    public void onGuildMemberJoin(GuildMemberJoinEvent event) {
        Guild guild = event.getGuild(); // Get the guild that the user joined.
        User user = event.getUser();    // Get the user that joined.
        JDA client = event.getJDA();    // Get the already existing JDA instance.

        List<TextChannel> channels = guild.getTextChannelsByName("awesome channel name", true); // Get the list of channels in the guild that matches that name.

        for (TextChannel channel : channels) { // Loops through the channels and sends a message to each one.
            channel.sendMessage("New member joined: " + user).queue();
        }
    }
}

Upvotes: 1

Minn
Minn

Reputation: 6134

Your code should look like this:

public class UserJoinModule extends ListenerAdapter {
    @Override // USE THIS WHEN YOU WANT TO OVERRIDE A METHOD
    public void onGuildMemberJoin(GuildMemberJoinEvent event) {
        String user = event.getMember().getAsMention();
        JDA client = event.getJDA(); // DO NOT CREATE A NEW JDA INSTANCE EVERY TIME
        List<TextChannel> channels = client.getTextChannelsByName("awesome channel name", true);
        for (TextChannel ch : channels) {
            ch.sendMessage("New member joined: " + user).queue();
        }
    }
}

And you must register this listeners in your JDABuilder instance, preferably you only have one of these in your entire codebase. See addEventListeners.

Upvotes: 1

Related Questions