英文:
(JDA) Mute command is breaking at the fourth line of the code
问题
Here are the translated parts of the code you provided:
String[] messageSent = event.getMessage().getContentRaw().split("\\s+");
if (messageSent[0].equalsIgnoreCase("/mute")) {
if (messageSent.length == 2) {
Member member = event.getGuild().getMemberById(messageSent[1].replace("<@&", "").replace(">", ""));
Role role = event.getGuild().getRoleById("731166424608931900");
if (!member.getRoles().contains(role)) {
// Mute user
event.getChannel().sendMessage("Muted " + messageSent[1] + ".").queue();
event.getGuild().addRoleToMember(member, role);
} else {
// Unmute user
event.getChannel().sendMessage("Unmuted " + messageSent[1] + ".").queue();
event.getGuild().removeRoleFromMember(member, role);
}
} else if (messageSent.length == 3) {
// Handle the case with 3 elements
} else {
event.getChannel().sendMessage("incorrect syntax i dont have time to make this error message pretty, fuck!").queue();
}
}
Regarding the issues you mentioned:
-
The fourth line
Member member = event.getGuild()...
extracts a member from the guild based on the provided input. It replaces<@&
and>
in the input to get the member's ID. -
The error you're encountering,
java.lang.NullPointerException
, suggests that there might be an issue with the objects being accessed. You should check ifevent.getGuild()
orevent.getMessage()
might be null in certain cases, causing this exception.
英文:
String[] messageSent = event.getMessage().getContentRaw().split("\\s+");
if (messageSent[0].equalsIgnoreCase("/mute")) {
if (messageSent.length == 2) {
Member member = event.getGuild().getMemberById(messageSent[1].replace("<@", "").replace(">", ""));
Role role = event.getGuild().getRoleById("731166424608931900");
if (!member.getRoles().contains(role)) {
//mute user
event.getChannel().sendMessage("Muted " + messageSent[1] + ".").queue();
event.getGuild().addRoleToMember(member, role);
}else {
//unmute user
event.getChannel().sendMessage("Unmuted " + messageSent[1] + ".").queue();
event.getGuild().removeRoleFromMember(member, role);
}
}else if (messageSent.length == 3) {
}else {
event.getChannel().sendMessage("incorrect syntax i dont have time to make this error message pretty, fuck!").queue();
}
}
so the problem with this is that
1st: it probably breaks at the fourth line aka Member member = event.getGuild().getMemberById(messageSent[1].replace("<@", "").replace(">", ""));
2nd: i get this error [JDA MainWS-ReadThread] ERROR JDA - One of the EventListeners had an uncaught exception
java.lang.NullPointerException
答案1
得分: 0
-
你应该只使用
Message#getMentionedUsers
,让 JDA 为你进行解析。 -
确保 Member 已经被 缓存,或者使用
Guild#retrieveMemberById
(或其他retrieveMember*
方法之一)来代替。
编辑:也请查看 故障排除:未在缓存中的用户/成员。
英文:
-
You should just use
Message#getMentionedUsers
and let JDA do the parsing for you. -
Make sure the Member is cached or use
Guild#retrieveMemberById
(or one of the otherretrieveMember*
methods) instead.
Edit: See also Troubleshooting:
Users/Members not in cache
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论