本文介绍了检查反应用户是否不适用于特定用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试仅对执行!pages命令的特定用户更改bot反应,我尝试了message.author和reaction.message.author == message.author,但没有成功!

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..

这是代码

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')

推荐答案

您可能需要对照 ctx.author ( reaction.message.author将是您的机器人).这意味着我们在创建支票时需要通过作者:

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))

这篇关于检查反应用户是否不适用于特定用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 11:18