【问题标题】:Check reaction pagination button检查反应分页按钮
【发布时间】:2019-04-03 02:14:59
【问题描述】:

我有一个命令可以通过 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)

但是当你以外的其他人点击按钮时,命令起作用,这不应该发生,只有执行命令的人才能点击按钮,如谓词检查所示,应该怎么做?

【问题讨论】:

    标签: python bots discord discord.py


    【解决方案1】:

    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

    【讨论】:

      猜你喜欢
      • 2016-07-29
      • 2018-11-09
      • 1970-01-01
      • 2019-01-18
      • 1970-01-01
      • 2017-12-17
      • 1970-01-01
      • 2018-11-11
      • 1970-01-01
      相关资源
      最近更新 更多