Unable to access decorators from Bot class when subclassing
Summary
Using the @slash_command() decorator in a subclass of discord.Bot does not work properly.
Reproduction Steps
I am on version 2.0.0rc1.
I was following the tutorial for subclassing bots at guide.pycord.dev/popular-topics/subclassing-bots, however, I was unable to implement slash commands. The tutorial states that a developer may use the slash_command decorator inside the subclass to add a slash command to the bot, however, this fails with a SyntaxError as the decorator is not defined upon simply importing discord but is under discord.Bot.slash_command.
I tried replacing @slash_command() with @discord.Bot.slash_command(), however this failed as the decorator was expecting an argument for self. I am not sure if this is an oversight by the tutorial, but I am not able to apply the @slash_command decorator in a subclass in any capacity.
Minimal Reproducible Code
# This code is simply copy-pasted from the tutorial at
# https://guide.pycord.dev/popular-topics/subclassing-bots
import discord
from dotenv import load_dotenv
import os
load_dotenv()
class MyBot(discord.Bot): # subclass discord.Bot
async def on_ready(self): # override the on_ready event
print('Logged in as')
print(self.user.name)
print(self.user.id)
print('------')
@slash_command() # Alternatively, @discord.Bot.slash_command()
async def ping(self, ctx):
await ctx.respond('Pong!')
bot = MyBot()
bot.run(os.getenv("TOKEN"))
Expected Results
For the bot to run and for the slash command to be enabled.
Actual Results
Traceback (most recent call last):
File "<my file location>", line 7, in <module>
class MyBot(discord.Bot): # subclass discord.Bot
File "<my file location>", line 14, in MyBot
@slash_command() # create a slash command
NameError: name 'slash_command' is not defined
Intents
No intents specified - I am using the default intents supplied by the constructor.
System Information
- Python v3.9.6-final
- py-cord v2.0.0-candidate
- py-cord pkg_resources: v2.0.0rc1
- aiohttp v3.7.4.post0
- system info: Windows 10 10.0.19043
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'm unsure if this is an issue with the tutorial, and developers are meant to outline slash commands in subclasses in a different manner.
Edit 7/09/2022: I found this SO post that seems to describe something similar to what is being attempted here: https://stackoverflow.com/questions/3001138/python-decorators-and-inheritance
I don't believe creating commands in a Bot subclass like that is supported by the library, you'd have to implement your own stuff for that.
Additionally, slash_command isn't imported, thus why it's undefined. discord.slash_command would have been used in that location instead.
As there doesn't seem to be an issue with the library, I'm going to close this, and you can open another issue in the guide repository as the example shown seems to be invalid.
If that's the case, it's very odd that the tutorial I linked specifies this method of defining commands when subclassing. The tutorial should be modified in that case.
I knew the slash_command method name should not have been accessible there, and I only tried it due to what was outlined by your website at guide.pycord.dev/popular-topics/subclassing-bots/. I mentioned that I used discord.Bot.slash_command, which is the correct way to access that member, and not discord.slash_command. This also did not work.
If the library does not support defining slash commands when subclassing, the official tutorials should not paint it as something that is possible. I will recreate this issue under the repository for your tutorial website.