-1

this is should be slash command about bot ping but i didnt understand how write slahs commands in cogs

import discord 
from discord.ext import commands
from discord import Interaction 

client = commands.Bot(command_prefix='#', intents=discord.Intents.all())

class ping(commands.Cog):
    def __init__(self, client):
        self.client = client
    @commands.Cog.listener()
    async def on_ready(self):
        await client.tree.sync()
        print("/ping.py is ready!")

@client.tree.command(name="ping", description="shows bot ping in ms.")
async def ping(self, interaction: discord.Interaction):
    bot_latency = round(self, client.latency * 1000)
    await interaction.response.send_message(f"Pong!{bot_latency} ms.") 


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

error: TypeError: unsupported type annotation <class 'discord.interactions.Interaction'>

help pls, i dont even close understand where is the problem

rui
  • 13
  • 1
  • Does this answer your question? [How do i make a working slash command in discord.py](https://stackoverflow.com/questions/71165431/how-do-i-make-a-working-slash-command-in-discord-py) – ESloman Feb 23 '23 at 12:20
  • there is no cogs – rui Feb 23 '23 at 13:00

1 Answers1

0

Your command is out of the cog and also, you are using client.tree.command, that decorator is only available outside cogs, in the main file. For cogs must use app_commands.command decorator. Also, try to use cog classes outside the main file to prevent errors.

import discord
from discord.ext import commands
from discord import app_commands
from discord import Interaction

# This must be in your main file, not in the cog one
client = commands.Bot(..)

# This must be in another file
class Ping(commands.Cog):
    # __init__ is unnecessary, the class auto applies the client parameter
    @commands.Cog.listener()
    async def on_ready(self):
        ...

    @app_commands.command(description = "Shows bot current latency in ms.")
    async def ping(self, interaction:discord.Interaction):
        bot_latency = round(self.client.latency * 1000) # here you can also use interaction.client.latency instead of self.client.latency
        await interaction.response.send_message(f"Pong! Bot latency is `{bot_latency}`ms")

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