JDA 机器人异常

JDA Bot Exception

我在为 Discord 创建 Java 机器人时遇到问题。我决定为其发出命令,将玩家踢出服务器。但是我运行陷入了困境。

这是一个代码:

package com.company;

import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;

import java.util.List;
import java.util.Objects;

public class KickComm extends ListenerAdapter {

    public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
        String[] args = event.getMessage().getContentRaw().split("\s+");

        List<Member> mentionedMembers = event.getMessage().getMentionedMembers();
        Member target = mentionedMembers.get(0);

        if(args[0].equalsIgnoreCase(Main.prefix + "kick"))
        {
            if (args.length < 2) {                                                                  //TODO NOT WORK

                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 (args.length < 3) {                                                                //WORK

                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(!Objects.requireNonNull(event.getMember()).hasPermission(Permission.KICK_MEMBERS)  //WORK
                &&
                     !event.getMember().canInteract(target)) {

                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(mentionedMembers.isEmpty()) {                                                       //TODO NOT WORK 

                EmbedBuilder NoMember = new EmbedBuilder();
                NoMember.setColor(0xff3923);
                NoMember.setTitle("\uD83D\uDD34I don't see member with this nickname.");
                NoMember.setFooter("Usage: " + Main.prefix + "kick <@username> <reason>.",
                        Objects.requireNonNull(event.getMember()).getUser().getAvatarUrl());

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

                NoMember.clear();
            }
        }
    }
}

两行问题:

if (args.length < 2) {

else if(mentionedMembers.isEmpty()) {

最重要的是,我不明白为什么第 1 行不起作用。当我在 discord 中写 !kick 命令时,控制台显示错误:

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

我不知道为什么 args 长度为 0,strange.I 很想把“if-else-if”改成“if”,但 work.I 真的不想继续创建该命令,但我不知道该怎么做。

P.S.:这不是所有的代码,因为由于这个 error.Sorry 我的 Google t运行slater 英语和许多谢谢

异常是由这一行引起的:

Member target = mentionedMembers.get(0);

你的问题是不是每条消息都可能提到一个成员。请记住,您还会收到自己的消息。如果机器人只发送一个嵌入,内容将为空。

如果内容只是空白,您的 split("\s+") 将 return 一个空数组。这将导致此行出现 ArrayIndexOutOfBoundsException

if(args[0].equalsIgnoreCase(Main.prefix + "kick"))

我建议在尝试访问它们各自的元素之前检查 mentionedMembers 的长度并检查 args 的长度。