I am trying to implement slash commands on discord using discord.py through steps shown in this video
The code in the video (which works) is as follows:
import discord
from discord import app_commands
class aClient(discord.Client):
def __init__(self):
super().__init__(intents=discord.Intents.default())
self.synced = False
async def on_ready(self):
await self.wait_until_ready()
if not self.synced:
await tree.sync(guild=discord.Object(id=GUILD_ID))
self.synced = True
printf(f"logged in as {self.user}")
client = aClient()
tree = app_commands.CommandTree(client)
@tree.command(name="test", description="testing", guild=discord.Object(id=GUILD_ID))
async def slash(interaction: discord.Interaction, name: str):
await interaction.response.send_message("test")
client.run(TOKEN)
But I would like to incorporate the outside instantiation and decoration of the tree object and the slash function into the main class itself, and not make the tree object separate from the main class and also the slash function part of the class. I read this answer and tried the following:
import discord
from discord import app_commands
class aClient(discord.Client):
def __init__(self):
super().__init__(intents=discord.Intents(messages=True, message_content=True))
self.tree = app_commands.CommandTree(self)
self.slash = self.tree.command(self.tree, name="name", description="name", guild=discord.Object(id=GUILD_ID))
self.synced = False
async def on_ready(self):
print(f"Connected as {self.user}")
await self.wait_until_ready()
if not self.synced:
self.tree.sync(guild=discord.Object(id=GUILD_ID))
self.synced = True
async def slash(self, interaction: discord.Interaction, name: str):
await interaction.response.send_message("test")
client = aClient()
client.run(TOKEN)
But doing so gives the following error:
Traceback (most recent call last):
File "E:\myprojects\new.py", line 33, in <module>
client = aClient()
File "E:\myprojects\new.py", line 15, in __init__
self.slash = self.tree.command(self.tree, name="name", description="name", guild=discord.Object(id=GUILD_ID))
TypeError: CommandTree.command() takes 1 positional argument but 2 positional arguments (and 3 keyword-only arguments) were given
I have used environment variables for GUILD_ID etc