(JDA) 静音命令在代码的第四行出现问题。

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

(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:

  1. 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.

  2. The error you're encountering, java.lang.NullPointerException, suggests that there might be an issue with the objects being accessed. You should check if event.getGuild() or event.getMessage() might be null in certain cases, causing this exception.

英文:
String[] messageSent = event.getMessage().getContentRaw().split(&quot;\\s+&quot;);
if (messageSent[0].equalsIgnoreCase(&quot;/mute&quot;)) {
            if (messageSent.length == 2) {
                Member member = event.getGuild().getMemberById(messageSent[1].replace(&quot;&lt;@&quot;, &quot;&quot;).replace(&quot;&gt;&quot;, &quot;&quot;));
                Role role = event.getGuild().getRoleById(&quot;731166424608931900&quot;);

                if (!member.getRoles().contains(role)) {
                        //mute user
                    event.getChannel().sendMessage(&quot;Muted &quot; + messageSent[1] + &quot;.&quot;).queue();
                    event.getGuild().addRoleToMember(member, role);
                }else {
                    //unmute user
                    event.getChannel().sendMessage(&quot;Unmuted &quot; + messageSent[1] + &quot;.&quot;).queue();
                    event.getGuild().removeRoleFromMember(member, role);
                }

            }else if (messageSent.length == 3) {

            }else {
                event.getChannel().sendMessage(&quot;incorrect syntax i dont have time to make this error message pretty, fuck!&quot;).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(&quot;&lt;@&quot;, &quot;&quot;).replace(&quot;&gt;&quot;, &quot;&quot;));

2nd: i get this error [JDA MainWS-ReadThread] ERROR JDA - One of the EventListeners had an uncaught exception
java.lang.NullPointerException

答案1

得分: 0

  1. 你应该只使用 Message#getMentionedUsers,让 JDA 为你进行解析。

  2. 确保 Member 已经被 缓存,或者使用 Guild#retrieveMemberById(或其他 retrieveMember* 方法之一)来代替。

编辑:也请查看 故障排除:未在缓存中的用户/成员

英文:
  1. You should just use Message#getMentionedUsers and let JDA do the parsing for you.

  2. Make sure the Member is cached or use Guild#retrieveMemberById (or one of the other retrieveMember* methods) instead.

Edit: See also Troubleshooting:
Users/Members not in cache

huangapple
  • 本文由 发表于 2020年8月2日 17:10:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63214263.html
匿名

发表评论

匿名网友

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

确定