How do I create a slash command in cogs?

78 Views Asked by At

I want to make a slash command in the cogs, but the usual command @commands.slash_command does not work. I tried writing @discord.slash_command but it didn't help either.

Cog code:

#file "test.py" in ./cogs/test.py

import discord
from discord.ext import commands

class Test(commands.Cog): 
    def __init__(self, bot):
        self.bot = bot

    @discord.slash_command(name='ping')
    async def ping(self, ctx):
        pong = self.bot.latency
        await ctx.send(f'pong! `{round(pong*1000)} ms`')

def setup(bot):
    bot.add_cog(Test(bot))

Main file:

#file "main.py" in ./main.py

import discord
import os
from discord.ext import commands
from dotenv import load_dotenv

intents = discord.Intents.all()
bot = commands.Bot(command_prefix="t.", intents=intents, help_command=None)

@bot.event
async def on_ready():
    try:
        bot.load_extension(f'cogs.{cog}')
        print(f'[+] cog "{cog}.py" installed')
    except:
        print(f'[-] cog "{cog}.py" NOT installed')


load_dotenv()
token = os.getenv("TOKEN")
bot.run(token)

I am using pycord, and there is no app_commands module in this library

1

There are 1 best solutions below

1
Softwaretyp Ausm Osten On

It seems like you are using @discord.slash_command() instead of commands.slash_command(). If you are using the commands module your command decorator should begin with commands instead of discord.