How do I prevent a user from sending more than one message in a channel?(如何防止用户在频道中发送多条消息?)
问题描述
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 中删除第二条消息,这样任何用户都只能发送一条消息.我被告知要使用 channel.history
,但我想不出一种方法来比较消息的作者以确保它们不是同一个人.
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.
这就是我想要的:我想防止重复发布:
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
来做到这一点.
I just don't know how to use channel.history
to do this.
推荐答案
您可以使用 on_message()
事件并将频道历史记录的限制设置为 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()
协程首先获取最新消息,除非另有说明,因此您可以将限制设置为 2 以获取用户刚刚发送的消息之前的最新消息.
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()
这篇关于如何防止用户在频道中发送多条消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何防止用户在频道中发送多条消息?


- 我如何透明地重定向一个Python导入? 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01