Search code examples
javajavadocdiscorddiscord-jda

Discord-JDA: How to get data about the user who joined the server?


How can I assign the User who has logged on to the server for the variable User newUser?

Part of my program:

Main.java:

public static void main(String[] args) throws Exception{
    try {
        JDA api = new JDABuilder(AccountType.BOT).setToken(token).build();
        api.addEventListener(new MyEventListener(api));

    } catch (Exception e) {
        e.printStackTrace();
    }
}

MyEventListener.java:

public void onMessageReceived(MessageReceivedEvent event) {
    User newUser;
}

I read Javadoc and maybe I need to use GuildMemberJoinEvent. If this is what I need, then please help by giving a sample code how to use this function correctly for my program.


Solution

  • private static User newUser;
    
    @Override
    public void onGuildMemberJoin(GuildMemberJoinEvent event) {
    
        newUser = event.getUser();
    
    }
    
    @Override
    public void onMessageReceived(MessageReceivedEvent event) {
    
        System.out.println(newUser.getName()); //...your code
    
    }