【问题标题】:Event listener for slash commands in discord.pydiscord.py 中斜杠命令的事件监听器
【发布时间】:2021-12-01 07:55:15
【问题描述】:

问题

在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:第一个问题,所以我愿意接受改进建议。

【问题讨论】:

  • 这很遗憾没有帮助,因为我不知道它是命令还是常规消息。

标签: python discord discord.py


【解决方案1】:

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

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

application_command 是斜杠命令交互,here are the docs 用于其他类型。
和here's the doc 为on_interaction 一般。

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

希望这至少有点用处。

【讨论】:

  • 绝对是。谢谢,它完全符合我的要求。 (关于 2.0 版本的重要提示)
猜你喜欢
  • 2022-11-30
  • 1970-01-01
  • 2023-02-26
  • 1970-01-01
  • 2023-02-08
  • 2021-10-02
  • 2022-12-23
  • 2023-01-13
  • 1970-01-01
相关资源
最近更新 更多