简体   繁体   English

discord.py 中斜线命令的事件侦听器

[英]Event listener for slash commands in discord.py

The Question问题

Is there something like an event listener for slash commands in discord.py or discord-py-slash-command , that execudes code when a slash command was send?discord.pydiscord-py-slash-command是否有类似斜杠命令的事件侦听器,在发送斜杠命令时执行代码?

What I know我知道的

As we all know, in discord.py there are event listeners, like on_message :众所周知,在on_message有事件侦听器,例如on_message

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

And with discord.ext there a listeners for commands :使用discord.ext有一个commands监听器:

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

And with discord_slash there are slash commands:使用discord_slash有斜线命令:

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

What I need我需要的

Regardless, I want to execute some code, whenever a slash command is used.无论如何,每当使用斜杠命令时,我都想执行一些代码。 I would imagine it to work like this:我会想象它是这样工作的:

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

Is there such a functionality in the mentioned libaries and if not, can I still somehow execude some code whenever a slash command was used?提到的库中是否有这样的功能,如果没有,我是否仍然可以在使用斜杠命令时以某种方式执行一些代码?

PS: First question, so I'm open to improvement suggestions. PS:第一个问题,所以我愿意接受改进建议。

I'm fairly certain that you cannot listen to the slash command interactions of other bots, but if you want to set up a listener for your own slash commands you can use the on_interaction listener:我相当肯定你不能听其他机器人的 slash 命令交互,但是如果你想为你自己的 slash 命令设置一个监听器,你可以使用on_interaction监听器:

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

application_command is a slash command interaction, here are the docs for the other types. application_command是一个斜线命令交互, 这里是其他类型的文档
And here's the doc for on_interaction in general.这是on_interaction一般的文档

Also please keep in mind that this only got added in the discord.py 2.0 alpha version, so if you do not have the newest version installed already, you can install it with pip install -U git+https://github.com/Rapptz/discord.py .另请记住,这仅在 discord.py 2.0 alpha 版本中添加,因此如果您尚未安装最新版本,则可以使用pip install -U git+https://github.com/Rapptz/discord.py安装它pip install -U git+https://github.com/Rapptz/discord.py

Hope this is at least somewhat useful.希望这至少有点用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM