Home > Enterprise >  How to send a DM to someone WITH response in JDA
How to send a DM to someone WITH response in JDA

Time:10-10

I am coding a feedback feature in my Discord Bot, when someone leaves, they should be DMed a message asking why they left.

event.getUser().openPrivateChannel()
                .flatMap(channel -> channel.sendMessage("Hello, we are sorry you're leaving " event.getGuild().getName() ", if you don't mind, please tell us why you left or leave any other feedback here, it'll help us improve the server and improve experience for you if you re-join again in the future.\n\nThank you ❤."))
                .queue();

The code above is responsible for sending it, I tried to create a state machine in a private channel but it didn't work:


import bot.Main;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.MessageChannel;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;

public class Feedback extends ListenerAdapter {
    private final long channelId, authorId;

    public Feedback(MessageChannel channel, User author) {
        this.channelId = channel.getIdLong();
        this.authorId = author.getIdLong();
    }

    @Override
    public void onMessageReceived(MessageReceivedEvent event) {
        if (event.getAuthor().isBot()) return;
        if (event.getAuthor().getIdLong() != authorId) return;
        if (event.getChannel().getIdLong() != channelId) return;
        MessageChannel channel = event.getChannel();
        String content = event.getMessage().getContentRaw();
        event.getChannel().sendMessage("Thanks for your feedback!").queue();
        EmbedBuilder feedback = new EmbedBuilder();
        feedback.setTitle("Automated System Operations - Leaving Feedback");
        feedback.addField("Feedback", content, false);
        feedback.setColor(0xC90004);
        feedback.setAuthor(event.getAuthor().getAsTag() " - " event.getAuthor().getId(), "https://support.discord.com/hc/en-us/articles/209572128-How-do-I-log-out-", event.getAuthor().getAvatarUrl());
        feedback.setImage("https://media.discordapp.net/attachments/894913784823566428/896323821336948736/unknown.png?width=384&height=192");
        Main.jda.getGuildById("894913620868202506").getTextChannelById("896322509874540545").sendMessage(feedback.build()).queue();
    }
}

I got this event state-machine channel but I don't know how to addListener to it in DMs.

Any help is accepted <3

CodePudding user response:

You can add the state machine event listener with JDA#addEventListener:

event.getUser().openPrivateChannel().flatMap(channel -> {
  event.getJDA().addEventListener(new Feedback(channel, event.getUser()));
  return channel.sendMessage("hello");
}).queue();

I would recommend to remove your event listener after you received that response with event.getJDA().removeEventListener(this);

CodePudding user response:

You never have to guess how to use a library - that's what documentation is for. Any library worth its salt has documentation listing every single class, method, and the property you need to worry about.

A quick google search for "discord-jda docs" takes us to the JavaDoc: https://ci.dv8tion.net/job/JDA/javadoc/index.html

You want to send a message to a user, right? So let's use the search bar and find User. First result under Types is net.dv8tion.jda.API.entities.User. We're now at https://ci.dv8tion.net/job/JDA/javadoc/net/dv8tion/jda/api/entities/User.html

If you want to know how to do something with a user, we look at the Methods every User has. Two catch my eye right away: User.hasPrivateChannel() and User.openPrivateChannel(). We'll click the second one since it looks relevant.

Lo and behold, the docs have example usage! I'll quote it below:

// Send message without response handling
public void sendMessage(User user, String content) {
    user.openPrivateChannel()
        .flatMap(channel -> channel.sendMessage(content))
        .queue();

} This seems pretty straightforward. So the basic usage you're looking for (assuming event is a MessageReceivedEvent) is this:

event.getAuthor().openPrivateChannel().flatMap(channel -> channel.sendMessage("hello")).queue();
  • Related