如何使用@client.event on_message discord-py-slash (discord.py)

时间:2021-06-19 05:01:14

标签: python 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!")

但是当我去 discord 时,我输入 / 和命令。我得到 Invalid interaction application command

我也试过这个:discord_slash: How to actually add a slash command/ why isn't mine working?
但我不明白如何忽略 on_message 函数中的 /slash 命令。

编辑 我尝试在 on_message 中打印 message.content,但我没有得到任何内容。当我尝试执行它时,终端中没有错误或异常。我只收到不和谐的错误。 This is the only error I get.

提前谢谢。

1 个答案:

答案 0 :(得分:0)

编辑

我实际上改变了很多东西,但我不小心认为这与同步命令有关,结果事实并非如此。 它不起作用的实际原因,它需要一个小时来注册全局斜杠命令,但是如果您将 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!")