I'm trying to use prefix commands together with slash commands, in cog files that are separated in a separate folder.
import os
import datetime
import asyncio
import random
from discord.ext import commands
from discord import app_commands
from dotenv import load_dotenv
import discloud
server_id = discord.Object(id=717615951113093130)
app_id = 1068876103009194025
hoje = datetime.datetime.today()
dia, mes, ano = hoje.day, hoje.month, hoje.year
hh, mm, ss = hoje.hour, hoje.minute, hoje.second
class MeuCliente(discord.Client):
def __init__(self,*,intents:discord.Intents):
super().__init__(
application_id=app_id,
command_prefix="!",
intents=intents,
)
self.tree = app_commands.CommandTree(self)
async def setup_hook(self):
await self.load_extension("cogs.dashboard")
await self.load_extension("cogs.embedcreator")
self.tree.copy_global_to(guild=server_id)
await self.tree.sync(guild=server_id)
bot = MeuCliente(intents=discord.Intents.default())
@bot.event
async def on_ready():
print(f"{bot.user.name} está online!")
load_dotenv()
TOKEN = os.getenv("DISCORD_TOKEN")
bot.run(TOKEN)
I want to be able to use both prefix commands and slash commands in cog files, but I can't implement a workaround for this error:
File "f:\BotLand\Ticket 2\main.py", line 29, in setup_hook
await self.load_extension("cogs.dashboard")
^^^^^^^^^^^^^^^^^^^
AttributeError: 'MeuCliente' object has no attribute 'load_extension'
Cogs are extensions to be loaded by an instance of commands.Bot. So your MeuCliente
class needs to inherit commands.Bot
. Also, commands.Bot
already has its own tree, so you don't need to create a new one.
class MeuCliente(commands.Bot):
def __init__(self):
intents = discord.Intents.default()
intents.message_content = True # Isso é necessário para usar comandos de texto
super().__init__(
command_prefix="!",
intents=intents,
)
async def setup_hook(self):
await self.load_extension("cogs.dashboard")
await self.load_extension("cogs.embedcreator")
self.tree.copy_global_to(guild=server_id)
await self.tree.sync(guild=server_id)
bot = MeuCliente()
Note: For your commands to work for both text and slash you need to define them using @hybrid_command decorator iside the cogs.