JDA 事件获取器返回 null 并获取角色中的所有成员,仅返回机器人本身

JDA Event getters returning null & getting all members in a role returning only the bot itself

我正在尝试创建一个 discord 机器人,当公会成员 add/remove 对消息做出反应时,该机器人 adds/removes 扮演角色,并在触发发生时从特定角色中删除所有成员。

每次我为 onMessageReactionRemove 事件做 event.getmember 时,由于某种原因我总是得到 null。当我还尝试获取具有特定角色的所有成员并将其放入列表时,我只会将机器人本身放入列表中。当机器人试图从特定角色中删除所有成员时,我也没有收到任何错误。一天前我让这个机器人工作,但我不知道为什么它现在不工作了。

在文档中,它说如果用户不再在公会中,它将 return null,但是当我删除一个反应时,我仍然在公会中。

这是当机器人试图获取删除反应的成员时的错误消息,当机器人试图从成员中删除角色时会触发此错误:

[JDA MainWS-ReadThread] ERROR JDA - One of the EventListeners had an uncaught exception
java.lang.IllegalArgumentException: Member may not be null
    at net.dv8tion.jda.internal.utils.Checks.notNull(Checks.java:64)
    at net.dv8tion.jda.internal.entities.GuildImpl.removeRoleFromMember(GuildImpl.java:1209)
    at me.david.tskmanager.eventlisteners.EventChannelsEventListener.onMessageReactionRemove(EventChannelsEventListener.java:47)
    at net.dv8tion.jda.api.hooks.ListenerAdapter.onEvent(ListenerAdapter.java:440)
    at net.dv8tion.jda.api.hooks.InterfacedEventManager.handle(InterfacedEventManager.java:96)
    at net.dv8tion.jda.internal.hooks.EventManagerProxy.handleInternally(EventManagerProxy.java:82)
    at net.dv8tion.jda.internal.hooks.EventManagerProxy.handle(EventManagerProxy.java:69)
    at net.dv8tion.jda.internal.JDAImpl.handleEvent(JDAImpl.java:150)
    at net.dv8tion.jda.internal.handle.MessageReactionHandler.onRemove(MessageReactionHandler.java:200)
    at net.dv8tion.jda.internal.handle.MessageReactionHandler.handleInternally(MessageReactionHandler.java:146)
    at net.dv8tion.jda.internal.handle.SocketHandler.handle(SocketHandler.java:36)
    at net.dv8tion.jda.internal.requests.WebSocketClient.onDispatch(WebSocketClient.java:947)
    at net.dv8tion.jda.internal.requests.WebSocketClient.onEvent(WebSocketClient.java:834)
    at net.dv8tion.jda.internal.requests.WebSocketClient.handleEvent(WebSocketClient.java:812)
    at net.dv8tion.jda.internal.requests.WebSocketClient.onBinaryMessage(WebSocketClient.java:985)
    at com.neovisionaries.ws.client.ListenerManager.callOnBinaryMessage(ListenerManager.java:385)
    at com.neovisionaries.ws.client.ReadingThread.callOnBinaryMessage(ReadingThread.java:276)
    at com.neovisionaries.ws.client.ReadingThread.handleBinaryFrame(ReadingThread.java:996)
    at com.neovisionaries.ws.client.ReadingThread.handleFrame(ReadingThread.java:755)
    at com.neovisionaries.ws.client.ReadingThread.main(ReadingThread.java:108)
    at com.neovisionaries.ws.client.ReadingThread.runMain(ReadingThread.java:64)
    at com.neovisionaries.ws.client.WebSocketThread.run(WebSocketThread.java:45)

这是我的代码:

@Override
    public void onMessageReactionRemove(@Nonnull MessageReactionRemoveEvent event) {
        System.out.println(event.getMember());
        if (trackedMessages.contains(event.getMessageId()) && event.getReactionEmote().getEmote().equals(event.getGuild().getEmoteById(emoteID))) {
            GuildCache cache = GuildCache.getCache(event.getGuild().getId());
            System.out.println(cache.getAttendingEventRole());
            event.getGuild().removeRoleFromMember(event.getMember(), cache.getAttendingEventRole()).queue();
        }
    }

    @Override
    public void onMessageUpdate(@Nonnull MessageUpdateEvent event) {
        if (trackedMessages.contains(event.getMessageId()) && event.getMessage().getContentRaw().startsWith("[FINISHED]")) {
            GuildCache cache = GuildCache.getCache(event.getGuild().getId());
            List<Member> members = new ArrayList<>();
            members.addAll(event.getGuild().getMembersWithRoles(cache.getAttendingEventRole()));
            for (Member member : members) {
                event.getGuild().removeRoleFromMember(member, cache.getAttendingEventRole()).queue();
                System.out.println("removed role");
            }
            trackedMessages.remove(event.getMessageId());
        }
    }

如果您需要更多代码片段,请评论您需要的内容。

编辑:解决了 event.getMember 只有 returning null 的问题。

编辑 2:所以 Moorhuhn 在评论中所说的帮助我解决了另一个问题。问题是机器人只缓存了语音聊天中的成员,所以它无法获得具有特定角色的成员。

改用event.getUserId()。 Discord 不提供用于反应删除的成员信息。 removeRoleFromMember 方法也接受用户 ID:removeRoleFromMember(String, Role)