問(wèn)題描述
User 1: Hello!
User 1: How are you?
User 2: I'm good.
User 2: hbu
User 3: hey guys!
User 1: i'm doing fine
我正在嘗試從用戶 1 和用戶 2 中刪除第二條消息,這樣任何用戶都只能發(fā)送一條消息.我被告知要使用 channel.history
,但我想不出一種方法來(lái)比較消息的作者以確保它們不是同一個(gè)人.
I'm trying to delete the second message from User 1 and User 2, so that any user can only send a single message. I was told to use channel.history
, but I can't think of a way to compare the author's of the messages to make sure they aren't the same person.
這就是我想要的:我想防止重復(fù)發(fā)布:
This is what I want: I want to prevent double posting:
User 1: Hello! How are you?
User 2: I'm good, hbu.
User 3: hey guys!
User 1: i'm doing fine
我只是不知道如何使用 channel.history
來(lái)做到這一點(diǎn).
I just don't know how to use channel.history
to do this.
推薦答案
您可以使用 on_message()
事件并將頻道歷史記錄的限制設(shè)置為 2:
You can use the on_message()
event and set the channel history's limit to 2 for this:
@bot.event
async def on_message(message):
recent_author = (await message.channel.history(limit=2).flatten())[1].author
if message.author == recent_author:
await message.delete()
history()
協(xié)程首先獲取最新消息,除非另有說(shuō)明,因此您可以將限制設(shè)置為 2 以獲取用戶剛剛發(fā)送的消息之前的最新消息.
The history()
coroutine gets the newest messages first, unless specified otherwise, so you can set the limit to 2 to get the most recent message before the one the user just sent.
參考資料:
Messageable.history()代碼>
Message.author
Message.delete()
這篇關(guān)于如何防止用戶在頻道中發(fā)送多條消息?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!