savioxavier/repo-finder-bot

TODO: Slash commands

Closed this issue ยท 8 comments

What's the status on slash commands?

I've been attempting to implement discord-interactions, but normal commands are not surviving: bot does not respond to any rf. commands, returning "command not found" message in Discord.


main.py:

...
# Tested self_bot=True but removed due to it not responding to rf. at all
client = commands.Bot(command_prefix=determine_prefix,
                      case_insensitive=True,
                      activity=activity,
                      intents=intents,
                      help_command=None,
                      status=Status.idle
                      )
slash = SlashCommand(client, sync_commands=True, sync_on_cog_reload=True)

...

# Having both decorators active at the same time throws two different exceptions:
# When @client is loaded first, "TypeError: Callback must be a coroutine"
# When @slash is loaded first, "TypeError: command_help() got an unexpected keyword 'kwargs'" and a "kwargs" argument is required while executing /help

@slash.slash(name="help", description="List commands", guild_ids=__GUILD_ID__)
# @client.command(name="help", description="List commands")
async def command_help(ctx: SlashContext):
    "Main help command for the bot"
    logger.debug(f"{ctx.author} - initiated help command")
...

The above code will respond to /help, but not rf.help

There was one instance where the bot would respond to both methods, but I cannot reproduce it. I believe it stopped after adding cog slash commands

Probably irrelevant, but

For documentation, tracebacks mentioned are as followed:

@client ...
@slash ...
Traceback (most recent call last):
  File "/Users/ventus/GitRepos/repo-finder-bot/main.py", line 120, in <module>
    async def command_help(ctx: SlashContext):
  File "/usr/local/lib/python3.9/site-packages/discord/ext/commands/core.py", line 1262, in decorator
    result = command(*args, **kwargs)(func)
  File "/usr/local/lib/python3.9/site-packages/discord/ext/commands/core.py", line 1433, in decorator
    return cls(func, name=name, **attrs)
  File "/usr/local/lib/python3.9/site-packages/discord/ext/commands/core.py", line 217, in __init__
    raise TypeError('Callback must be a coroutine.')
@slash ...
@client ...
An exception has occurred while executing command `help`:
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/discord_slash/client.py", line 1352, in invoke_command
    await func.invoke(ctx, **args)
  File "/usr/local/lib/python3.9/site-packages/discord_slash/model.py", line 210, in invoke
    return await self.func(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/discord/ext/commands/core.py", line 374, in __call__
    return await self.callback(*args, **kwargs)
TypeError: command_help() got an unexpected keyword argument 'kwargs'

@savioxavier you mentioned you were working on a prototype. Do you still have those remnants? Are you willing to upload it to a new branch?

I've been attempting to implement discord-interactions, but normal commands are not surviving: bot does not respond to any rf. commands, returning "command not found" message in Discord.

I've been getting the same errors, when trying to work on the slash commands inside cogs. It was exactly as you had said: a coroutine error when client commands (using @commands.command) are loaded first and a unexpected keyword argument when slash is loaded first.

I figured the only way to load both slash and regular commands was by duplicating the exact code from cogs, except converting them to slash commands by replacing @commands.command with @cog_ext.cog_slash Also, I found that certain variables such as ctx.message.author had to be converted to ctx.author as slash commands don't rely on any chat messages.

Later, I had a supposedly genius idea of doing something like this in order to reduce the amount of repetitive code:

class Meta(commands.Cog):
    async def command_main(self, ctx):
        # Main code

    @commands.command(name="info")
    async def regular_command(self, ctx):
        await self.command_main(ctx)

    @cog_ext.cog_slash(name="info", description="Get bot info", guild_ids=[DEV_GUILD])
    async def slash_command(self, ctx):
        await self.command_main(ctx)

Now, of course this didn't work because I got the same errors (due to loading regular before slash and vice versa) and due to the fact that certain stuff such as ctx.message.author don't exist when working with slash commands, I decided the only way to use them both was by creating an entire folder, named slashcogs with the same code as cogs, except with a few modifications.

@savioxavier you mentioned you were working on a prototype. Do you still have those remnants? Are you willing to upload it to a new branch?

I've kept this idea on hold for a few days, as I had other projects to work on, but I'll give it another go right now and see if it still works.

Also, as of commit 0b43e5f of goverfl0w/discord-interactions, it looks like we would have to use yet another package for slash commands and button interactions?

@V3ntus Branch slash-commands has been pushed right now. The code's a bit weird and repetitive, but I guess there are ways to fix that. How about making a folder called main at the root and importing files such as build_query.py, common.py etc. from there? It's currently importing them all from cogs itself.

Oh awesome

Yes, I do think folder structure should be reworked. I'll take a look

@V3ntus Updated the folder structure, added and modified a few more stuff. Works fine for me and I think it's ready for deployment. If you have any suggestions, please do tell them. else I'll merge the slash-commands branch, if that's alright.

How about making a folder called main at the root and importing files such as build_query.py, common.py etc. from there? It's currently importing them all from cogs itself.

Side note: I had to rename main to utils due to a circular error (as main.py existed at the root)

Works fine for me and I think it's ready for deployment.

Oh awesome. If you think it's ready, then merge away!

One thing about the folder structure. It seems repetitive to have a folder structure for commands as cogs/src/commands with the parent directories being empty. If we refine this, we'll have to of course alter the module imports in the required core files, as well as cogs/src/commands/__init__.py:

Removing a single dn() call should push the directory down by one. So dn(dn(sys,path[0])) might return repo-finder/ and dn(sys.path[0]) will return repo-finder/cogs/

...
    sys.path.insert(0, dn(dn(sys.path[0])))
...

Edit: maybe the parent path injection in __init__.py is not needed anymore if we do this change

This change linked to #20

Since slash commands have already been implemented, I guess it's time to close this issue.