为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行不起作用。当我在不和谐中编写!kick命令时,控制台显示错误:
java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
我不知道为什么args长度为0,这很奇怪。我试图将“ if-else-if”改为“ if”,但是它不起作用。我真的想继续创建该命令,但是我不知道该怎么办。
P.S .:并非全部代码,因为该错误导致我无法进一步前进。对不起,我的Google翻译器英文非常感谢
答案 0 :(得分:0)
此行引起异常:
Member target = mentionedMembers.get(0);
您的问题是,并非每条消息都可能提及一个成员。请记住,您还会收到自己的消息。如果漫游器仅发送嵌入内容,则内容将为空。
如果内容仅是空白,则split("\\s+")
将返回一个空数组。这将在此行上产生ArrayIndexOutOfBoundsException
:
if(args[0].equalsIgnoreCase(Main.prefix + "kick"))
在尝试访问各自的元素之前,我建议检查mentionedMembers
的长度并检查args
的长度。