Java 在if/else语句中,Discord bot通过两条路径?
我正在使用discordapi,并且我正在尝试让我的javabot根据发送到通道的内容输出消息。然而,出于某种原因,bot似乎正在通过if/else语句的两个分支,我认为这是不可能的。我的课程代码是:Java 在if/else语句中,Discord bot通过两条路径?,java,api,discord,Java,Api,Discord,我正在使用discordapi,并且我正在尝试让我的javabot根据发送到通道的内容输出消息。然而,出于某种原因,bot似乎正在通过if/else语句的两个分支,我认为这是不可能的。我的课程代码是: import java.util.ArrayList; import net.dv8tion.jda.core.entities.*; import net.dv8tion.jda.core.events.message.MessageReceivedEvent; import net.dv8t
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.
}
}
}
import java.util.ArrayList;
导入net.dv8tion.jda.core.entities.*;
导入net.dv8tion.jda.core.events.message.MessageReceivedEvent;
导入net.dv8tion.jda.core.hooks.ListenerAdapter;
//channel.sendMessage(“Pong”).queue();
//默认消息发送者^
公共类EListener扩展ListenerAdapter
{
ArrayList=新建ArrayList();
MessageReceived上的公共无效(MessageReceivedEvent事件)
{
if(event.getAuthor().isBot())return;//这有助于bot忽略其他发送消息的bot,这些消息可能会因某种原因而受到干扰。如果是bot,则返回到start。
Message Message=event.getMessage();//将消息设置为变量类型“Message”,以便在程序中修改。
String content=message.getContentRaw();//获取消息的原始内容并将其设置为字符串(将消息类型转换为字符串的最佳方式)。
MessageChannel=event.getChannel();//获取发送消息的通道(用于发送错误消息)。
if(content.startsWith(“!suspend”)//非常简单。如果消息以!suspend开头,则进入循环。
{
String[]splicted=content.split(\\s+);//根据消息中的空格将消息拆分为数组。
TextChannel TextChannel=event.getGuild().getTextChannelsByName(“ranked-ms_惩罚”,true)。get(0);//如果有一个名为ranked-ms_惩罚的频道,则应将其值设置为等于变量。
int length=spaited.length;//将“length”设置为数组中的项数。
if(length==3)//如果数组中的项数为3,则。。。
{
如果(拼接[1].startsWith(“我认为MessageReceived(MessageReceivedEvent事件)上的方法运行了两次,则在一个调用中,长度的条件匹配,而在另一个调用中,长度的条件不匹配,因此您将获得两个输出