python - 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:第一个问题,所以我愿意接受改进建议。
解决方案
我相当确定您无法收听其他机器人的斜线命令交互,但如果您想为自己的斜线命令设置监听器,您可以使用 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
.
希望这至少有点用处。
推荐阅读
- javascript - 我的 f7/vue 单选按钮没有按预期工作
- c++ - 如何使用 std::make_unique() 在构造函数中启动 std::unique_ptr
- batch-file - 使用 BAT 文件时如何在 CMD 中输入命令?
- findersync - Finder Cover 流程分配第二个 FIFinderSync
- ruby-on-rails - 通过rails控制台将用户分配为rails中的管理员
- django - scrapy上的多蜘蛛
- php - Mac上的laravel迁移错误
- regex - .Net Regex - 在组捕获中省略空间
- apache-spark - spark.sql.shuffle.partitions 和 df.repartition 之间的区别(如果有的话)
- vba - 范围类的排序方法失败运行时错误1004