Check reaction user not work for specific user?

风流意气都作罢 提交于 2021-02-11 04:55:28

问题


I'm trying to make the bot reaction only change for the specific user that do !pages command, I tried message.author and reaction.message.author == message.author but it didn't work!

The issue is that when someone used this command, it will also worked for others which is not what I expected..

Here's the code

from discord.ext import commands

bot = commands.Bot(command_prefix='!')

left = '⏪'
right = '⏩'

messages = ("1", "2", "3")

def predicate(message, l, r):
    def check(reaction, user):
        if reaction.message.id != message.id or user == bot.user:
            return False
        if l and reaction.emoji == left and reaction.message.author == message.author:
            print('Left')
            return True
        if r and reaction.emoji == right and reaction.message.author == message.author:
            print('Right')
            return True
        return False

    return check


@bot.command(pass_context=True)
async def pages(ctx):
    index = 0
    msg = None
    action = ctx.send
    while True:
        res = await action(content=messages[index])
        if res is not None:
            msg = res
        l = index != 0
        r = index != len(messages) - 1

        if l:
            await msg.add_reaction(left)
        if r:
            await msg.add_reaction(right)
        react, user = await bot.wait_for('reaction_add', check=predicate(msg, l, r))
        if react.emoji == left and user == ctx.author:
            index -= 0
            await msg.delete()
            print(f'Left {index}')
            action = ctx.send
        elif react.emoji == right and user == ctx.author:
            index += 1
            await msg.delete()
            print(f'Right {index}')
            action = ctx.send
bot.run('token')

回答1:


You can need to check user against ctx.author (reaction.message.author will be your bot). That means we need to pass the author when we create the check:

def predicate(message, l, r, author):
    def check(reaction, user):
        if author.id != user.id:
            return False
        if reaction.message.id != message.id or user == bot.user:
            return False
        if l and reaction.emoji == left:
            return True
        if r and reaction.emoji == right:
            return True
        return False

    return check

react, user = await bot.wait_for('reaction_add', check=predicate(msg, l, r, ctx.author))


来源:https://stackoverflow.com/questions/58286840/check-reaction-user-not-work-for-specific-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!