discord.py 2.0 slash commands in cogs

Viewed 92

I have had a lot of trouble getting slash commands to work in python 2.0. I found the code below and managed to make it work for me. However, I have no clue how to get it working in cogs. I even found this that uses a similar system.my load_extensions command also doesn't even work via the system below.

import discord
from discord import app_commands
import os, asyncio

class aclient(discord.Client):
    def __init__(self):
        super().__init__(intents = discord.Intents.default())
        self.synced = False #we use this so the bot doesn't sync commands more than once

    async def on_ready(self):
        await self.wait_until_ready()
        if not self.synced: #check if slash commands have been synced 
            await tree.sync() #guild specific: leave blank if global (global registration can take 1-24 hours)
            self.synced = True
        print(f"have logged in as {self.user}.")

client = aclient()
tree = app_commands.CommandTree(client)

@tree.command(name = 'hello', description='say hi!')
async def hello(interaction: discord.Interaction):
    await interaction.response.send_message(f"Hi there!", ephemeral = True) 

client.run("token")

async def load_extensions():
    for filename in os.listdir('./cogs'):
        if filename.endswith('.py'):
            await client.load_extension(f'cogs.{filename[:-3]}')
1 Answers
Related