java - JDA。公会成员加入/离开事件

标签 java bots discord-jda

我编写了以下方法,但它们都不起作用。有人知道为什么以及如何解决它吗?

PS:该机器人具有管理员权限。

public class GuildMemberJoin extends ListenerAdapter {
    public void onGuildMemberJoin(GuildMemberJoinEvent event) {
        EmbedBuilder join = new EmbedBuilder();

        join.setColor(Color.getHSBColor(227, 74, 64));
        join.setTitle("SERVER UPDATE");
        join.setDescription(event.getMember().getAsMention() + " has now joined The server!");
        event.getGuild().getDefaultChannel().sendMessage(join.build()).queue();
    }
public class GuildMemberLeave extends ListenerAdapter {
    public void onGuildMemberLeave(GuildMemberLeaveEvent event) {
        EmbedBuilder join = new EmbedBuilder();
        TextChannel spamChannel = event.getGuild().getTextChannelById("713429117546135572");

        join.setColor(Color.getHSBColor(227, 74, 64));
        join.setTitle("SERVER UPDATE");
        join.setDescription(event.getMember().getAsMention() + " has now left the server!");
        spamChannel.sendMessage(join.build()).queue();
    }

默认 channel 设置

channel settings

最佳答案

引用JDA Wiki :

There are many reasons why your event listener might not be executed but here are the most common issues:

  1. You are using the wrong login token?
    If the token is for another bot which doesn't have access to the desired guilds then the event listener code cannot run.
  2. Your bot is not actually in the guild?
    Make sure your bot is online and has access to the resource you are trying to interact with.
  3. You never registered your listener?
    Use jda.addEventListener(new MyListener()) on either the JDABuilder or JDA instance
  4. You did not override the correct method?
    Use @Override and see if it fails. Your method has to use the correct name and parameter list defined in ListenerAdapter.
  5. You don't actually extend EventListener or ListenerAdapter.
    Your class should either use extends ListenerAdapter or implements EventListener.
  6. You are missing a required GatewayIntent for this event.
    Make sure that you enableIntents(...) on the JDABuilder to allow the events to be received.
  7. The event has other requirements that might not be satisfied such as the cache not being enabled.
    Please check the requirements on the event documentation.

If none of the above apply to you then you might have an issue in your listener's code, at that point you should use a debugger.

澄清:

您可以通过在 JDABuilder 上执行 builder.enableIntents(GatewayIntent.GUILD_MEMBERS) 来启用 GUILD_MEMBERS 意图。

例如:

JDABuilder builder = JDABuilder.createDefault(token);
builder.enableIntents(GatewayIntent.GUILD_MEMBERS);
builder.addEventListeners(myListener);
JDA jda = builder.build();

另请参阅:Gateway Intents and Member Cache Policy

关于java - JDA。公会成员加入/离开事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62267453/

相关文章:

java - 尝试使用 String.split ("\\?"时出现意外行为)

java - 棋盘坐标

Java - 如何检查字符串中下一个即将出现的空格? (对于 IRC 机器人)

javascript - 如何阻止机器人抓取我基于 AJAX 的 URL?

javascript - 如何使用 BotKit 在 Slack 上发送带有随机@提及的消息?

java - JDA - 如何将 "Member"对象转换为 "User"对象,而不是相反?

java - Discord bot - Js 事件监听器发送多条消息

使用 AES 加密的 Java GUI 程序

gradle - 随着JDA 4.2.0的新更新,VPS上新建的JAR文件返回NoClassDefFoundError

Java Swing : Jtable ArrayIndexOutOfBoundsException