[英]I'm using JDA 4.4.0_352 and my Discord Bot is unable to send message in the channel
Here's my code in Bot.java.这是我在 Bot.java 中的代码。
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import javax.security.auth.login.LoginException;
public class Bot {
private JDA api;
public Bot() throws LoginException, InterruptedException {
api = JDABuilder.createDefault("token")
.addEventListeners(new search()).build()
.awaitReady();
}
public static void main(String[] args) throws LoginException, InterruptedException {
new Bot();
}
}
token is replaced with an actual token in my code:)令牌在我的代码中被替换为实际令牌:)
And here's my code in search.java这是我在搜索中的代码。java
package core;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
public class search extends ListenerAdapter {
@Override
public void onMessageReceived(MessageReceivedEvent event) {
String msg = event.getMessage().getContentRaw();
if (event.getAuthor().isBot())
return;
if (msg.equalsIgnoreCase("!search")) {
event.getPrivateChannel()
.sendMessage("Please enter the course name: ").queue();
}
}
}
I'm not sure why my bot doesn't send the appropriate message when I put search to my discord channel (tried both private and guild).我不确定为什么当我搜索我的 discord 频道时我的机器人没有发送适当的消息(尝试了私人和公会)。
Please let me know if you know where the problem is, thank you!知道问题出在哪里的请告知,谢谢!
The Bot's intents/permissions need to be set in the createDefault method (and possibly inside the discord developer portal). Bot 的意图/权限需要在 createDefault 方法中设置(并且可能在 discord 开发人员门户中)。 For direct messages it'd be:
对于直接消息,它将是:
JDABuilder.createDefault("token", GatewayIntent.DIRECT_MESSAGES)
To handle messages in your guild, I think it'd require Message Content Intent enabled in the discord developer portal;要处理公会中的消息,我认为需要在 discord 开发人员门户中启用消息内容意图; and it'd be:
它会是:
JDABuilder.createDefault("token", GatewayIntent.GUILD_MESSAGES)
The GatewayIntent parameter inside the createDefault method uses varargs, so you can tack on any number of intents: createDefault 方法中的 GatewayIntent 参数使用可变参数,因此您可以处理任意数量的意图:
JDABuilder.createDefault("token", GatewayIntent.DIRECT_MESSAGES, GatewayIntent.GUILD_MESSAGES)
Also, event.getPrivateChannel()
will throw an IllegalStateException in the event that the message is not from a private channel;此外,如果消息不是来自私人频道,则
event.getPrivateChannel()
将抛出 IllegalStateException; I'd recommend first checking if event.getChannelType() == ChannelType.PRIVATE
, or a try/catch.我建议首先检查是否
event.getChannelType() == ChannelType.PRIVATE
或 try/catch。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.