反应分页按钮前进和后退python [英] Reaction pagination button forward and back python

查看:131
本文介绍了反应分页按钮前进和后退python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在3个不同的图像之间来回切换按钮,但该按钮不会在上一个图像中回来,只是前进到下一个图像,有人可以帮我吗?

I'm trying to make a button / reaction to go back and forth between 3 different images, but the button is not coming back in the previous image, just advancing to the next one, could anyone help me?

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

推荐答案

这就是我的想法.

基本上,我们有一个循环,可根据所需的反应检查每个反应,然后删除旧消息并发送新的消息(如果我们看到所需的反应之一).

Basically, we have a loop that checks every reaction against the ones we want, and then delete the old message and send the new one if we see one of the reactions we're looking for.

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

有人要求提供一种用于编辑消息的版本,而不是发送新消息,我还已将其更新为最新版本:

Someone asked for a version that edits the message instead of sending a new one, I've also updated it to the latest version:

@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

这篇关于反应分页按钮前进和后退python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆