onMessageReceived() 未被调用 |不和谐机器人
onMessageReceived() not being called | Discord Bot
我正在用 Java 制作我的第一个 Discord 机器人。我正在使用 Gradle。到目前为止,这是我的代码:
public class Main extends ListenerAdapter {
public static void main(String[] args) throws LoginException {
JDABuilder builder = new JDABuilder(AccountType.BOT);
String token = "x";
builder.setToken(token);
builder.addEventListener(new Main());
builder.buildAsync();
}
@Override
public void onMessageReceived(MessageReceivedEvent event) {
System.out.println("We received a message from " +
event.getAuthor().getName() + ": " +
event.getMessage().getContentDisplay()
);
if (event.getMessage().getContentRaw().equals("yomyman")) {
event.getChannel().sendMessage("Pong!").queue();
}
}
}
当我执行程序时,我没有收到任何警告,我的机器人在我的 discord 服务器上线了。但是由于某种原因,当我在任何频道中键入“yomyman”时,什么也没有发生。控制台上没有打印任何内容,机器人也没有在 .最重要的是方法 onMessageReceived();根本不会被调用。这是我在这个项目中唯一的代码。
附加信息:
- Gradle 正在使用
- 正在使用 JDA
- 没有错误打印到控制台
您遇到了 a discord developer support article 中描述的问题:
WHY IS MY BOT NO LONGER WORKING SUDDENLY?
When the new gateway update was deployed, bots which weren't adequately prepared to specify gateway intents could have experienced a variety of issues. Common symptoms include:
- The bot not responding anymore, despite being online
- The bot cache being empty (only seeing a few users across all guilds)
- A library on ready event not firing or timing out (happening mostly on discord.py and discord.js bots but other libraries might exhibit the same behavior)
If you are affected by this, this is because of 2 gateway changes:
- You no longer have access to disabled privileged intents if you are not specifying intents
- You can no longer request member info for multiple guilds at the same time
HOW DO I GET BACK UP AND RUNNING?
UPDATING YOUR LIBRARY
First, you must make sure you're using a library version that can handle intents, and by extension no longer requests information on multiple guilds at once.
For discord.py, this means you need to update to v1.5 or higher.
For discord.js, this means you need to update to v12 or higher.
If you're unsure how to upgrade your bot to a new library release or are unclear regarding the intent compatibility of an alternative library, please see the library links provided in our Developer Community Resources.
SPECIFYING GATEWAY INTENTS
Please note that a variety of our gateway intents are not privileged, meaning they do not require specific switches to be flipped or whitelisted access to be requested. You can review the entirety of our available intents here.
Specifying which intents you'd like to receive varies based on your chosen library. Documentation on how to specify gateway intents in discord.js is available here. Documentation for specifying gateway intents in discord.py is available here.
所以,您需要先更新JDA。我建议您为此使用最新版本。您可以在此处查看最新版本代码:
之后,您应该像这样更改 in the README of JDA 定义的 JDABuilder 构造函数的使用:
JDABuilder builder=JDABuilder.createDefault("YOUR_TOKEN_HERE");
如果您需要特权网关意图,您还需要在开发者页面和您的应用程序中启用它们。
我正在用 Java 制作我的第一个 Discord 机器人。我正在使用 Gradle。到目前为止,这是我的代码:
public class Main extends ListenerAdapter {
public static void main(String[] args) throws LoginException {
JDABuilder builder = new JDABuilder(AccountType.BOT);
String token = "x";
builder.setToken(token);
builder.addEventListener(new Main());
builder.buildAsync();
}
@Override
public void onMessageReceived(MessageReceivedEvent event) {
System.out.println("We received a message from " +
event.getAuthor().getName() + ": " +
event.getMessage().getContentDisplay()
);
if (event.getMessage().getContentRaw().equals("yomyman")) {
event.getChannel().sendMessage("Pong!").queue();
}
}
}
当我执行程序时,我没有收到任何警告,我的机器人在我的 discord 服务器上线了。但是由于某种原因,当我在任何频道中键入“yomyman”时,什么也没有发生。控制台上没有打印任何内容,机器人也没有在 .最重要的是方法 onMessageReceived();根本不会被调用。这是我在这个项目中唯一的代码。
附加信息:
- Gradle 正在使用
- 正在使用 JDA
- 没有错误打印到控制台
您遇到了 a discord developer support article 中描述的问题:
WHY IS MY BOT NO LONGER WORKING SUDDENLY?
When the new gateway update was deployed, bots which weren't adequately prepared to specify gateway intents could have experienced a variety of issues. Common symptoms include:
- The bot not responding anymore, despite being online
- The bot cache being empty (only seeing a few users across all guilds)
- A library on ready event not firing or timing out (happening mostly on discord.py and discord.js bots but other libraries might exhibit the same behavior)
If you are affected by this, this is because of 2 gateway changes:
- You no longer have access to disabled privileged intents if you are not specifying intents
- You can no longer request member info for multiple guilds at the same time
HOW DO I GET BACK UP AND RUNNING? UPDATING YOUR LIBRARY
First, you must make sure you're using a library version that can handle intents, and by extension no longer requests information on multiple guilds at once.
For discord.py, this means you need to update to v1.5 or higher.
For discord.js, this means you need to update to v12 or higher.
If you're unsure how to upgrade your bot to a new library release or are unclear regarding the intent compatibility of an alternative library, please see the library links provided in our Developer Community Resources.
SPECIFYING GATEWAY INTENTS
Please note that a variety of our gateway intents are not privileged, meaning they do not require specific switches to be flipped or whitelisted access to be requested. You can review the entirety of our available intents here.
Specifying which intents you'd like to receive varies based on your chosen library. Documentation on how to specify gateway intents in discord.js is available here. Documentation for specifying gateway intents in discord.py is available here.
所以,您需要先更新JDA。我建议您为此使用最新版本。您可以在此处查看最新版本代码:
之后,您应该像这样更改 in the README of JDA 定义的 JDABuilder 构造函数的使用:
JDABuilder builder=JDABuilder.createDefault("YOUR_TOKEN_HERE");
如果您需要特权网关意图,您还需要在开发者页面和您的应用程序中启用它们。