問(wèn)題描述
我正在使用 Python (v. 3.6.1) 編寫一個(gè) Discord 機(jī)器人,它檢測(cè)在一個(gè)頻道中發(fā)送的所有消息并在同一頻道中回復(fù)它們.但是,機(jī)器人會(huì)自行回復(fù)消息,從而導(dǎo)致無(wú)限循環(huán).
I am writing a Discord bot using Python (v. 3.6.1) which detects all messages sent in a channel and replies to them in that same channel. However, the bot replies to messages by itself, causing an infinite loop.
@bot.event
async def on_message(message)
await bot.send_message(message.channel, message.content)```
我該如何解決這個(gè)問(wèn)題?
How would I fix this?
推薦答案
message
類包含有關(guān)消息的 author
,您可以使用它來(lái)確定是否回復(fù)消息.作者
是 會(huì)員
對(duì)象(或其超類 User
如果頻道是私有的),它具有 id
屬性,但也支持用戶之間的直接邏輯比較.
The message
class contains information on the message's author
, which you can utilize to determine whether or not to respond to the message. author
is a Member
object (or its superclass User
if the channel is private), which has an id
property but also supports direct logical comparisons between users.
例如:
@bot.event
async def on_message(message):
if message.author != bot.user:
await bot.send_message(message.channel, message.content)
應(yīng)該按需要發(fā)揮作用
這篇關(guān)于如何讓我的 Python Discord 機(jī)器人檢查消息是否由機(jī)器人本身發(fā)送?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!