为什么我的JDA不和谐机器人无法接收DM并将其打印出来

时间:2020-10-24 18:23:31

标签: java discord-jda

我正在尝试使用JDA制作我的第一个错误报告不合格的bot,并且能够将该bot告知用户DM,但是当用户不响应时,即使我使用了接收端的方法,我也没有任何异常突出显示为橙色,表示我正确输入了方法

这是我的主要方法

import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import javax.security.auth.login.LoginException;

public class Main extends ListenerAdapter{
    public static void main(String[] args) throws InterruptedException, LoginException {
        JDABuilder builder = JDABuilder.createDefault("NzY5NDAwMDA0NTExMDA2NzMx.X5Odlw.45gBjejxyTnbKaTfP2kS46RPGYw");
        builder.setActivity(Activity.playing("RoParty By Myself"));
        builder.setStatus(OnlineStatus.ONLINE);
        JDA jda = builder.build();
        jda.awaitReady();
        jda.addEventListener(new Main());
    }


    @Override
    public void onMessageReceived(MessageReceivedEvent event){
        User user = event.getAuthor();
        String rawMessage = event.getMessage().getContentRaw();

        if(rawMessage.equalsIgnoreCase("Ro") && !user.isBot()) {
            event.getChannel().sendMessage("Party!").queue();
        }else if(rawMessage.equalsIgnoreCase("hello there") && !user.isBot()){
            event.getChannel().sendMessage("General Kenobi!").queue();
        }else if(rawMessage.equalsIgnoreCase("!report") && !user.isBot() && event.getChannel().getName().equals("bug-reports")){
            event.getChannel().sendMessage("Please File Your Report In The DMs").queue();
            DMController dmController = new DMController(user, "Thanks for filling a bug report!");
        }
    }
}

这是我的DM控制器类

import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.events.message.priv.PrivateMessageReceivedEvent;
import net.dv8tion.jda.api.events.message.priv.PrivateMessageUpdateEvent;
import net.dv8tion.jda.api.exceptions.ErrorResponseException;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.jetbrains.annotations.NotNull;

public class DMController extends ListenerAdapter {

    private User user;
    private PrivateChannel channel;

    public DMController(User user, String startMessage) {
        this.user = user;
        this.channel = user.openPrivateChannel().complete();
        SendMessage(startMessage);
    }

    public boolean SendMessage(String message){
        try {
            channel.sendMessage(message).queue();
        }catch(ErrorResponseException ignored) {
            System.out.println("error");
        }

        return true;
    }

    @Override
    public void onPrivateMessageReceived(PrivateMessageReceivedEvent event) {
        System.out.println("responded");
    }
}

1 个答案:

答案 0 :(得分:0)

您创建了DMController,但从未将其添加为JDA的事件监听器。

DMController dmController = new DMController(user, "Thanks for filling a bug report!");
event.getJDA().addEventListener(dmController);

我建议您更改逻辑以使用queue()而不是complete()。可以通过使您的类使用PrivateChannel作为构造函数参数来实现:

构造函数

    public DMController(PrivateChannel channel, String startMessage) {
        this.user = channel.getUser();
        this.channel = channel;
        SendMessage(startMessage);
    }

用法

user.openPrivateChannel().queue(channel -> {
  DMController dmController = new DMController(channel, "Thanks for filling a bug report!");
  event.getJDA().addEventListener(dmController);
});

完成JDA#removeEventListener后,您可以删除监听器。