Skip to content Skip to sidebar Skip to footer

Reaction Pagination Button Forward And Back Python

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, cou

Solution 1:

Here's what I came up with.

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

defpredicate(message, l, r):
    defcheck(reaction, user):
        if reaction.message.id != message.idor user == bot.user:
            returnFalseif l and reaction.emoji == left:
            returnTrueif r and reaction.emoji == right:
            returnTruereturnFalsereturn check


@bot.command(pass_context=True)asyncdefseries(ctx):
    index = 0whileTrue:
        msg = await bot.say(messages[index])
        l = index != 0
        r = index != len(messages) - 1if 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 -= 1elif react.emoji == right:
            index += 1await 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)asyncdefseries(ctx):
    index = 0
    msg = None
    action = ctx.send
    whileTrue:
        res = await action(content=messages[index])
        if res isnotNone:
            msg = res
        l = index != 0
        r = index != len(messages) - 1if 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 -= 1elif react.emoji == right:
            index += 1
        action = msg.edit

Post a Comment for "Reaction Pagination Button Forward And Back Python"