Search code examples
discord-jda

Response to someone which send his first DM to you in JDA


Could I response to someone who send his first DM to me in JDA?


Solution

  • This question will depend on what you need, you can use the

    public void onPrivateMessageReceived(@Nonnull PrivateMessageReceivedEvent event) {
    //code
    }
    

    method to get the private channel for DM's.

    After that, you could check the history with the user. This would be example code, so you need to edit it to your needs:

        @Override
        public void onPrivateMessageReceived(@Nonnull PrivateMessageReceivedEvent event) {
            if (event.getUser().isBot()) return; // Don't do anything if the user is bot (aka, self)
            if (event.getChannel().getHistory().size() < 2) { //Made it <2 in case array starts with 0
                event.getChannel().sendMessage("HI THIS IS THE MESSAGE I WANT TO SEND").queue();
            }
        }