Discord.py 斜线命令在齿轮中不起作用

时间:2021-07-09 09:10:01

标签: python discord

我正在构建一个 discord bot,并想在 cogs 中使用斜杠命令,但这些命令不显示或不起作用,这是代码


## cog
guild_ids = [858573429787066368, 861507832934563851]

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

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


## bot
bot = discord.ext.commands.Bot(command_prefix = "!")

@bot.event
async def on_ready():
  print(f'{bot.user} has logged in.')
  bot.load_extension('slash_music_cog')

bot.run("bot-token")

2 个答案:

答案 0 :(得分:0)

您需要稍微修改 bot.py 文件以初始化 SlashCommand 对象。查看图书馆的 README 以获得使用齿轮的机器人的更详细示例。

# bot
from discord_slash import SlashCommand

bot = discord.ext.commands.Bot(command_prefix = "!")
slash = SlashCommand(bot)

@bot.event
async def on_ready():
  print(f'{bot.user} has logged in.')
  bot.load_extension('slash_music_cog')

bot.run("bot-token")

答案 1 :(得分:0)

在为自己解决这个问题的时间比我想承认的要长得多之后,我终于找到了罪魁祸首:
您必须在运行机器人之前加载您的齿轮。
出于某种原因,如果您在 on_ready 函数中加载齿轮,它不会将您的命令注册为不和谐,因此这里的解决方案是将您的扩展加载代码移动到您的 bot.run("bot-token") 之前声明。

所以完成的代码应该看起来像

## cog
guild_ids = [858573429787066368, 861507832934563851]

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

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


## bot
bot = discord.ext.commands.Bot(command_prefix = "!")

@bot.event
async def on_ready():
  print(f'{bot.user} has logged in.')


bot.load_extension('slash_music_cog')
bot.run("bot-token")
相关问题