godofclash
godofclash

Reputation: 96

Add Slash Command to Cog Discord.py

I want to make the slash commands with discord.app_commands. But i dont know how to do that in the cog

Here is my code:

from discord import *
from discord.app_commands import CommandTree

class aclient(Client):
    def __init__(self):
        super().__init__(intents=Intents().all())
        self.synced = False

    async def on_ready(self):
        await self.wait_until_ready()
        if not self.synced:
            await tree.sync(guild = Object(id=1007367355208319126))
aclient = aclient()
tree = CommandTree(client=aclient)

class slash(tree):




    @tree.command(name='ping', description='Pongs back')
    async def self(self, interaction : Interaction, name : str = 'Pong'):
        await interaction.response.send_message('Pong' + name)


async def setup(client):
    await client.add_cog(slash(client))

Maybe it's about what I have to replace the tree in the slash class with but I have no idea for what I can replace it.

Upvotes: 1

Views: 6417

Answers (1)

MegaWatt_
MegaWatt_

Reputation: 9

here is code for slash commands:

from discord import app_commands # im using discord.py rewrite(2.0.0)

class cmds(commands.Cog):
    def __init__(self, client: MyClient):
        self.client = client

    @app_commands.command()
    async def ping(self, interaction: discord.Interaction) -> None:
        ping1 = f"{str(round(self.client.latency * 1000))} ms"
        embed = discord.Embed(title = "**Pong!**", description = "**" + ping1 + "**", color = 0xafdafc)
        await interaction.response.send_message(embed = embed)

async def setup(client):
    await client.add_cog(cmds(client))

Upvotes: 0

Related Questions