JDA 提及成员 – 命令无效

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

JDA Mentioned member - not working command

问题

I am developing my bot for Discord using Java and JDA API. Before that, I asked a similar question, but I ran into another problem.

From this line, the problems started:

final Member MentionedMem = event.getMessage().getMentionedMembers().get(0);

Thanks to https://stackoverflow.com/users/10630900/minn for answering the previous question in which he explained to me that this line is causing the error:

java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0

For some reason or other, it raises an error.

I was trying to find an answer to why this command does not work. The only thing I know is that this command returns an empty array args. Because of this, I cannot finish the bot, that is, I cannot check whether a member can kick others and can't make the main block of code kick members.

How can I fix this error and/or write the rest of the code? Sorry for my English and many thanks to you.

Some code:

public class KickComm extends ListenerAdapter {

    public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
        String[] message = event
                .getMessage()
                .getContentRaw()
                .split(" ");

        // final Member target = event.getMessage().getMentionedMembers().get(0); ERROR CUZ I DONT NOW WHY

        final Member SelfMember = event
                .getGuild()
                .getSelfMember();

        if(message[0].equalsIgnoreCase(Main.prefix + "kick"))
        {
            if (message.length < 2) {

                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 (message.length < 3) {

                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(!SelfMember.hasPermission(Permission.KICK_MEMBERS)) {

                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(!Objects.requireNonNull(event.getMember()).hasPermission(Permission.KICK_MEMBERS) || !event.getMember().canInteract(target)) { //Example, don't works 

                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();
            }
        }
    }
}
英文:

I am developing my bot for discord using Java and JDA API. Before that I asked a similar question, but I ran into another problem.

From this line the problems started :

final Member MentionedMem = event.getMessage().getMentionedMembers().get(0);

Thanks to https://stackoverflow.com/users/10630900/minn for answering the previous question in which he explained to me that this line is causing the error :

java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0

For some reason or other it raises an error.

I was trying to find an answer to why this command does not work. The only thing I know is that this command returns an empty array args. Because of this, I cannot finish the bot, that is, I cannot check whether a member can kick others and can't make the main block of code kick members.

How can I fix this error and / or write the rest of the code?
Sorry for my English and many thanks to you.

Some code :

public class KickComm extends ListenerAdapter {
public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
String[] message = event
.getMessage()
.getContentRaw()
.split(&quot; &quot;);
// final Member target = event.getMessage().getMentionedMembers().get(0); ERROR CUZ I DONT NOW WHY
final Member SelfMember = event
.getGuild()
.getSelfMember();
if(message[0].equalsIgnoreCase(Main.prefix + &quot;kick&quot;))
{                                                                                                   
if (message.length &lt; 2) {
EmbedBuilder NoUser = new EmbedBuilder();
NoUser.setColor(0xff3923);
NoUser.setTitle(&quot;\uD83D\uDD34You need to add a &lt;@username&gt; and &lt;reason&gt;&quot;);
NoUser.setFooter(&quot;Usage: &quot; + Main.prefix + &quot;kick &lt;@username&gt; &lt;reason&gt;.&quot;,
Objects
.requireNonNull(event.getMember())
.getUser()
.getAvatarUrl());
event
.getChannel()
.sendMessage(NoUser.build())
.queue();
NoUser.clear();
} else if (message.length &lt; 3) {                                                                
EmbedBuilder NoReason = new EmbedBuilder();
NoReason.setColor(0xff3923);
NoReason.setTitle(&quot;\uD83D\uDD34You need to add a &lt;reason&gt;.&quot;);
NoReason.setFooter(&quot;Usage: &quot; + Main.prefix + &quot;kick &lt;@username&gt; &lt;reason&gt;.&quot;,
Objects
.requireNonNull(event.getMember())
.getUser()
.getAvatarUrl());
event
.getChannel()
.sendMessage(NoReason.build())
.queue();
NoReason.clear();
} else if(!SelfMember.hasPermission(Permission.KICK_MEMBERS)) {
EmbedBuilder NoPermission = new EmbedBuilder();
NoPermission.setColor(0xff3923);
NoPermission.setTitle(&quot;\uD83D\uDD34You don&#39;t have permission to use this command.&quot;);
NoPermission.setFooter(&quot;Usage: &quot; + Main.prefix + &quot;kick &lt;@username&gt; &lt;reason&gt;.&quot;,
Objects
.requireNonNull(event.getMember())
.getUser()
.getAvatarUrl());
event
.getChannel()
.sendMessage(NoPermission.build())
.queue();
NoPermission.clear();
} else if(!Objects.requireNonNull(event.getMember()).hasPermission(Permission.KICK_MEMBERS) || !event.getMember().canInteract(target)) {        //Example, don&#39;t works 
EmbedBuilder NoPermission = new EmbedBuilder();
NoPermission.setColor(0xff3923);
NoPermission.setTitle(&quot;\uD83D\uDD34You don&#39;t have permission to use this command.&quot;);
NoPermission.setFooter(&quot;Usage: &quot; + Main.prefix + &quot;kick &lt;@username&gt; &lt;reason&gt;.&quot;,
Objects
.requireNonNull(event.getMember())
.getUser()
.getAvatarUrl());
event
.getChannel()
.sendMessage(NoPermission.build())
.queue();
NoPermission.clear();
}
}
}
}

UPD: Please, if you downgrade, then point out the mistakes that I made, and not just downgrade it because you wanted to. I want to correct mistakes, not ruin your mood

答案1

得分: 0

以下是翻译好的部分:

似乎您没有完全理解您的代码中存在的问题。

final Member target = event.getMessage().getMentionedMembers().get(0);

如果您的消息没有提到任何成员,这段代码将引发 IndexOutOfBoundsException。因为在这种情况下,getMentionedMembers() 返回的列表为空。没有对象可以访问。get(0) 无法获取任何内容。

为了修复这个问题,您首先需要检查列表是否为空或长度是否为零,就像Minn已经建议的那样。如果是这样,您可以显示一条消息,指出他们需要添加一个 @用户名。

清理您的代码并相应地重新构造它将有很大帮助。请仔细检查,确保所需的数据是否存在,以及是否真的需要它。有些东西只在少数情况下需要,每次消息/命令都调用它们不是一个好的做法。

例如,目前您的机器人试图从每个发送的消息中获取第一个被提到的成员。这是不必要的,因此您只在执行踢人命令时需要它。

顺便说一下,SelfMember 是机器人本身。在您的代码中,您告诉用户他们没有权限,尽管机器人(而不一定是用户)可能缺少权限,这可能会令人困惑。

我之前没有看到:您的机器人也接收到自己的消息。在继续之前,您可能希望检查消息的作者是您的机器人还是其他机器人。

最后,我建议您始终尝试理解给出的答案或建议。为同一个问题提出另一个问题既不会帮助您,也不会为您以及您学习的努力蒙上好的光辉。

英文:

It seems like you didn't quite get the problems in your code.

final Member target = event.getMessage().getMentionedMembers().get(0);

This will throw an IndexOutOfBoundsException if your message doesn't include a mentioned member. Because in this case the list of getMentionedMembers() is empty. There is no object to access. get(0) can't get anything.

In order to fix this you have to check first if the list is empty or if the length is zero as Minn already proposed. If it is you can display a message stating, that they need to add an @username.

Cleaning up your code and restructuring it accordingly would help a lot I suppose. Please go over it and make sure to always check, if the needed data exists in the first place and if you really need it. Some things are only needed for a small number of cases, calling them on every message / command is not good practice.

For example, at the moment your bot tries to get the first mentioned member from EVERY message that is sent. This isn't necessary hence you only need it in case it is the kick command.

By the way, the SelfMember is the bot itself. In your code you are telling the user that they don't have the permission although the bot (and not necessarily the user) might lack it which could be confusing.

I didn't see it earlier: Your bot is receiving its own messages as well. You might want to check, if the author of a message is your / a bot before continuing.

In the end I would advice you to always try to understand the answer or advice that is given to you. Asking another question for the same problem isn't going to help you nor does it cast a good light on you and your attempt to learn.

答案2

得分: 0

用户 mentionedUser = event.getMessage().getMentionedMembers().get(0).getUser();

如果还有人需要...

问题在于,出现了某种原因,您使用了 Member 而不是 User
此外,您必须使用 .getUser() 才能真正获取到所需的用户。

英文:
User mentionedUser = event.getMessage().getMentionedMembers().get(0).getUser();

If someone still needs it...

The issue, for some reason, was that you used Member instead of User.
Futhermore, you must use .getUser() to really get the user you want.

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

发表评论

匿名网友

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

确定