【发布时间】:2021-05-03 21:24:45
【问题描述】:
我正在编写一个不和谐的机器人,以便在私人服务器中使用,只是为了搞乱。这是我第一次接触java。我正在使用 Discord JDA 库对机器人进行编码。不过,我认为这不是主要问题。
我对如何从我创建的单独类中的特定方法提取输出感到困惑。
我试图从一个名为 Color.java 的单独类中的公共 String 方法中提取一个 String 到一个名为 Commands.java 的文件中。该字符串旨在通过使用带有随机数生成器的 Array List 进行随机化。
这是我的 Commands.java 代码。这不是主文件,而是与问题相关的文件,更具体地说是此代码的最后一个 else if {}。
public class Commands extends ListenerAdapter {
@Override
public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
String[] args = event.getMessage().getContentRaw().split(" ");
if (args[0].equalsIgnoreCase(bot.prefix + "info")) {
event.getChannel().sendTyping().queue();
event.getChannel().sendMessage("This is a test info description").queue();
}
else if (args [0].equalsIgnoreCase(bot.prefix + "ping")) {
long ping = event.getJDA().getGatewayPing();
event.getChannel().sendMessage(ping + "ms").queue();
}
else if (args [0-100].equalsIgnoreCase("white")){
Race newColorobj = new Color();
String white_test = newColorobj.white();
event.getChannel().sendMessage(white_test + ".").queue();
}
}
}
我打算从这个文件中提取最后的“else if”,Color.java,从数组列表“white”中挑选出一个随机的文本字符串,并将其输出到不和谐的聊天频道中。
public class Color {
Random rand = new Random();
int upperbound = 1;
int int_random = rand.nextInt(upperbound);
public String white() {
ArrayList<String> white = new ArrayList<String>();
white.add("This is a test");
return white.get(int_random);
}
}
我的终端在编译的时候输出这个错误,但是还是成功运行了:
white : The term 'white' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At line:1 char:1
+ white c:; cd 'c:\Users\Colin\Dylan and Colin'; & 'c:\Users\Colin\.vsc ...
+ ~~~~~
+ CategoryInfo : ObjectNotFound: (white:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
如果用户试图对机器人说“白色”,它不会以我想要它响应的文本字符串响应。
我还是 Java 新手。我做错了什么?
【问题讨论】:
-
错误信息与问题中的代码不相关。请出示相关的类和代码。
标签: java discord discord-jda