未知问题反应检测不合格

时间:2020-05-29 19:54:53

标签: python discord.py

我正在编写命令,通过让角色对消息做出反应来向用户添加角色。这是我的代码:

@BOT.command(pass_context = True , name = "join_game")
async def join_game(context):
    roles = {"minecraft" : "minecraft" , "AC" : "animal crossing" , "pokemon" : "pokemon"} #"emoji_name" : "role_name" , ...
    roleEmojis = {get(context.message.guild.emojis, name = emoji_name) : get(context.message.guild.roles , name = role_name) for emoji_name , role_name in zip(roles , roles.keys())}
    reactions = []
    msg = await context.message.channel.send("react with which game(s) you would like to be a part of.\n**press the :OK: when you are done**")

    for emoji in roleEmojis.keys():
        await msg.add_reaction(emoji)
    await msg.add_reaction(get(context.message.guild.emojis, name = "OK"))

    while True:
        reaction , user = await BOT.wait_for("reaction_add" , check = lambda react , user : True if react in [emoji for emoji in roleEmojis.keys()] and user == BOT.user else False) 
        if reaction == get(context.message.guild.emojis, name = "OK"):
            continue
        else:
            reactions.append(reaction)

    print(reactions)

    #add roles

一切正常,没有错误,但是当我添加反应时它不会检测到,因为没有输出,而while循环永远持续下去而不会中断。我相信这与我的检查功能有关,但老实说,我不确定。并不是说这是我第一次尝试第一个机器人,但是我可能错过了一些非常明显的东西。

1 个答案:

答案 0 :(得分:0)

您应该尝试使用客户端事件on_reaction_add

(我假设您已经在代码中的其他位置定义了get()

@BOT.command(pass_context = True , name = "join_game")
async def join_game(context):
    roles = {"minecraft" : "minecraft" , "AC" : "animal crossing" , "pokemon" : "pokemon"} #"emoji_name" : "role_name" , ...
    roleEmojis = {get(context.message.guild.emojis, name = emoji_name) : get(context.message.guild.roles , name = role_name) for emoji_name , role_name in zip(roles , roles.keys())}
    reactions = []
    msg = await context.message.channel.send("react with which game(s) you would like to be a part of.\n**press the :OK: when you are done**")

@BOT.event
async def on_reaction_add(reaction, user):

    # check if the user did the command #
    message = reaction.message
    if reaction.emoji == get(message.guild.emojis, name='minecraft'):
        role_to_assign = message.guild.get_role(ID_OF_MINECRAFT_ROLE_GOES_HERE)
    elif reaction.emoji == get(message.guild.emojis, name='AC'):
        role_to_assign = message.guild.get_role(ID_OF_ANIMALCROSSING_ROLE_GOES_HERE)
    elif reaction.emoji == get(message.guild.emojis, name='pokemon'):
        role_to_assign = message.guild.get_role(ID_OF_POKEMON_ROLE_GOES_HERE)
    elif reaction.emoji == get(message.guild.emojis, name='OK'):
        # do stuff to clean up (no more roles assigned) #
        return
    await user.add_role(role_to_assign)