Skip to content

Instantly share code, notes, and snippets.

@repen
Forked from AmirSbss/antispam.py
Created August 18, 2020 20:49
Show Gist options
  • Select an option

  • Save repen/c6f8301e52389cb5f290fd872b0c38a4 to your computer and use it in GitHub Desktop.

Select an option

Save repen/c6f8301e52389cb5f290fd872b0c38a4 to your computer and use it in GitHub Desktop.

Revisions

  1. AmirSbss revised this gist Jan 22, 2018. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions antispam.py
    Original file line number Diff line number Diff line change
    @@ -4,8 +4,7 @@
    max = 5 # Seconds
    ban = 300 # Seconds

    def is_spam(msg):
    user_id = msg.from_user.id
    def is_spam(user_id):
    try:
    usr = spams[user_id]
    usr["messages"] += 1
  2. AmirSbss created this gist Jan 22, 2018.
    28 changes: 28 additions & 0 deletions antispam.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    import time as tm
    spams = {}
    msgs = 4 # Messages in
    max = 5 # Seconds
    ban = 300 # Seconds

    def is_spam(msg):
    user_id = msg.from_user.id
    try:
    usr = spams[user_id]
    usr["messages"] += 1
    except:
    spams[user_id] = {"next_time": int(tm.time()) + max, "messages": 1, "banned": 0}
    usr = spams[user_id]
    if usr["banned"] >= int(tm.time()):
    return True
    else:
    if usr["next_time"] >= int(tm.time()):
    if usr["messages"] >= msgs:
    spams[user_id]["banned"] = tm.time() + ban
    # text = """You're banned for {} minutes""".format(ban/60)
    # bot.send_message(user_id, text)
    # User is banned! alert him...
    return True
    else:
    spams[user_id]["messages"] = 1
    spams[user_id]["next_time"] = int(tm.time()) + max
    return False