slash commands with subclassed bot don't work
Summary
slash command decorator in subclassed bot doesn't work
Reproduction Steps
Try to implement a subclassed bot with a slash command as described here in the documentation https://guide.pycord.dev/popular-topics/subclassing-bots
I can't get it to work.
Minimal Reproducible Code
import discord
class MyBot(discord.Bot):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@discord.slash_command(name="test")
async def test(self, ctx: discord.context.ApplicationContext):
await ctx.respond("TEST")
bot = MyBot()
bot.run('token')
Expected Results
The commands should have been updated. They are not updated. See the sample code in the documentation https://guide.pycord.dev/popular-topics/subclassing-bots
I expect the /test command to work.
Actual Results
This is what I see in the logs. There should be a PUT to update the command list when my bot connects.
Dispatching event socket_event_type
Dispatching event guild_available
GET https://discord.com/api/v10/applications/<REDACTED>/commands with None has returned 200
GET https://discord.com/api/v10/applications/<REDACTED>/commands has received []
Skipping bulk command update: Commands are up to date
Dispatching event ready
Intents
defaults
System Information
- Python v3.10.5-final
- py-cord v2.0.0-final
- aiohttp v3.8.1
- system info: Windows 10 10.0.22622
Checklist
- [X] I have searched the open issues for duplicates.
- [X] I have shown the entire traceback, if possible.
- [X] I have removed my token from display, if visible.
Additional Context
I can get it working without subclassing the Bot class. This works...
import discord
bot = discord.Bot()
@bot.slash_command(name="test")
async def test(ctx):
await ctx.respond("TEST")
bot.run('token')
Because you're never adding the Slash Command to the bot's internal list of Slash Commands Sure, it's an attribute of the bot, but that pretty much means nothing
I am new to the library, and I was trying to follow the documentation. Is it possible to add it to the internal list of slash commands?
Yeah. According to the code, you can use discord.Bot.add_application_command to manually add in an Application Command into the internal list. If you loop through every attribute of the custom bot class in __init__ then you can add them all in.
I am also able to get it working using a Cog like this.
class MyCog(discord.Cog):
@discord.slash_command(name="test")
async def test(self, ctx):
await ctx.respond("TEST")
class MyBot(discord.Bot):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.add_cog(MyCog())
I am also able to get it working using a Cog like this.
class MyCog(discord.Cog): @discord.slash_command(name="test") async def test(self, ctx): await ctx.respond("TEST") class MyBot(discord.Bot): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.add_cog(MyCog())
Cogs perform the process I described but automatically once you add them to the bot.
Okay then my main issue is with the documentation. If you look at the documentation I linked in the OP, it looks very easy to just add "@slash_command()" to any member method and it should work.
I am perfectly fine with just updating that page of the docs to use a Cog instead of that spurious @slash_command().
This issue is pretty much a duplicate of what I experienced at https://github.com/Pycord-Development/pycord/issues/1479 - the issue lies with the docs providing an incorrect example.
Guide fixed in guide@4a75192