英文:
JDA - How to get the user ID of the bot itself
问题
我正在制作一个使用JDA(Java Discord API)的程序,需要检查消息是否是由机器人自己发送的。我该如何实现这一点?我考虑过通过检查消息发送者的用户ID是否等于机器人的用户ID来实现,但是在程序中如何获取机器人自身的用户ID呢?
英文:
I'm making a JDA (Java Discord API) program that needs to check if a message was sent by the bot itself. How can I achieve this? I thought about checking if the user ID of the message sender equals the bot's user ID, but how can I get the user ID of the bot itself in the program?
答案1
得分: 1
If you use the MessageReceivedEvent, you can just check if the sender is a bot by using:
event.getAuthor.isBot()
.
You can access the user of the bot itself by accessing JDA and calling getSelfUser() as follows with the MessageReceivedEvent in mind:
event.getJDA().getSelfUser()
On the SelfUser you can call SelfUser#getId()
or SelfUser#getIdLong()
to access the ID.
Example Code:
public class Listener extends ListenerAdapter {
@Override
public void onMessageReceived(MessageReceivedEvent event) {
boolean isBot = event.getAuthor().isBot(); // 检查消息发送者是否为机器人
long id = event.getJDA().getSelfUser().getIdLong(); // 机器人的ID
}
}
英文:
If you use the MessageReceivedEvent, you can just check if the sender is a bot by using:
event.getAuthor.isBot()
.
You can access the user of the bot itself by accessing JDA and calling getSelfUser() as follows with the MessageReceivedEvent in mind:
event.getJDA().getSelfUser()
On the SelfUser you can call SelfUser#getId()
or SelfUser#getIdLong()
to access the ID.
Example Code
public class Listener extends ListenerAdapter {
@Override
public void onMessageReceived(MessageReceivedEvent event) {
boolean isBot = event.getAuthor().isBot() //Check if the Message Sender is a bot
long id = event.getJDA().getSelfUser().getIdLong() //the bot ID
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论