英文:
JDA Discord - How to get all names of Users who reacted to a bot's message in an array
问题
我正在尝试编写一个抽奖机器人。我遇到了问题,无法获取对机器人发送的抽奖消息做出反应的所有用户的用户名,并将它们存储在一个类型为String
的Array
中。
英文:
I am trying to code a giveaway bot. I am having problems getting all Usernames of Users who reacted to the Giveaway Message that have been sent by the bot and store them in a Array
of Type String
.
答案1
得分: 1
你可以通过获取所在频道的历史记录并根据消息的ID从历史记录中获取消息的“Message”对象。
“Message”对象有一个“getReactions”方法,它返回消息上所有反应的列表,然后你可以遍历这些反应,然后再遍历给定反应上的所有用户,并将每个反应的用户名/ID保存到你选择的数组、列表或数据结构中。
英文:
You can get the Message
object of the message by getting the history of the channel its in and then get the message by id from the history.
The Message
object has a getReactions
method that returns a list of all reactions on a message, you can then iterate through the reactions and in turn then iterate thru all the users that reacted on the given reaction and save the name/id of each user who reacted into an array or list or data structure of your choice.
答案2
得分: 0
List<MessageReaction> reactionsList = event.event.getMessage().getReactions();
// List of users that reacted to every possible emote.
for (int i = 0; i < reactionsList.size(); i++)
{
List<User> users = reactionsList.get(i).retrieveUsers().complete();
}
英文:
List<MessageReaction> reactionsList = event.event.getMessage().getReactions();
// List of users that reacted to every possible emote.
for (int i = 0; i < reactionsList.size(); i++)
{
List<User> users = reactionsList.get(i).retrieveUsers().complete();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论