[英]How can I make my bot say something when it or another bot is used in the command? (discord.py)
[英]Trying to make discord.py bot say something when its gets mentioned, doesnt respond
當有人提到機器人時,我希望它回應說:“嘿。我被提及了。我的前綴是>
。你可以輸入>help
來查看我的命令。” 我已經寫出了代碼,但是當提到它時,機器人根本沒有響應。 當我運行它或提及機器人時,我沒有收到任何錯誤。 我怎樣才能讓它工作? 我的代碼如下。 提前致謝。
@bot.event
async def on_message(message):
if message.author.id == bot.user.id:
return
msg_content = message.content.lower()
mention = ['@830599839623675925', '@Ultimate Bot']
if any(word in msg_content for word in mention):
await message.channel.send("Hey! I've been mentioned. My prefix is `>`. You can type `>help` to see my commands.")
else:
await bot.process_commands(message)
您可以使用mentioned_in()
方法。
所以在這種情況下它會是;
@bot.event
async def on_message(message):
if message.author.id == bot.user.id: return
if bot.user.mentioned_in(message):
await message.channel.send("Hey! I've been mentioned.")
await bot.process_commands(message)
需要注意的一些事項:
硬編碼值(例如您的mention
變量)是不好的做法。 它使維護代碼變得更加困難,並且當您的項目變得更大時,您可能會質疑這些看似隨機的字符串所指的目的是什么。 例如,如果您要更改機器人的名稱,或者將您的代碼轉移到一個新的機器人,那么遍歷您的整個代碼庫並挑選並更改這些值將遠非理想。
我不知道您的應用程序的 rest,但您可能不希望process_commands()
出現在else
語句中。 例如,如果用戶在他們的消息中提到了機器人,但正在使用命令,則該命令將被阻止,因為您沒有觸發process_commands()
。
希望這能回答你的問題
您可以改為檢查 bot.user 是否在消息提及中。
您還可以檢查是否使用了機器人顯示名稱。
if bot.user in message.mentions or (message.guild and message.guild.me in message.mentions):
print("bot was mentioned")
elif message.guild and message.guild.me.display_name in message.content:
print("bot name was used")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.