woohyunjng / discord.py-components

An unofficial third-party library of discord.py for discord components.
https://devkiki7000.gitbook.io/discord-components/
MIT License
128 stars 43 forks source link

Add example a command(buttons navigations) with while and check #52

Closed helish88 closed 3 years ago

helish88 commented 3 years ago

PR TYPE

Why did you open this PR (If it is other, please write a more detailed description.)

  • [x] New feature
  • [ ] Fix bug
  • [ ] Typo
  • [ ] Other

Checks

kiki7000 commented 3 years ago

It would be better if you use break instead of stop

helish88 commented 3 years ago

Agree. ill change

kiki7000 commented 3 years ago

Could you divide the file? I think it will crash because of on_button_click

helish88 commented 3 years ago

image code


@bot.command()
async def buttons(ctx):
    """Buttons example of navigation with while and with check"""
    msg = await ctx.channel.send(embed=discord.Embed(color=discord.Color.random(), title="Navigation"),
                                 components=[
                                   [Button(style=ButtonStyle.blue, label="Backward", emoji="⏮️"),
                                    Button(style=ButtonStyle.red, label="Stop", emoji="❌"),
                                    Button(style=ButtonStyle.blue, label="Forward",  emoji="⏭️")]])
    def check(m):
        return m.user == ctx.author and m.message.channel == ctx.channel
    while True:
        try:
            res = await bot.wait_for("button_click", check=check, timeout=60)
        except asyncio.TimeoutError:
            await msg.delete()
            await ctx.send(embed=discord.Embed(color=discord.Color.red(),
                                                      title="Timeout"))
            break
        if res.channel == ctx.channel:
            if res.component.label == "Forward":
                await msg.edit(embed=discord.Embed(color=discord.Color.random(),
                                                   title="Forward"))
                await res.respond(
                    type=InteractionType.DeferredUpdateMessage
                )
            elif res.component.label == "Backward":
                await msg.edit(embed=discord.Embed(color=discord.Color.random(),
                                                   title="Backward"))
                await res.respond(
                    type=InteractionType.DeferredUpdateMessage
                )
            elif res.component.label == "Stop":
                await msg.edit(embed=discord.Embed(color=discord.Color.random(),
                                                   title="Stop"))
                await res.respond(
                    type=InteractionType.UpdateMessage
                )
                await msg.delete()```
helish88 commented 3 years ago

and wdym Could you divide the file? this is one command

kiki7000 commented 3 years ago

https://github.com/kiki7000/discord.py-components/pull/56#issuecomment-850964598

kiki7000 commented 3 years ago

Why open two PRs