How do I prevent a user from sending more than one message in a channel?

隐身守侯 提交于 2021-02-10 17:03:34

问题


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

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

I just don't know how to use channel.history to do this.


回答1:


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()

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.


References:

  • Messageable.history()
  • Message.author
  • Message.delete()


来源:https://stackoverflow.com/questions/62247215/how-do-i-prevent-a-user-from-sending-more-than-one-message-in-a-channel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!