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

[Bug] Button Command within cogs #58

Closed FeelsBadMan1 closed 3 years ago

FeelsBadMan1 commented 3 years ago

Hello, i now sucessfully made buttons working in the mainfile. now im trying to get it into cogs and used a examplecode in the examplefolder on this githubpage. However i get a dumb issure. I will provide the code im using and the error that appears when running the command below:

Ignoring exception in on_socket_response
Traceback (most recent call last):
  File "D:\Downloads\Python\python 3.8.7\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "D:\Downloads\Python\python 3.8.7\lib\site-packages\discord_components\client.py", line 79, in on_socket_response
    event = self._events.get(res["d"]["data"]["component_type"], None)
KeyError: 'component_type'
An exception has occurred while executing command `button`:
Traceback (most recent call last):
  File "D:\Downloads\Python\python 3.8.7\lib\site-packages\discord_slash\client.py", line 744, in invoke_command
    await coro
  File "D:\Downloads\Python\python 3.8.7\lib\site-packages\discord_slash\model.py", line 227, in invoke
    return await self.func(self.cog, *args, **kwargs)
  File "D:\my_python_projects\MongoDB Test\Cogs\music.py", line 537, in buttontest
    await ctx.send(
TypeError: send() got an unexpected keyword argument 'components'
    @cog_ext.cog_slash(name="button", guild_ids=slash_ids)
    async def buttontest(self, ctx):
        await ctx.send(
            "Here is an example of a button",
            components=[
                [
                    Button(style=ButtonStyle.green, label="GREEN"),
                    Button(style=ButtonStyle.red, label="RED"),
                    Button(style=ButtonStyle.grey, label="GREY", disabled=True),
                ],
                Button(style=ButtonStyle.blue, label="BLUE"),
                Button(style=ButtonStyle.URL, label="URL", url="https://www.example.com"),
            ],
        )
Shaqalito commented 3 years ago

This happens because you are using slash commands and the send() of the SlashContext(Slash command's ctx) doesn't support the "components" keyword (it's not implemented), but I found a workaround:

Send an empty message before the actual message with components.

Get the ctx from the empty message you sent, and use it to send your actual message with your components.

Here is an example that comes from my code:

msg = await ctx.send("᲼") #Send the empty message
ctx = await client.get_context(msg) #Get the Context(ctx) from it

em= Embed(title= "Muted Users", description= final_list, color= 0xFF5BF8)

await ctx.send(embed= em, components=[ #Use the ctx that you got from the empty message.
  Button(label= "Unmute All", style=4, id="clear_muted_button"),
])
PythonSerious commented 3 years ago

@FeelsBadMan1 Hello, I wrote the example cog. You are using slash context not discord context which is what we hook into at the end, I don't know if you are trying to use slash commands But I'm pretty sure slash commands don't support buttons. If they do try using DiscordComponents(slash) rather than DiscordComponents(bot)!

Image

FeelsBadMan1 commented 3 years ago

Hello, I tried both versions. on the version from @Shaqalito it print me following issure:

An exception has occurred while executing command `button`:
Traceback (most recent call last):
  File "D:\Downloads\Python\python 3.8.7\lib\site-packages\discord_slash\client.py", line 744, in invoke_command
    await coro
  File "D:\Downloads\Python\python 3.8.7\lib\site-packages\discord_slash\model.py", line 227, in invoke
    return await self.func(self.cog, *args, **kwargs)
  File "D:\my_python_projects\MongoDB Test\Cogs\music.py", line 540, in buttontest
    ctx = await bot.get_context(msg)  # Get the Context(ctx) from it
  File "D:\Downloads\Python\python 3.8.7\lib\site-packages\discord\ext\commands\bot.py", line 883, in get_context
    if self._skip_check(message.author.id, self.user.id):
AttributeError: 'NoneType' object has no attribute 'id'

on the second version from @PythonSerious follwoing error is being raised:

An exception has occurred while executing command `button`:
Traceback (most recent call last):
  File "D:\Downloads\Python\python 3.8.7\lib\site-packages\discord_slash\client.py", line 744, in invoke_command
    await coro
  File "D:\Downloads\Python\python 3.8.7\lib\site-packages\discord_slash\model.py", line 227, in invoke
    return await self.func(self.cog, *args, **kwargs)
  File "D:\my_python_projects\MongoDB Test\Cogs\music.py", line 540, in buttontest
    await ctx.send(
TypeError: send() got an unexpected keyword argument 'components'

My current code within the cog:

    @cog_ext.cog_slash(name="button", guild_ids=slash_ids)
    async def buttontest(self, ctx):
        await ctx.send(
            "Here is an example of a button",
            components=[
                [
                    Button(style=ButtonStyle.green, label="GREEN"),
                    Button(style=ButtonStyle.red, label="RED"),
                    Button(style=ButtonStyle.grey, label="GREY", disabled=True),
                ],
                Button(style=ButtonStyle.blue, label="BLUE"),
                Button(style=ButtonStyle.URL, label="URL", url="https://www.example.com"),
            ],
        )

I use DiscordComponents(slash) within the Cogs.

kiki7000 commented 3 years ago

Can't use discord-py-slash-commands and this library both

Shaqalito commented 3 years ago

@FeelsBadMan1

How are you catching the interaction?

You would need to do something like this:

res = await client.wait_for("button_click")
#Your user would be
user = res.user #This gives you a Member Object

Or maybe you're catching the wrong type try also using

(ctx.author.id, self.res.user.id) 

The error is not from the slash or components library, it's a python error in your skip_check function. It means it's on your end. Try what I sent you and tell me if it works

Shaqalito commented 3 years ago

@kiki7000 Yes there is a way if you want to know what I did add me on discord Shaqalito#2110 also I'm on the server

FeelsBadMan1 commented 3 years ago

Can't use discord-py-slash-commands and this library both

I also tried it with discord.py but then the bot is not responsing and i not get any error.

FeelsBadMan1 commented 3 years ago

Hello, i got it almost working. I just dont know how i can check using a ifstatement in a command if a button is clicked. There is a examplecode event but this run everytime someone cklicks it with the same answer you definied within the event. but now i want to create different buttons with different responses to each differnt command. i really tried lots of things with ifstatements etc. but none of those helped me with that. it would be great, if someone is so kind and get a solution for me.

Shaqalito commented 3 years ago

Hello, i got it almost working. I just dont know how i can check using a ifstatement in a command if a button is clicked. There is a examplecode event but this run everytime someone cklicks it with the same answer you definied within the event. but now i want to create different buttons with different responses to each differnt command. i really tried lots of things with ifstatements etc. but none of those helped me with that. it would be great, if someone is so kind and get a solution for me.

Dm me on discord