首页 > 解决方案 > discord.py 中斜杠命令的事件监听器

问题描述

问题

是否有类似于discord.py或中的斜线命令的事件侦听器discord-py-slash-command,它在发送斜线命令时执行代码?

我知道的

众所周知,在 discord.py 中有事件监听器,例如on_message

@bot.event
async def on_message(message: discord.Message):
    await message.channel.send(f'You said {message.content}.')

discord.ext有一个听众commands

@bot.command()
async def hello(ctx):
    await ctx.send('Hi!')

并且discord_slash有斜杠命令:

@slash.slash(
    name='hello',
    description='Say hello.'
)
async def _hello(ctx):
    await ctx.send('Hi!')

我需要的

无论如何,每当使用斜杠命令时,我都想执行一些代码。我会想象它像这样工作:

@slash.event()
async def on_slash(ctx):
    # Something

提到的库中是否有这样的功能,如果没有,我是否仍然可以在使用斜杠命令时以某种方式执行一些代码?

PS:第一个问题,所以我愿意接受改进建议。

标签: pythondiscorddiscord.py

解决方案


我相当确定您无法收听其他机器人的斜线命令交互,但如果您想为自己的斜线命令设置监听器,您可以使用 on_interaction监听器:

@bot.event
async def on_interaction(interaction):
    if str(interaction.type) == "InteractionType.application_command":
        print("test")

application_command是斜杠命令交互,这里是其他类型的文档。
这是一般的文档on_interaction

另外请记住,这只是在 discord.py 2.0 alpha 版本中添加的,所以如果您还没有安装最新版本,您可以使用pip install -U git+https://github.com/Rapptz/discord.py.

希望这至少有点用处。


推荐阅读