問題描述
所以我只是想知道,如何在 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 機器人的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!