java - Discord 机器人在 if/else 语句中遍历两条路径?

标签 java api discord

我正在使用discord api,并且我正在尝试让我的java机器人根据发送到 channel 的内容输出一条消息。然而,由于某种原因,机器人似乎正在经历 if/else 语句的两个分支,我认为这是不可能的。我的类(class)代码是:

import java.util.ArrayList;

import net.dv8tion.jda.core.entities.*;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
import net.dv8tion.jda.core.hooks.ListenerAdapter;

//channel.sendMessage("Pong ").queue();
//Default Message sender. ^

public class EListener extends ListenerAdapter
{
    ArrayList<String> list = new ArrayList<String>();

public void onMessageReceived(MessageReceivedEvent event)
{
    if (event.getAuthor().isBot()) return; //This helps the bot ignore other bots who send messages that would interfere for some reason. If it is a bot, returns to start.

    Message message = event.getMessage(); //Sets the message equal to the variable type 'Message' so it can be modified within the program.
    String content = message.getContentRaw(); //Gets the raw content of the message and sets it equal to a string (best way to convert types Message to String).
    MessageChannel channel = event.getChannel(); //Gets the channel the message was sent in (useful for sending error messages).

    if(content.startsWith("!suspend"))//Pretty self explanatory. Enters the loop if the message begins with !suspend.
    {
        String[] spliced = content.split("\\s+"); //Splits the message into an array based on the spaces in the message.
        TextChannel textChannel = event.getGuild().getTextChannelsByName("ranked-ms_punishments",true).get(0); //If there is a channel called ranked-ms_punishments which there should be set the value of it equal to the variable.

        int length = spliced.length;//Sets 'length' equal to the number of items in the array.

        if(length == 3)//If the number of items in the array is 3 then...
        {
            if(spliced[1].startsWith("<"))
            {
                textChannel.sendMessage("nab").queue();//Sends the message in the quotations.

            }
        }else {
            channel.sendMessage("Please use the following format for suspending a user: '!suspend' <@user> (length)").queue(); //If length doesn't equal 3 then it sends the message in quotations.
        }
    }
}

}

正如您所看到的,根据代码,如果满足循环中的要求(数组的长度等于三),则机器人应将“nab”输出到指定的不和谐 channel ,如果不满足这些要求满足时,它会向用户输出一条错误消息。由于某种原因,无论我是否符合要求,这两件事都会发生。我的代码中是否存在某些错误导致它执行此操作?

编辑:实际上,只有当我不符合要求时,它才会同时执行这两种操作,但当我符合要求时,它不会发送错误消息。

最佳答案

我认为 onMessageReceived(MessageReceivedEvent event) 方法运行了两次,一次调用的长度条件匹配,而另一次调用条件不匹配,因此您将获得两个输出。

关于java - Discord 机器人在 if/else 语句中遍历两条路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54001911/

相关文章:

javascript - Discord Bot Node JS 简单错误.send

java - 我应该在 API 中集成后台任务吗?

java - Java中按标点符号分割字符串

java - 如何从任意位置使用 JDBC 驱动程序

delphi - Delphi 2010 的 QT 绑定(bind)

Python 和 Youtube Api

python - 如何修复错误 : module 'discord' has no attribute 'Bot'

Java嵌套循环逻辑错误

php - 带有 $id 的路由或 Codeigniter 中的所有数据

python - 我如何知道命令是否需要用户具有特定权限(才能运行)?