Discord bot 无法验证用户是否不存在,抛出 indexoutofbounds 错误
Discord bot cannot verify if the user is not existing, throws an indexoutofbounds error
我正在创建一个不和谐的机器人,我已经设法调用用户输入的名称,但是,如果服务器中不存在该用户,我无法抛出错误消息并一直给我IndexOutOfBoundsError.
String[] message = e.getMessage().getContentRaw().split(" ");
if(message.length == 1 && message[0].equalsIgnoreCase("!user")){
e.getChannel().sendMessage("To get a user's info, type !user [name]").queue();
}
else if(message.length == 2 && message[0].equalsIgnoreCase("!user")){
String username = message[1];
User user = e.getGuild().getMembersByName(username, true).get(0).getUser();
EmbedBuilder avatarEmbed = new EmbedBuilder();
String avatar = user.getAvatarUrl();
avatarEmbed.setTitle(user.getName() + "'s Info:");
avatarEmbed.addField("Name", user.getName(), true);
avatarEmbed.addField("Online Status: ", e.getGuild().getMembersByName(username,
true).get(0).getOnlineStatus().toString(), true);
例如,如果我输入!user Mark,那么如果用户存在于服务器中,机器人将显示用户的信息。
if(!user.getName().equalsIgnoreCase(username)){
e.getChannel().sendMessage("User doesn't exist!").queue();
}
这是验证用户是否不存在的代码,机器人会发消息但它给了我一个 IndexOutOfBoundError。可能是什么原因?如果您需要更多代码,请留下答案,但我相信这就足够了,其他代码只是添加字段和设计等。
[JDA MainWS-ReadThread] ERROR JDA - One of the EventListeners had an uncaught exception
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.base/java.util.LinkedList.checkElementIndex(LinkedList.java:559)
at java.base/java.util.LinkedList.get(LinkedList.java:480)
at java.base/java.util.Collections$UnmodifiableList.get(Collections.java:1310)
at UserInfoCommand.onGuildMessageReceived(UserInfoCommand.java:25)
at net.dv8tion.jda.core.hooks.ListenerAdapter.onEvent(ListenerAdapter.java:373)
at net.dv8tion.jda.core.hooks.InterfacedEventManager.handle(InterfacedEventManager.java:84)
at net.dv8tion.jda.core.handle.MessageCreateHandler.handleInternally(MessageCreateHandler.java:90)
at net.dv8tion.jda.core.handle.SocketHandler.handle(SocketHandler.java:37)
at net.dv8tion.jda.core.requests.WebSocketClient.handleEvent(WebSocketClient.java:1040)
at net.dv8tion.jda.core.requests.WebSocketClient.onTextMessage(WebSocketClient.java:680)
这是错误代码。谢谢!
问题出在User user = e.getGuild().getMembersByName(username, true).get(0).getUser();
看,您正在使用 getMembersByName("name", ignoreCase);
方法调用成员数组列表。但是因为没有找到成员,所以那个arraylist将被赋予0个成员。
尝试使用此代码进行更改:
String[] message = e.getMessage().getContentRaw().split(" ");
if(message.length == 1 && message[0].equalsIgnoreCase("!user")){
e.getChannel().sendMessage("To get a user's info, type !user [name]").queue();
}
else if(message.length == 2 && message[0].equalsIgnoreCase("!user")){
String username = message[1];
ArrayList<Member> memberList = e.getGuild().getMembersByName(username, true);
if (memberList.size()>0) {
User user = memberList.get(0);
EmbedBuilder avatarEmbed = new EmbedBuilder();
String avatar = user.getAvatarUrl();
avatarEmbed.setTitle(user.getName() + "'s Info:");
avatarEmbed.addField("Name", user.getName(), true);
avatarEmbed.addField("Online Status: ", memberList.get(0).getOnlineStatus().toString(), true);
} else {
e.getChannel().sendMessage("User doesn't exist!").queue();
}
}
这将检查公会中是否有满足论点的成员。如果不是(没有该名称的成员),它将转到错误消息!
我正在创建一个不和谐的机器人,我已经设法调用用户输入的名称,但是,如果服务器中不存在该用户,我无法抛出错误消息并一直给我IndexOutOfBoundsError.
String[] message = e.getMessage().getContentRaw().split(" ");
if(message.length == 1 && message[0].equalsIgnoreCase("!user")){
e.getChannel().sendMessage("To get a user's info, type !user [name]").queue();
}
else if(message.length == 2 && message[0].equalsIgnoreCase("!user")){
String username = message[1];
User user = e.getGuild().getMembersByName(username, true).get(0).getUser();
EmbedBuilder avatarEmbed = new EmbedBuilder();
String avatar = user.getAvatarUrl();
avatarEmbed.setTitle(user.getName() + "'s Info:");
avatarEmbed.addField("Name", user.getName(), true);
avatarEmbed.addField("Online Status: ", e.getGuild().getMembersByName(username,
true).get(0).getOnlineStatus().toString(), true);
例如,如果我输入!user Mark,那么如果用户存在于服务器中,机器人将显示用户的信息。
if(!user.getName().equalsIgnoreCase(username)){
e.getChannel().sendMessage("User doesn't exist!").queue();
}
这是验证用户是否不存在的代码,机器人会发消息但它给了我一个 IndexOutOfBoundError。可能是什么原因?如果您需要更多代码,请留下答案,但我相信这就足够了,其他代码只是添加字段和设计等。
[JDA MainWS-ReadThread] ERROR JDA - One of the EventListeners had an uncaught exception
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.base/java.util.LinkedList.checkElementIndex(LinkedList.java:559)
at java.base/java.util.LinkedList.get(LinkedList.java:480)
at java.base/java.util.Collections$UnmodifiableList.get(Collections.java:1310)
at UserInfoCommand.onGuildMessageReceived(UserInfoCommand.java:25)
at net.dv8tion.jda.core.hooks.ListenerAdapter.onEvent(ListenerAdapter.java:373)
at net.dv8tion.jda.core.hooks.InterfacedEventManager.handle(InterfacedEventManager.java:84)
at net.dv8tion.jda.core.handle.MessageCreateHandler.handleInternally(MessageCreateHandler.java:90)
at net.dv8tion.jda.core.handle.SocketHandler.handle(SocketHandler.java:37)
at net.dv8tion.jda.core.requests.WebSocketClient.handleEvent(WebSocketClient.java:1040)
at net.dv8tion.jda.core.requests.WebSocketClient.onTextMessage(WebSocketClient.java:680)
这是错误代码。谢谢!
问题出在User user = e.getGuild().getMembersByName(username, true).get(0).getUser();
看,您正在使用 getMembersByName("name", ignoreCase);
方法调用成员数组列表。但是因为没有找到成员,所以那个arraylist将被赋予0个成员。
尝试使用此代码进行更改:
String[] message = e.getMessage().getContentRaw().split(" ");
if(message.length == 1 && message[0].equalsIgnoreCase("!user")){
e.getChannel().sendMessage("To get a user's info, type !user [name]").queue();
}
else if(message.length == 2 && message[0].equalsIgnoreCase("!user")){
String username = message[1];
ArrayList<Member> memberList = e.getGuild().getMembersByName(username, true);
if (memberList.size()>0) {
User user = memberList.get(0);
EmbedBuilder avatarEmbed = new EmbedBuilder();
String avatar = user.getAvatarUrl();
avatarEmbed.setTitle(user.getName() + "'s Info:");
avatarEmbed.addField("Name", user.getName(), true);
avatarEmbed.addField("Online Status: ", memberList.get(0).getOnlineStatus().toString(), true);
} else {
e.getChannel().sendMessage("User doesn't exist!").queue();
}
}
这将检查公会中是否有满足论点的成员。如果不是(没有该名称的成员),它将转到错误消息!