JDA. GuildMemberJoin/LeaveEvent

大城市里の小女人 提交于 2021-02-05 09:26:11

问题


I have written the following methods and they both are not working. Anyone know why and how to fix it?

PS: The bot has admin perms.

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

Default channel settings


回答1:


To quote the 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.

For clarification:

You can enable the GUILD_MEMBERS intent by doing builder.enableIntents(GatewayIntent.GUILD_MEMBERS) on JDABuilder.

For example:

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


来源:https://stackoverflow.com/questions/62267453/jda-guildmemberjoin-leaveevent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!