检查反应分页按钮

时间:2018-10-30 04:36:16

标签: python bots discord discord.py

我有一个命令,可以通过2个按钮来回移动图像序列:

left = '⏪'
right = '⏩'

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:
            return True
        if r and reaction.emoji == right:
            return True
        return False

    return check

一些东西

mmsg = ("1.png", "2.png", "3.png", "4.png")

index = 0
while True:
    msg = await Bot.send_file(ctx.message.channel, mmsg[index])
    l = index != 0
    r = index != len(mmsg) - 1
    if l:
        await Bot.add_reaction(msg, left) 
    if r:
        await Bot.add_reaction(msg, right)
    Bot.wait_for_reaction
    reaction, ctx.message.author = await Bot.wait_for_reaction(check=predicate(msg, l, r))
    if reaction.emoji == left:
        index -= 1
    elif reaction.emoji == right:
        index += 1
    await Bot.delete_message(msg)

但是,当您单击该按钮以外的其他按钮时,该命令有效,这是不应该发生的,只有执行该命令的用户才能单击该按钮,如谓词检查中所示,应该怎么做?

1 个答案:

答案 0 :(得分:0)

predicate检查不会对此进行检查。它只是检查反应是否来自机器人。您可以在user上添加一个wait_for_reaction参数,以仅接受来自该ctx.message.author的反应:

mmsg = ("1.png", "2.png", "3.png", "4.png")

index = 0
while True:
    msg = await Bot.send_file(ctx.message.channel, mmsg[index])
    l = index != 0
    r = index != len(mmsg) - 1
    if l:
        await Bot.add_reaction(msg, left) 
    if r:
        await Bot.add_reaction(msg, right)
    reaction, _ = await Bot.wait_for_reaction(check=predicate(msg, l, r), user=ctx.message.author)
    if reaction.emoji == left:
        index -= 1
    elif reaction.emoji == right:
        index += 1
    await Bot.delete_message(msg)

您还正在分配给我删除的ctx.message.author。没有理由分配给Context