反应分页按钮前进和后退python

Posted

技术标签:

【中文标题】反应分页按钮前进和后退python【英文标题】:Reaction pagination button forward and back python 【发布时间】:2019-01-18 15:07:13 【问题描述】:

我正在尝试制作一个按钮/反应来在 3 个不同的图像之间来回切换,但按钮在前一个图像中没有返回,只是前进到下一个图像,有人可以帮我吗?

    if message.content.startswith('!image'):

        msg1 = await Bot.send_file(message.channel, 'image1.png')

        toReact = ('⏩')
        for reaction in toReact:
            await Bot.add_reaction(msg1, reaction)
        def checkReaction(reaction, user):
            e = str(reaction.emoji)
            return e.startswith('⏩')
        res = await Bot.wait_for_reaction(message=msg1, user=message.author, timeout=30, check=checkReaction)

        if res is None:
            await Bot.delete_message(msg1)

        elif '⏩' in str(res.reaction.emoji):
            await Bot.delete_message(msg1)
            msg2 = await Bot.send_file(message.channel, 'image2.png')

            toReact = ['⏪', '⏩']
            for reaction in toReact:
                await Bot.add_reaction(msg2, reaction)
            def checkReaction2(reaction, user):
                e = str(reaction.emoji)
                return e.startswith('⏪','⏩')
            res2 = await Bot.wait_for_reaction(message=msg2, user=message.author, timeout=30, check=checkReaction2)

            if res2 is None:
                await Bot.delete_message(msg2)

            elif '⏩' in str(res.reaction.emoji):
                await Bot.delete_message(mensagem2)
                await Bot.send_file(message.channel, 'image3.png')

【问题讨论】:

***.com/q/55075157/11146632 【参考方案1】:

这就是我想出的。

基本上,我们有一个循环来检查每个反应与我们想要的反应,然后如果我们看到我们正在寻找的反应之一,则删除旧消息并发送新消息。

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

    return check


@bot.command(pass_context=True)
async def series(ctx):
    index = 0
    while True:
        msg = await bot.say(messages[index])
        l = index != 0
        r = index != len(messages) - 1
        if l:
            await bot.add_reaction(msg, left) 
        if r:
            await bot.add_reaction(msg, right)
        # bot.wait_for_reaction
        react, user = await bot.wait_for_reaction(check=predicate(msg, l, r))
        if react.emoji == left:
            index -= 1
        elif react.emoji == right:
            index += 1
        await bot.delete_message(msg)

bot.run("TOKEN")

有人要求编辑消息而不是发送新消息的版本,我也将其更新到最新版本:

@bot.command(pass_context=True)
async def series(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:
            index -= 1
        elif react.emoji == right:
            index += 1
        action = msg.edit

【讨论】:

对不起,我不知道重写,只是让我更加困惑 @LucasTesch 我已经进行了必要的修改以在异步分支上执行此代码 @PatrickHaugh while 循环何时结束?这对服务器来说不是很糟糕吗? @echan00 这是在事件循环中运行的异步代码。当它到达await bot.wait_for 时,控制权会传递给在该事件循环中运行的其他异步函数,直到用户按下它正在等待的按钮之一,此时它会运行循环的另一次迭代。 @PatrickHaugh 如果没有用户按下按钮怎么办?

以上是关于反应分页按钮前进和后退python的主要内容,如果未能解决你的问题,请参考以下文章

Gatsby.js:使用 URL 参数和浏览器后退/前进按钮导航

全球css中的反应分页样式不起作用

JS分页条

vue中通过history api拦截浏览器的前进后退按钮事件

用iframe后 点后退按钮时总是在潜套框里后退,如何让整个父页面整体后退呢?

lumen 6:如何在 lumen 上重构响应分页的数据?