JDA机器人未监听消息。

huangapple go评论74阅读模式
英文:

JDA bot is not listening to messages

问题

以下是您提供的代码的翻译部分:

import net.dv8tion.jda.api.AccountType;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.jetbrains.annotations.NotNull;

public class Main extends ListenerAdapter {
    public static void main(String[] args) throws Exception{
        JDABuilder bot = new JDABuilder(AccountType.BOT);
        String token = "token";
        bot.setToken(token);
        bot.build();
    }

    @Override
    public void onMessageReceived(@NotNull MessageReceivedEvent event) {
        System.out.println("message received");
        event.getChannel().sendMessage("reeeeeeee").queue();
        super.onMessageReceived(event);
    }
}

如果您认为问题可能出在 "public void onMessageReceived" 部分,您可以尝试使用上述翻译后的代码。

英文:

I'm trying to make a very simple discord bot and it is my first time making one in java (with the IntelliJ IDE). It logs in and goes online correctly, but won't receive any messages that I send in the guild. The code is below:

import net.dv8tion.jda.api.AccountType;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.jetbrains.annotations.NotNull;

public class Main extends ListenerAdapter {
    public static void main(String[] args) throws Exception{
        JDABuilder bot = new JDABuilder(AccountType.BOT);
        String token = "token";
        bot.setToken(token);
        bot.build();
    }

    @Override
    public void onMessageReceived(@NotNull MessageReceivedEvent event) {
        System.out.println("message received");
        event.getChannel().sendMessage("reeeeeeee");
        super.onMessageReceived(event);
    }
}

I think that the flawed part is somewhere around "public void onMessageReceived". I have tried many things such as rearranging my code or rewriting it, but nothing seems to work.

答案1

得分: 2

你没有在sendMessage返回的MessageAction上调用queue()

> 在使用 X 时没有任何反应
>
> 在 JDA 中,我们通过使用通用的 RestAction 类来处理异步速率限制。
> 当你的代码中有诸如 channel.sendMessage("hello");message.delete(); 这样的代码时,实际上什么都不会发生。这是因为 sendMessage(...)delete() 都返回一个 RestAction 实例。你在这里的工作还没有完成,因为该类只是执行你的请求的中间步骤。在这里,你可以选择使用异步的 queue()(推荐),或者 submit(),或者阻塞的 complete()(不推荐)。
>
> 你可能会注意到 queue() 返回的是 void。这是因为它是异步的,并使用回调。阅读更多

来自 JDA 的疑难解答 Wiki

你还没有注册你的事件监听器。而且你正在使用 JDABuilder 的已弃用构造函数。

public class Main extends ListenerAdapter {
    public static void main(String[] args) throws Exception{
        JDABuilder.createDefault(token) // 不要使用已弃用的构造函数
                  .addEventListeners(new Main()) // 注册你的监听器
                  .build();
    }

    @Override
    public void onMessageReceived(@NotNull MessageReceivedEvent event) {
        System.out.println("message received");
        event.getChannel().sendMessage("reeeeeeee").queue(); // 调用 queue
    }
}

而且你绝对不应该在任何地方泄露你的机器人令牌!

英文:

You didn't call queue() on the MessageAction returned by sendMessage.

> Nothing happens when using X
>
> In JDA we make use of async rate-limit handling through the use of the common RestAction class.
> When you have code such as channel.sendMessage("hello"); or message.delete(); nothing actually happens. This is because both sendMessage(...) as well as delete() return a RestAction instance. You are not done here since that class is only an intermediate step to executing your request. Here you can decide to use async queue() (recommended) or submit() or the blocking complete() (not recommended).
>
> You might notice that queue() returns void. This is because it's async and uses callbacks instead. Read More

From the JDA Troubleshooting Wiki

You also never registered your event listener. And you're using the deprecated constructor for JDABuilder.

public class Main extends ListenerAdapter {
    public static void main(String[] args) throws Exception{
        JDABuilder.createDefault(token) // don't use the deprecated constructor
                  .addEventListeners(new Main()) // register your listener
                  .build();
    }

    @Override
    public void onMessageReceived(@NotNull MessageReceivedEvent event) {
        System.out.println("message received");
        event.getChannel().sendMessage("reeeeeeee").queue(); // call queue
    }
}

And you should never leak your bot token anywhere!

huangapple
  • 本文由 发表于 2020年9月8日 20:17:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63793761.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定