如何在 discord.py 中使用斜线命令

时间:2021-03-18 18:18:16

标签: discord.py

我的代码:

import discord
from discord.ext import commands
from discord_slash import SlashCommand
from discord_slash import SlashContext

bot = commands.Bot(command_prefix="#", intents=discord.Intents.all())
slash = SlashCommand(bot, sync_commands=True)

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


  @slash.slash(name="test", description="hello!")
  async def hello(self, ctx: SlashContext):
    await ctx.respond()
    await ctx.send(content="HELLO!")

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

代码运行时没有任何错误,但没有斜线命令。你能修好吗?

并且我希望我的库提交斜线命令以使其更容易

1 个答案:

答案 0 :(得分:0)

在 cog 中创建斜杠命令时需要使用:

@cog_ext.cog_slash()

代替:

@slash.slash()

您还需要在代码开头使用 cog 扩展名:

from discord_slash import cog_ext