T
T
Telmor2021-10-10 11:50:12
Python
Telmor, 2021-10-10 11:50:12

How to make slash commands in discord.py cogs?

I want to make a command slash in my bot which I do in cogs. Here is main.py code:

import discord
from discord.ext import commands
import os
from discord_components import DiscordComponents
from discord import Intents
from discord.ext.commands import Bot
import discord_slash
from discord_slash import SlashCommand


bot = commands.Bot(command_prefix="!", intents=Intents.default())
#bot.remove_command("help") - включить при запуске бота
DiscordComponents(bot)
slash = SlashCommand(bot)


@bot.command()
async def load(ctx, extension):
    if ctx.author.id == 861522863704899604:
        bot.load_extension(f"cogs.{extension}")
        await ctx.send("Коги загружаются.....")
    else:
        await ctx.send(f"{ctx.author.mention}, Вы не **являетесь** разработчиком бота")


@bot.command()
async def unload(ctx, extension):
    if ctx.author.id == 861522863704899604:
        bot.unload_extension(f"cogs.{extension}")
        await ctx.send("Коги отгружаются.....")
    else:
        await ctx.send(f"{ctx.author.mention}, Вы не **являетесь** разработчиком бота")


@bot.command()
async def reload(ctx, extension):
    if ctx.author.id == 861522863704899604:
        bot.unload_extension(f"cogs.{extension}")
        bot.load_extension(f"cogs.{extension}")
        await ctx.send("Коги перезагружаются.....")
    else:
        await ctx.send(f"{ctx.author.mention}, Вы не **являетесь** разработчиком бота")


for filename in os.listdir("./cogs"):
    if filename.endswith(".py"):
        bot.load_extension(f"cogs.{filename[:-3]}")


bot.load_extension("cog")
bot.run("TOKEN")

And here is the subordinate file (bot.py):
from discord import Embed
from discord.ext.commands import Bot, Cog
from discord_slash import cog_ext, SlashContext

class Slash(Cog):
    def __init__(self, bot: Bot):
        self.bot = bot

    @cog_ext.cog_slash(name="test")
    async def _test(self, ctx: SlashContext):
        embed = Embed(title="Embed Test")
        await ctx.send(embed=embed)

def setup(bot: Bot):
    bot.add_cog(Slash(bot))

And here is the error in the console:
Traceback (most recent call last):
  File "C:\Users\Fenix\Desktop\modern\main.py", line 50, in <module>
    bot.load_extension("cog")
  File "C:\Users\Fenix\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 676, in load_extension
    raise errors.ExtensionNotFound(name)
discord.ext.commands.errors.ExtensionNotFound: Extension 'cog' could not be loaded.
Для продолжения нажмите любую клавишу . . .

Please help me with this issue.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question