JDA 机器人异常

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

JDA Bot Exception

问题

以下是翻译好的内容:

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("⛔ You 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("⛔ You 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("⛔ You 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("⛔ I don't see a 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()) {

尤其是,我无法理解为什么第一行代码不起作用。当我在 Discord 中输入 !kick 命令时,控制台会显示错误:

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

我不知道为什么参数 args 的长度为 0,这非常奇怪。我尝试将 "if-else-if" 更改为 "if",但它不起作用。我真的很想继续创建这个命令,但我不知道该怎么办。

附注:这不是全部的代码,因为由于这个错误,我无法进一步进行。对于我的谷歌翻译英语表示抱歉,非常感谢。

英文:

I have a problem while creating a Java bot for Discord. I decided to make a command for it to kick players from the server. But I ran into difficulties.

Here's a code :

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(&quot;\\s+&quot;);
List&lt;Member&gt; mentionedMembers = event.getMessage().getMentionedMembers();
Member target = mentionedMembers.get(0);
if(args[0].equalsIgnoreCase(Main.prefix + &quot;kick&quot;))
{
if (args.length &lt; 2) {                                                                  //TODO NOT WORK
EmbedBuilder NoUser = new EmbedBuilder();
NoUser.setColor(0xff3923);
NoUser.setTitle(&quot;\uD83D\uDD34You need to add a &lt;@username&gt; and &lt;reason&gt;&quot;);
NoUser.setFooter(&quot;Usage: &quot; + Main.prefix + &quot;kick &lt;@username&gt; &lt;reason&gt;.&quot;,
Objects.requireNonNull(event.getMember()).getUser().getAvatarUrl());
event.getChannel().sendMessage(NoUser.build()).queue();
NoUser.clear();
}
else if (args.length &lt; 3) {                                                                //WORK
EmbedBuilder NoReason = new EmbedBuilder();
NoReason.setColor(0xff3923);
NoReason.setTitle(&quot;\uD83D\uDD34You need to add a &lt;reason&gt;.&quot;);
NoReason.setFooter(&quot;Usage: &quot; + Main.prefix + &quot;kick &lt;@username&gt; &lt;reason&gt;.&quot;,
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
&amp;&amp;
!event.getMember().canInteract(target)) {
EmbedBuilder NoPermission = new EmbedBuilder();
NoPermission.setColor(0xff3923);
NoPermission.setTitle(&quot;\uD83D\uDD34You don&#39;t have permission to use this command.&quot;);
NoPermission.setFooter(&quot;Usage: &quot; + Main.prefix + &quot;kick &lt;@username&gt; &lt;reason&gt;.&quot;,
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(&quot;\uD83D\uDD34I don&#39;t see member with this nickname.&quot;);
NoMember.setFooter(&quot;Usage: &quot; + Main.prefix + &quot;kick &lt;@username&gt; &lt;reason&gt;.&quot;,
Objects.requireNonNull(event.getMember()).getUser().getAvatarUrl());
event.getChannel().sendMessage(NoMember.build()).queue();
NoMember.clear();
}
}
}
}

Promlem in two lines :

if (args.length &lt; 2) {

And

else if(mentionedMembers.isEmpty()) {

Most of all, I can't understand why line 1 does not work. When I write the !kick command in the discord, the console displays an error :

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

I don't know why args length is 0, it's very strange.I tried to "if-else-if" to "if", but it doesn't work.I really wanna to continue creating that command, but i don't know what to do.

P.S.:it's not all of code, because i can't progress further due to this error.Sorry for my Google translater english and many thanks

答案1

得分: 0

异常是由这行代码引起的:

Member target = mentionedMembers.get(0);

你在这里的问题是,并非每条消息都会提到一个成员。请记住,你也会收到自己的消息。如果机器人只发送一个嵌入式消息(embed),内容将为空。

如果内容只有空白字符,你的 split(&quot;\\s+&quot;) 将会返回一个空数组。这将在这行代码中导致 ArrayIndexOutOfBoundsException

if(args[0].equalsIgnoreCase(Main.prefix + &quot;kick&quot;))

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

英文:

The exception is caused by this line:

Member target = mentionedMembers.get(0);

Your problem here is that not every message may mention a member. Keep in mind you also receive your own messages. If the bot only sends an embed, the content will be empty.

If the content is only whitespace your split(&quot;\\s+&quot;) will return an empty array. This will cause an ArrayIndexOutOfBoundsException on this line:

if(args[0].equalsIgnoreCase(Main.prefix + &quot;kick&quot;))

I would recommend checking for the length of mentionedMembers and checking for the length of args before trying to access their respective elements.

huangapple
  • 本文由 发表于 2020年7月27日 19:04:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63113991.html
匿名

发表评论

匿名网友

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

确定