Home > Enterprise >  Discord JDA "Kick" command is not working
Discord JDA "Kick" command is not working

Time:06-13

I've been sitting here since 3 days now. Trying to figure out what's wrong with my code. I have tried multiple solutions and tried to research my problem on multiple platforms. Nothing helped.

I always get a long error message in the console:

[JDA MainWS-ReadThread] ERROR JDA - One of the EventListeners had an uncaught exception
java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
    at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:100)
    at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:106)
    at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:302)
    at java.base/java.util.Objects.checkIndex(Objects.java:359)
    at java.base/java.util.ArrayList.get(ArrayList.java:427)
    at java.base/java.util.Collections$UnmodifiableList.get(Collections.java:1347)
    at de.ayahuascasuppe.commands.Kick.onMessageReceived(Kick.java:31)
    at net.dv8tion.jda.api.hooks.ListenerAdapter.onEvent(ListenerAdapter.java:358)
    at net.dv8tion.jda.api.hooks.InterfacedEventManager.handle(InterfacedEventManager.java:96)
    at net.dv8tion.jda.internal.hooks.EventManagerProxy.handleInternally(EventManagerProxy.java:88)
    at net.dv8tion.jda.internal.hooks.EventManagerProxy.handle(EventManagerProxy.java:70)
    at net.dv8tion.jda.internal.JDAImpl.handleEvent(JDAImpl.java:163)
    at net.dv8tion.jda.internal.handle.MessageCreateHandler.handleInternally(MessageCreateHandler.java:111)
    at net.dv8tion.jda.internal.handle.SocketHandler.handle(SocketHandler.java:36)
    at net.dv8tion.jda.internal.requests.WebSocketClient.onDispatch(WebSocketClient.java:953)
    at net.dv8tion.jda.internal.requests.WebSocketClient.onEvent(WebSocketClient.java:840)
    at net.dv8tion.jda.internal.requests.WebSocketClient.handleEvent(WebSocketClient.java:818)
    at net.dv8tion.jda.internal.requests.WebSocketClient.onBinaryMessage(WebSocketClient.java:992)
    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)
[JDA MainWS-ReadThread] ERROR JDA - One of the EventListeners had an uncaught exception
java.lang.IllegalStateException: This message event did not happen in a guild
    at net.dv8tion.jda.api.events.message.GenericMessageEvent.getGuild(GenericMessageEvent.java:154)
    at de.ayahuascasuppe.commands.Kick.onMessageReceived(Kick.java:30)
    at net.dv8tion.jda.api.hooks.ListenerAdapter.onEvent(ListenerAdapter.java:358)
    at net.dv8tion.jda.api.hooks.InterfacedEventManager.handle(InterfacedEventManager.java:96)
    at net.dv8tion.jda.internal.hooks.EventManagerProxy.handleInternally(EventManagerProxy.java:88)
    at net.dv8tion.jda.internal.hooks.EventManagerProxy.handle(EventManagerProxy.java:70)
    at net.dv8tion.jda.internal.JDAImpl.handleEvent(JDAImpl.java:163)
    at net.dv8tion.jda.internal.handle.MessageCreateHandler.handleInternally(MessageCreateHandler.java:111)
    at net.dv8tion.jda.internal.handle.SocketHandler.handle(SocketHandler.java:36)
    at net.dv8tion.jda.internal.requests.WebSocketClient.onDispatch(WebSocketClient.java:953)
    at net.dv8tion.jda.internal.requests.WebSocketClient.onEvent(WebSocketClient.java:840)
    at net.dv8tion.jda.internal.requests.WebSocketClient.handleEvent(WebSocketClient.java:818)
    at net.dv8tion.jda.internal.requests.WebSocketClient.onBinaryMessage(WebSocketClient.java:992)
    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)

So I tried creating a new class. with exact the same code I had, and at the beginning it worked, especially these lines of code worked, which didn't work prior:

            if (!author.hasPermission(Permission.KICK_MEMBERS)) {
            channel.sendMessage("**ERROR**: You don't have permission to kick people!").queue();
            return;
        }
        if (event.getMessage().getMentionedMembers().isEmpty()) {
            channel.sendMessage("**ERROR**: You must mention who you want to be kicked").queue();
            return;
        }
        if (args.length < 2) {
            channel.sendMessage("**ERROR**: Correct usage: `"   Main.prefix   "kick <@User> <reason>").queue();
            return;
        }

Now I added a few things here and there, and then It just stopped working again. I deleted everything I added to the code to the point where the code was working, and it still didn't worked.

at de.ayahuascasuppe.commands.Kick.onMessageReceived(Kick.java:31)

refers to:

mentioned = event.getMessage().getMentionedMembers().get(0);

Here my full code:

Prefix = "u!"

logChannel = "984832326947197008"

package de.ayahuascasuppe.commands;

import java.awt.Color;
import java.time.OffsetDateTime;
import java.util.Random;

import de.ayahuascasupe.de.main.Main;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.MessageChannel;
import net.dv8tion.jda.api.entities.PrivateChannel;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;

public class Kick extends ListenerAdapter {
protected Member mentioned;
Random randNum = new Random();

public void onMessageReceived(MessageReceivedEvent event) {

    String[] args = event.getMessage().getContentStripped().split(" ");
    String authorName = event.getAuthor().getName();
    String authorAvatar = event.getAuthor().getAvatarUrl();
    String message = event.getMessage().getContentStripped();
    MessageChannel channel = event.getChannel();
    Member author = event.getMessage().getMember();
    Guild guild = event.getGuild();
    mentioned = event.getMessage().getMentions().getMembers().get(0);
    
    // add code here:
    if (message.startsWith(Main.prefix   "kick")) {
        if (!author.hasPermission(Permission.KICK_MEMBERS)) {
            channel.sendMessage("**ERROR**: You don't have permission to kick people!").queue();
            return;
        }
        if (event.getMessage().getMentions().getMembers().isEmpty()) {
            channel.sendMessage("**ERROR**: You must mention who you want to be kicked").queue();
            return;
        }
        if (args.length < 2) {
            channel.sendMessage("**ERROR**: Correct usage: `"   Main.prefix   "kick <@User> <reason>").queue();
            return;
        }

        // event.getGuild().kick(event.getMentionedMembers().get(0).getId(),
        // reason).queue();
        channel.sendMessage(
                "**SUCCESS**: You have kicked "   mentioned.getAsMention()   " from the Discord server.").queue();
        EmbedBuilder embed = new EmbedBuilder();
        embed.setTitle(event.getAuthor().getName()   " kicked "   mentioned.getEffectiveName(), null);
        embed.setDescription("**Action**: Kick\n\nUser "   mentioned.getAsMention()   " got kicked by "
                  event.getAuthor().getAsMention()   " from the Discord server.\n\nReason: "   "reason");
        embed.setThumbnail(event.getGuild().getIconUrl());
        embed.setAuthor(authorName, null, authorAvatar);
        embed.setColor(Color.RED);
        embed.setFooter(Main.embedFooter);
        embed.setTimestamp(OffsetDateTime.now());
        event.getGuild().getTextChannelById(Main.logChannel).sendMessageEmbeds(embed.build()).queue();
        embed.clear();

        PrivateChannel dm = mentioned.getUser().openPrivateChannel().complete();

        EmbedBuilder embedDM = new EmbedBuilder();
        embedDM.setTitle("You got kicked from the Discord server.", null);
        embedDM.setDescription("Hello buddy. It seems like you got kicked from the "   guild.getName()
                  " Discord.\nDon't worry. You are not banned. You can use the invite link to join the Discord again.\n\nReason for the kick: "
                  "reason");
        embedDM.setThumbnail(event.getGuild().getIconUrl());
        embedDM.setColor(Color.RED);
        embedDM.setFooter(Main.embedFooter);
        embedDM.setTimestamp(OffsetDateTime.now());
        dm.sendMessageEmbeds(embedDM.build()).queue();
        embedDM.clear();
    }
    
}

}

Information: JavaSE-17

JDA Version: 5.0.0-alpha.4

CodePudding user response:

The problem not with kick command, the problem is that in your event there's no any mentioned members in the message.

mentioned = event.getMessage().getMentionedMembers().get(0);

Try to use a debugger to see what the event you receive from JDA. Probably, you receive another kind of event (not the new message one), or you don't mention anybody, like it doing by in Discord - @user.

Also, do not share your access tokens on public resources, like Stackoverflow, even if it was revoked :)

CodePudding user response:

Reading the exception tells you the problem

java.lang.IllegalStateException: This message event did not happen in a guild

This means you got a message event for a direct message, which means you cannot get member mentions from it.

  • Related