Search code examples
javadiscorddiscord-jda

JDA Mentioned member - not working command


I am developing my bot for discord using Java and JDA API. Before that I asked a similar question, but I ran into another problem.

From this line the problems started :

final Member MentionedMem = event.getMessage().getMentionedMembers().get(0);

Thanks to https://stackoverflow.com/users/10630900/minn for answering the previous question in which he explained to me that this line is causing the error :

java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0

For some reason or other it raises an error.

I was trying to find an answer to why this command does not work. The only thing I know is that this command returns an empty array args. Because of this, I cannot finish the bot, that is, I cannot check whether a member can kick others and can't make the main block of code kick members.

How can I fix this error and / or write the rest of the code? Sorry for my English and many thanks to you.

Some code :

public class KickComm extends ListenerAdapter {

    public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
        String[] message = event
                .getMessage()
                .getContentRaw()
                .split(" ");

        // final Member target = event.getMessage().getMentionedMembers().get(0); ERROR CUZ I DONT NOW WHY
        
        final Member SelfMember = event
                .getGuild()
                .getSelfMember();

        if(message[0].equalsIgnoreCase(Main.prefix + "kick"))
        {                                                                                                   
            if (message.length < 2) {

                EmbedBuilder NoUser = new EmbedBuilder();
                NoUser.setColor(0xff3923);
                NoUser.setTitle("\uD83D\uDD34You need to add a <@username> and <reason>");
                NoUser.setFooter("Usage: " + Main.prefix + "kick <@username> <reason>.",
                        Objects
                                .requireNonNull(event.getMember())
                                .getUser()
                                .getAvatarUrl());

                event
                        .getChannel()
                        .sendMessage(NoUser.build())
                        .queue();

                NoUser.clear();

            } else if (message.length < 3) {                                                                

                EmbedBuilder NoReason = new EmbedBuilder();
                NoReason.setColor(0xff3923);
                NoReason.setTitle("\uD83D\uDD34You need to add a <reason>.");
                NoReason.setFooter("Usage: " + Main.prefix + "kick <@username> <reason>.",
                        Objects
                                .requireNonNull(event.getMember())
                                .getUser()
                                .getAvatarUrl());

                event
                        .getChannel()
                        .sendMessage(NoReason.build())
                        .queue();

                NoReason.clear();

            } else if(!SelfMember.hasPermission(Permission.KICK_MEMBERS)) {

                EmbedBuilder NoPermission = new EmbedBuilder();
                NoPermission.setColor(0xff3923);
                NoPermission.setTitle("\uD83D\uDD34You don't have permission to use this command.");
                NoPermission.setFooter("Usage: " + Main.prefix + "kick <@username> <reason>.",
                        Objects
                                .requireNonNull(event.getMember())
                                .getUser()
                                .getAvatarUrl());

                event
                        .getChannel()
                        .sendMessage(NoPermission.build())
                        .queue();

                NoPermission.clear();

            } else if(!Objects.requireNonNull(event.getMember()).hasPermission(Permission.KICK_MEMBERS) || !event.getMember().canInteract(target)) {        //Example, don't works 

                EmbedBuilder NoPermission = new EmbedBuilder();
                NoPermission.setColor(0xff3923);
                NoPermission.setTitle("\uD83D\uDD34You don't have permission to use this command.");
                NoPermission.setFooter("Usage: " + Main.prefix + "kick <@username> <reason>.",
                        Objects
                                .requireNonNull(event.getMember())
                                .getUser()
                                .getAvatarUrl());

                event
                        .getChannel()
                        .sendMessage(NoPermission.build())
                        .queue();

                NoPermission.clear();
            }
        }
    }
}

UPD: Please, if you downgrade, then point out the mistakes that I made, and not just downgrade it because you wanted to. I want to correct mistakes, not ruin your mood


Solution

  • It seems like you didn't quite get the problems in your code.

    final Member target = event.getMessage().getMentionedMembers().get(0);
    

    This will throw an IndexOutOfBoundsException if your message doesn't include a mentioned member. Because in this case the list of getMentionedMembers() is empty. There is no object to access. get(0) can't get anything.

    In order to fix this you have to check first if the list is empty or if the length is zero as Minn already proposed. If it is you can display a message stating, that they need to add an @username.

    Cleaning up your code and restructuring it accordingly would help a lot I suppose. Please go over it and make sure to always check, if the needed data exists in the first place and if you really need it. Some things are only needed for a small number of cases, calling them on every message / command is not good practice.

    For example, at the moment your bot tries to get the first mentioned member from EVERY message that is sent. This isn't necessary hence you only need it in case it is the kick command.

    By the way, the SelfMember is the bot itself. In your code you are telling the user that they don't have the permission although the bot (and not necessarily the user) might lack it which could be confusing.

    I didn't see it earlier: Your bot is receiving its own messages as well. You might want to check, if the author of a message is your / a bot before continuing.

    In the end I would advice you to always try to understand the answer or advice that is given to you. Asking another question for the same problem isn't going to help you nor does it cast a good light on you and your attempt to learn.