英文:
how to use cogs and slash commands
问题
I'm trying to use prefix commands together with slash commands, in cog files that are separated in a separate folder.
import discord
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 MyClient(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 = MyClient(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: 'MyClient' object has no attribute 'load_extension'
英文:
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'
答案1
得分: 0
A few days ago there was a similar question click me. I gave an easy explanation on how to setup a main file and a cog. Take a look at the main file. Maybe it helps you with the debugging. In this case it was about prefix commands. If you want to use slash commands you have to use the @app_commands decorator like this:
from discord import app_commands
@app_commands.command(name="blablabla", description='my first slash command')
Taking a closer look at your problem, I think you should try to implement your class like this:
class MeuCliente(commands.Bot)
Because in the discord.py documenation the "load_extensions" method is listed as a method of the commands.bot class, and not of the discord.client class.
英文:
A few days ago there was a similar question click me. I gave an easy explanation on how to setup a main file and a cog. Take a look at the main file. Maybe it helps you with the debugging. In this case it was about prefix commands. If you want to use slash commands you have to use the @app_commands decorator like this:
from discord import app_commands
@app_commands.command(name="blablabla", description='my first slash command')
Taking a closer look at your problem, I think you should try to implement your class like this:
class MeuCliente(commands.Bot):
Because in the discord.py documenation the "load_extensions" method is listed as a method of the commands.bot class, and not of the discord.client class.
答案2
得分: 0
Cogs是由commands.Bot的一个实例加载的扩展。因此,你的MeuCliente
类需要继承commands.Bot
。此外,commands.Bot
已经有了自己的树,所以你不需要创建新的树。
class MeuCliente(commands.Bot):
def __init__(self):
intents = discord.Intents.default()
intents.message_content = True # This is necessary to use text commands
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()
注意:为了使你的命令同时适用于文本和斜杠,你需要在cogs内使用@hybrid_command装饰器来定义它们。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论