如何在 cogs 中创建斜线命令?

问题描述 投票:0回答:1

我想在齿轮中创建斜线命令,但通常的命令

@commands.slash_command
不起作用。我尝试写
@discord.slash_command
,但也没有帮助。

齿轮代码:

#file "test.py" in ./cogs/test.py

import discord
from discord.ext import commands

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

    @discord.slash_command(name='ping')
    async def ping(self, ctx):
        pong = self.bot.latency
        await ctx.send(f'pong! `{round(pong*1000)} ms`')

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

主文件:

#file "main.py" in ./main.py

import discord
import os
from discord.ext import commands
from dotenv import load_dotenv

intents = discord.Intents.all()
bot = commands.Bot(command_prefix="t.", intents=intents, help_command=None)

@bot.event
async def on_ready():
    try:
        bot.load_extension(f'cogs.{cog}')
        print(f'[+] cog "{cog}.py" installed')
    except:
        print(f'[-] cog "{cog}.py" NOT installed')


load_dotenv()
token = os.getenv("TOKEN")
bot.run(token)

我正在使用pycord,并且这个库中没有

app_commands
模块

python discord pycord
1个回答
0
投票

您似乎正在使用

@discord.slash_command()
而不是
commands.slash_command()
。如果您使用命令模块,您的命令装饰器应以
commands
开头,而不是
discord

© www.soinside.com 2019 - 2024. All rights reserved.