首页 > 解决方案 > 如何使用@client.event on_message discord-py-slash (discord.py) 添加斜杠命令

问题描述

我正在尝试向我现有的机器人添加斜杠命令。
目前我正在使用

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


@client.event
async def on_message(message):
    # do some stuff   


@slash.slash(name="test",
             description="This is just a test command, nothing more.")
async def test(ctx):
    await ctx.send(content="Hello World!")

但是当我不和谐时,我输入 / 和命令。我明白了 Invalid interaction application command

我也试过这个:discord_slash:如何实际添加斜线命令/为什么我的不工作?.
但我不明白如何忽略on_message函数中的 /slash 命令。

编辑 我尝试在中打印 message.content on_message,但我没有得到任何内容。当我尝试执行它时,终端中没有错误或异常。我只得到不和谐的错误。 这是我得到的唯一错误。

提前谢谢。

标签: pythondiscord.py

解决方案


编辑

我实际上正在更改很多东西,但我不小心以为这与同步命令有关,结果并非如此。它不起作用的实际原因是,它需要一个小时来注册全局斜杠命令,但是如果你将 guild_ids 参数添加到斜杠装饰器,它会更改为 guild_slash_command。它可以立即工作(不到 1 分钟)。我还添加了sync_command,它在添加或删除时将更改应用于斜杠命令。

编辑代码

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

guilds_ids = [guild(server) ids]

@client.event
async def on_message(message):
    # do some stuff   


@slash.slash(name="test",
             description="This is just a test command, nothing more.", guild_ids=guild_ids)
async def test(ctx):
    await ctx.send(content="Hello World!")

原来的

经过一些尝试和错误,我发现通过删除斜线 obj 声明中的 sync_commands 。有效

这是整个代码..

intents = discord.Intents.all()
client = commands.Bot(command_prefix=".", intents=intents)
slash = SlashCommand(client)


@client.event
async def on_message(message):
    # do some stuff   


@slash.slash(name="test",
             description="This is just a test command, nothing more.")
async def test(ctx):
    await ctx.send(content="Hello World!")


推荐阅读