我在我的 pycord 机器人中使用齿轮并制作了斜杠命令,但我看不到它们
这是我的机器人运行的主文件:
import tracemalloc
tracemalloc.start()
import os
import pathlib
import discord
from discord.ext import commands
from discord import option
from dotenv import load_dotenv
load_dotenv()
CMDS_PATH = pathlib.Path(__file__).parent / "cmds"
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(intents=intents)
bot.auto_sync = True
@bot.event
async def on_ready():
print(f'{bot.user} has connected to Discord!')
for cmd_file in CMDS_PATH.glob("*.py"):
if cmd_file.name != "__init__.py":
bot.load_extension(f"cmds.{cmd_file.name[:-3]}")
bot.run(os.getenv("TOKEN"))
这是我的测试齿轮,位于“cogs”文件夹中
import discord
from discord.ext import commands
class test(commands.Cog):
def __init__(self, bot: discord.Bot):
self.bot = bot
@commands.slash_command()
async def hello(self, ctx):
await ctx.defer(ephemeral=True)
await ctx.respond("hello", ephemeral=True)
def setup(bot: discord.Bot):
bot.add_cog(test(bot))
有人知道为什么我看不到斜杠命令吗? 我第一次尝试同步,但被告知 pycord 中不再需要同步,因为它会自动同步
Pycord 在
on_ready
之前注册应用程序命令。在这种情况下,这意味着您应该在 on_ready
事件之外加载齿轮。
例如:
@bot.event
async def on_ready():
print(f'{bot.user} has connected to Discord!')
for cmd_file in CMDS_PATH.glob("*.py"):
if cmd_file.name != "__init__.py":
bot.load_extension(f"cmds.{cmd_file.name[:-3]}")