JDA事件的getter返回null,获取角色中的所有成员仅返回机器自身。

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

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

问题

以下是翻译好的部分:

@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());
    }
}

EDITevent.getMember 仅返回 null 的问题已解决。

EDIT 2:根据评论中 Moorhuhn 的建议,我解决了另一个问题。问题是机器人只缓存了语音聊天中的成员,因此无法获取具有特定角色的成员。

英文:

I am trying to create a discord bot that adds/removes roles from a guild member when they add/remove a reaction from a message, and remove all members from a specific role when a trigger happens.

Every time I do event.getmember for the onMessageReactionRemove event, I always get null for some reason. When I also try to get all the members that have a specific role and put it in a list, I only get the bot itself in the list. I also don't get any errors for when the bot tries to remove all members from a specific role. I got this bot to work a day ago, but I don't know why it doesn't work now.

On the documentation, it says that it will return null if the user isn't in the guild anymore, but when I removed a reaction, I was still in the guild.

Here is the error message when the bot tries to get the member who removed a reaction, this error is fired when the bot tries to remove a role from a member:

[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)

Here is my code:

@Override
	public void onMessageReactionRemove(@Nonnull MessageReactionRemoveEvent event) {
		System.out.println(event.getMember());
		if (trackedMessages.contains(event.getMessageId()) &amp;&amp; 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()) &amp;&amp; event.getMessage().getContentRaw().startsWith(&quot;[FINISHED]&quot;)) {
			GuildCache cache = GuildCache.getCache(event.getGuild().getId());
			List&lt;Member&gt; members = new ArrayList&lt;&gt;();
			members.addAll(event.getGuild().getMembersWithRoles(cache.getAttendingEventRole()));
			for (Member member : members) {
				event.getGuild().removeRoleFromMember(member, cache.getAttendingEventRole()).queue();
				System.out.println(&quot;removed role&quot;);
			}
			trackedMessages.remove(event.getMessageId());
		}
	}

If you need more code snippets just comment what you need.

EDIT: The problem where the event.getMember only returning null is solved.

EDIT 2: So what Moorhuhn said in the comments helped me to solve the other problem. The problem what that the bot only cached the members in a voice chat so it couldn't get the members with a specific role.

答案1

得分: 1

使用 event.getUserId() 替代。Discord 在移除反应时不提供成员信息。removeRoleFromMember 方法还接受用户 ID:removeRoleFromMember(String, Role)

英文:

Use event.getUserId() instead. Discord doesn't provide member information for reaction remove. The removeRoleFromMember method also accepts user ids: removeRoleFromMember(String, Role)

huangapple
  • 本文由 发表于 2020年9月18日 03:23:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63944941.html
匿名

发表评论

匿名网友

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

确定