Discord.py - how to detect if a user mentions/pings the bot(Discord.py - 如何检测用户是否提到/ping 机器人)
问题描述
所以我只是想知道,如何在 discord.py 中创建一个事件,如果用户 ping 机器人,它会回复一条消息?
So I am just wondering, how can I create an event in discord.py where if a user pings the bot it will respond with a message?
我在任何地方都没有找到关于如何做到这一点的任何具体内容,如果有人能帮助我了解如何做到这一点,我将不胜感激.我很感激!
I have not found anything concrete anywhere on how to do this and I would appreciate it if someone could help get my feet wet on how to do this. I appreciate it!
推荐答案
发现默认函数discord.User.提到_in 有效
I found that the default function discord.User.mentioned_in works
为了让它检查我们的机器人,我们在函数前面添加 client.user(这是我们的机器人),使其成为 client.user.提到的_in(message)代码>.参数 message 应该与您为 async def on_message(message) 行提供的参数相同
In order for it to check for our bot, we add client.user (which is our bot) in front of the function, making it client.user.mentioned_in(message). The argument message, should be the same argument that you gave for the async def on_message(message) line
例子:
@client.event
async def on_message(message):
if client.user.mentioned_in(message):
await message.channel.send('You mentioned me!')
我不知道为什么,但我还没有看到其他人使用这个功能.也许他们只是没有意识到这一点,而是在 message.content 中使用 client.user.id
I'm not sure why but I haven't seen any others use this function though. Maybe they just aren't aware of this, and instead use client.user.id in message.content
这篇关于Discord.py - 如何检测用户是否提到/ping 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Discord.py - 如何检测用户是否提到/ping 机器人
基础教程推荐
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
