英文:
Java Discord API commands printing nothing
问题
以下是翻译好的内容:
Init 类:
package corp.vjz.bots.discord.testbot;
import javax.security.auth.login.LoginException;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.OnlineStatus;
public class Initialize {
public static void main(String[] args) throws LoginException {
// 启动 Discord 机器人
JDA jda = JDABuilder.createDefault("NzIyMTMyNjczOTkyMzI3MzEw.Xueodg.EpeszQDFxc1IM21_CZmKMUv7Wys").build();
// 设置 Discord 机器人的状态
jda.getPresence().setStatus(OnlineStatus.ONLINE);
// 添加新的事件监听器
jda.addEventListener(new Commands());
}
}
以下是我的 commands 类:
package corp.vjz.bots.discord.testbot;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
public class Commands extends ListenerAdapter {
private static String PREFIX = "-";
public void onGuildMsgReceived(GuildMessageReceivedEvent event) {
// 解析消息
String[] args = event.getMessage().getContentRaw().split("\\s+");
if (args[0].equalsIgnoreCase(Commands.PREFIX + "info")) {
event.getChannel().sendTyping().queue();
event.getChannel().sendMessage("Wassup! BRO.").queue();
}
}
}
这应该在用户发送“-info”时显示“Wassup! BRO.”,但实际上没有显示出来。是的,我确实邀请了 Discord 机器人并进行了 OAuth2 等操作,那一端没问题,只是我不知道为什么没有输出预期的结果。
英文:
So I am extremely new to this JDA library but I am well-versed in Java. I just can't seem to find out why the following code does not work. I have a Init class and a commands class.
Init class:
package corp.vjz.bots.discord.testbot;
import javax.security.auth.login.LoginException;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.OnlineStatus;
public class Initialize {
public static void main(String[] args) throws LoginException {
//starts the discord robot
JDA jda = JDABuilder.createDefault("NzIyMTMyNjczOTkyMzI3MzEw.Xueodg.EpeszQDFxc1IM21_CZmKMUv7Wys").build();
//sets the status of the discord robot
jda.getPresence().setStatus(OnlineStatus.ONLINE);
//add a new event listener
jda.addEventListener(new Commands());
}
}
Here is my commands class:
package corp.vjz.bots.discord.testbot;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
public class Commands extends ListenerAdapter {
private static String PREFIX = "-";
public void onGuildMsgReceived(GuildMessageReceivedEvent event) {
//parse through message
String[] args = event.getMessage().getContentRaw().split("\\s+");
if (args[0].equalsIgnoreCase(Commands.PREFIX + "info")) {
event.getChannel().sendTyping().queue();
event.getChannel().sendMessage("Wassup! BRO.").queue();
}
}
}
This should say "Wassup! BRO." whenever a user chats "-info" but in reality, nothing shows up. And yes I did invite the discord bot and did the Oauth2 and whatever, that end is fine, just I don't know why its not printing what it should print.
答案1
得分: 0
你没有覆盖ListenerAdapter中的方法。引用来自故障排除指南:
4. 你没有覆盖正确的方法?
使用@Override
,看看是否失败。你的方法必须使用ListenerAdapter中定义的正确名称和参数列表。阅读更多。
因此正确的方法名称在ListenerAdapter的javadoc中有记录,即onGuildMessageReceived
。
英文:
You didn't override the method from ListenerAdapter. To quote from the troubleshooting guide:
> 4. You did not override the correct method?<br>
> Use @Override
and see if it fails. Your method has to use the correct name and parameter list defined in ListenerAdapter. Read More.
So the right method name is documented in the ListenerAdapter javadoc which is onGuildMessageReceived
.
答案2
得分: -1
你没有为if语句编写else部分的代码;
英文:
you didn't write the else code for the if command;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论