0

So, I found this question I want to make a multi-page help command using discord.py But the answer make me a doubt. I tried a similar code to just try this thing, but while the loop is going on I literally can't use any of the other commands. What should I do in order to permit all the commands work while the loop is going on?

import discord
import datetime
from datetime import timedelta
import pytz
from discord.ext import commands

client = discord.Client()

client = commands.Bot(command_prefix = "!")

prefix = "!"

id = client.get_guild(766402039495262218)


@client.event
async def on_ready():
    print("Bot is ready")
    game = discord.Game("Could I Destroy a Server...?")
    await client.change_presence(status=discord.Status.idle, activity=game)

@client.event
async def on_message(message):
    await client.process_commands(message)


@client.event
async def on_reaction_add(reaction, user):
    channel = reaction.message.channel
    if reaction.emoji == '➡':
        if reaction.message.embeds == discord.embeds.Embed(title='Corsi Informatica I Anno'):
            await channel.send("ok")
            print("ok")
        else:
            print(reaction.message.embeds)
            print("not ok")
    else:
        await channel.send("YOLO")

@client.command(pass_context = True)
async def test(ctx):

    await ctx.message.channel.send("this is a testing test")

@client.command(pass_context=True)
async def corsi(ctx):

    page = 1
    left_arrow = '⬅'
    right_arrow = '➡'
    first_year = discord.Embed(
        title = 'Corsi Informatica I Anno',
        description = 'Lista dei corsi:',
        colour = discord.Colour.dark_blue()
    )

    second_year = discord.Embed(
        title = 'Corsi Informatica II Anno',
        description = 'Lista dei corsi:',
        colour = discord.Colour.dark_green()
    )

    third_year = discord.Embed(
        title = 'Corsi Informatica III Anno',
        description = 'Lista dei corsi:',
        colour = discord.Colour.dark_red()
    )

    first_year.set_footer(text = datetime.datetime.now().strftime('%d/%m/%Y %H:%M') + "\t\t\t\t\t\t\t\t\t\t\t\t\t" + "pag." + " " + str(page)+"/3")
    first_year.set_author(name = ctx.message.author)
    first_year.add_field(name = 'Calcolo I', value = 'I Semestre', inline = True)
    first_year.add_field(name = 'Algebra Lineare', value='I Semestre', inline = True)
    first_year.add_field(name ='Programmazione I', value='I Semestre', inline=True)
    first_year.add_field(name='--------------------------------------------------------------------------',value="**--------------------------------------------------------------------------**"
                         ,inline=False)
    first_year.add_field(name ='Elaboratori I', value='I Semestre', inline = True)
    first_year.add_field(name ='Matematica Discreta', value='I Semestre', inline = True)
    first_year.add_field(name ='Programmazione II', value='II Semestre', inline = True)
    first_year.add_field(name='--------------------------------------------------------------------------',value="**--------------------------------------------------------------------------**",
                         inline=False)
    first_year.add_field(name ='Elaboratori II', value='II Semestre', inline = True)
    first_year.add_field(name ='Calcolo II', value='II Semestre', inline = True)
    first_year.add_field(name ='Inglese', value='II Semestre', inline = True)

    second_year.set_footer(text = datetime.datetime.now().strftime('%d/%m/%Y %H:%M') + "\t\t\t\t\t\t\t\t\t\t\t\t\t" + "pag." + " " + str(page)+"/3")
    second_year.set_author(name = ctx.message.author)

    third_year.set_footer(text = datetime.datetime.now().strftime('%d/%m/%Y %H:%M') + "\t\t\t\t\t\t\t\t\t\t\t\t\t" + "pag." + " " + str(page)+"/3")
    third_year.set_author(name = ctx.message.author)


    while True:
        if page == 1:
            sent = await ctx.message.channel.send(client.get_channel("795387716967333889"), embed=first_year)
            await sent.add_reaction(right_arrow)
            page = 4
            # Do not do nothing until user use a reaction
        elif page == 2:
            sent = await ctx.message.channel.send(client.get_channel("795387716967333889"), embed=second_year)
            await sent.add_reaction(left_arrow)
            await sent.add_reaction(right_arrow)
        elif page == 3:
            sent = await ctx.message.channel.send(client.get_channel("795387716967333889"), embed=third_year)
            await sent.add_reaction(left_arrow)
Skel
  • 33
  • 7
  • Please provide an example, or "attempted code" we can help you by working off of – Cohen Feb 02 '21 at 23:11
  • I edited the post, for example if i write !corsi, it will send me the first page (then i set page = 4) to "block" the loop and dont permit it to send infinite messages. But during it if a try to write !test it will not send me the message. (I know the reaction check isnt working, i didnt implement it at the moment, I just wanted to test the loop) – Skel Feb 02 '21 at 23:13

1 Answers1

0

Instead of using a while True loop that blocks your processing, you should make use of the on_reaction_add event to watch for added reactions. You can identify the embed by checking the embed title & embed poster (your bot).

Documentation can be found here here

Kelo
  • 1,783
  • 2
  • 9
  • 21
  • Can I add "@client.event async def on_reaction_add": inside the "async def corsi(ctx):" block? If I add it outside it seems working bad – Skel Feb 03 '21 at 13:13
  • If I add on_reaction_add event outside the block, And if I try to react to the bot's message it won't do nothing. I tried to just print on console to see what happens. But if I add it inside the commands block, it seems work. But I dont know if I can do due """ python rules """ – Skel Feb 03 '21 at 13:18
  • I edited the question: Console prints me this ` [] not ok – Skel Feb 03 '21 at 13:47
  • You're comparing with an empty embed with a title. Take the embed and use `if reaction.message.embeds[0].title == "Corsi Informatica I Anno":` – Kelo Feb 03 '21 at 14:05
  • How Can I modify the embed with one how the other two if they arent in that block? – Skel Feb 03 '21 at 14:26
  • use the message object to edit the message contents with the new embed. – Kelo Feb 03 '21 at 14:26
  • But should I rewrite the second_year embed and third_year embed inside the on_reaction_add function? – Skel Feb 03 '21 at 14:33
  • You could, or you could define them outside a function as global variables, so you don't need to repetitively write them & it allows for easy editing. – Kelo Feb 03 '21 at 14:58