Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Choice Reaction python (discord.py)

Tags:

python

discord

I'm trying to do a multiple choice button / reaction in discord using python (discord.py) -- something similar to the image below:

Multiple choice reaction discord example image

For example when it reacts to 2️⃣, that shows page 2, when it reacts to 3️⃣, that shows page 3 ... Could anyone help me, please?

import discord
from discord.ext import commands

class Wiki(commands.Cog):
    def __init__(self,bot):
        self.bot=bot

    #commands
    @commands.command(name="wiki",aliases=["w"])
    async def wiki(self,ctx):
        page1=discord.Embed(
        title='Page 1/3',
        description='Description1',
        colour=discord.Colour.orange()
        )
        page2=discord.Embed(
        title='Page 2/3',
        description='Description2',
        colour=discord.Colour.orange()
        )
        page3=discord.Embed(
        title='Page 3/3',
        description='Description3',
        colour=discord.Colour.orange()
        )
        pages=[page1,page2,page3]

        message= await ctx.send(embed=page1)

        await message.add_reaction('1️⃣')
        await message.add_reaction('2️⃣')
        await message.add_reaction('3️⃣')
        
        emoji=""
        if emoji=="1️⃣":
            await message.edit_message(message,embed=pages[0])
        if emoji=="2️⃣":
            await message.edit_message(message,embed=pages[1])
        if emoji=="3️⃣":
            await message.edit_message(message,embed=pages[2])       

def setup(bot):
    bot.add_cog(Wiki(bot))
like image 344
ivansaul Avatar asked May 01 '26 19:05

ivansaul


2 Answers

Take a look at the documentation for wait_for, which is how you should treat this kind of case. I won't spoonfeed you the code though, you should give it a fair shot yourself first.

Also, discord.py already has something built-in for embeds that change pages automatically based on reactions called menus, so it'll probably be easier to just use that instead of re-implementing it yourself.

like image 86
stijndcl Avatar answered May 03 '26 09:05

stijndcl


Answering my own question.

I know it's not the best answer, but it works XD. Any suggestion will be well accepted. Thank you all so much for your help.

import asyncio
import discord
from discord.ext import commands
class Wiki(commands.Cog):
    def __init__(self,bot):
        self.bot=bot
    
    #commands
    @commands.command(name="wiki",aliases=["w"])
    async def wiki(self,ctx):
        first_run = True
        while True:
            if first_run:
                page1=discord.Embed(title='Page 1/3',description='Description1',colour=discord.Colour.orange())
                first_run=False
                msg = await ctx.send(embed=page1)

                reactmoji = ["1️⃣","2️⃣","3️⃣"]
                
                for react in reactmoji:
                    await msg.add_reaction(react)

            def check_react(reaction, user):
                if reaction.message.id != msg.id:
                    return False
                if user != ctx.message.author:
                    return False
                if str(reaction.emoji) not in reactmoji:
                    return False
                return True
            
            try:
                res, user = await self.bot.wait_for('reaction_add', check=check_react)
            except asyncio.TimeoutError:
                return await msg.clear_reactions()

            if user != ctx.message.author:
                pass
            elif '1️⃣' in str(res.emoji):
                print('<<1️⃣>>')
                await msg.remove_reaction("1️⃣",user)
                await msg.edit(embed=page1)
            elif '2️⃣' in str(res.emoji):
                print('<<2️⃣>>')
                page2=discord.Embed(title='Page 2/3',description='Description2',colour=discord.Colour.orange())
                await msg.remove_reaction("2️⃣",user)
                await msg.edit(embed=page2)
            elif '3️⃣' in str(res.emoji):
                print('<<3️⃣>>')
                page3=discord.Embed(title='Page 3/3',description='Description3',colour=discord.Colour.orange())
                await msg.remove_reaction("3️⃣",user)
                await msg.edit(embed=page3)
            

def setup(bot):
    bot.add_cog(Wiki(bot))
like image 39
ivansaul Avatar answered May 03 '26 09:05

ivansaul