英文:
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("\\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();
}
}
}
}
Promlem in two lines :
if (args.length < 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("\\s+")
将会返回一个空数组。这将在这行代码中导致 ArrayIndexOutOfBoundsException
:
if(args[0].equalsIgnoreCase(Main.prefix + "kick"))
我建议在尝试访问它们各自的元素之前,检查 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("\\s+")
will return an empty array. This will cause an ArrayIndexOutOfBoundsException
on this line:
if(args[0].equalsIgnoreCase(Main.prefix + "kick"))
I would recommend checking for the length of mentionedMembers
and checking for the length of args
before trying to access their respective elements.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论