Skip to content

Commit

Permalink
Merge pull request #279 from AnimeKaizoku/KigyoDev
Browse files Browse the repository at this point in the history
handle spam
  • Loading branch information
Dank-del authored Nov 28, 2023
2 parents 6b8fbc3 + 4aeab47 commit 7996255
Show file tree
Hide file tree
Showing 53 changed files with 331 additions and 67 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pillow>=4.2.0
psutil
psycopg2-binary
pyYAML>=5.1.2
pyrate-limiter
pyrate-limiter==2.10.0
python-telegram-bot==13.15
requests
spamwatch
Expand Down
10 changes: 9 additions & 1 deletion tg_bot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
# NOTE: Module order is not guaranteed, specify that in the config file!
from tg_bot.modules import ALL_MODULES
from tg_bot.modules.helper_funcs.chat_status import is_user_admin
from tg_bot.modules.helper_funcs.decorators import kigcmd, kigcallback, kigmsg
from tg_bot.modules.helper_funcs.decorators import kigcmd, kigcallback, kigmsg, rate_limit
from tg_bot.modules.helper_funcs.misc import paginate_modules
from tg_bot.modules.language import gs

Expand Down Expand Up @@ -126,6 +126,7 @@ def test(update: Update, _: CallbackContext):

@kigcallback(pattern=r'start_back')
@kigcmd(command='start', pass_args=True)
@rate_limit(5, 60)
def start(update: Update, context: CallbackContext): # sourcery no-metrics
"""#TODO
Expand Down Expand Up @@ -287,6 +288,7 @@ def start(update: Update, context: CallbackContext): # sourcery no-metrics


# for test purposes
@rate_limit(5, 60)
def error_callback(_, context: CallbackContext):
"""#TODO
Expand Down Expand Up @@ -315,6 +317,7 @@ def error_callback(_, context: CallbackContext):


@kigcallback(pattern=r'help_')
@rate_limit(5, 60)
def help_button(update: Update, context: CallbackContext):
"""#TODO
Expand Down Expand Up @@ -402,6 +405,7 @@ def help_button(update: Update, context: CallbackContext):


@kigcmd(command='help')
@rate_limit(5, 60)
def get_help(update: Update, context: CallbackContext):
'''#TODO
Expand Down Expand Up @@ -535,6 +539,7 @@ def send_settings(chat_id: int, user_id: int, user=False):


@kigcallback(pattern=r"stngs_")
@rate_limit(5, 60)
def settings_button(update: Update, context: CallbackContext):
'''#TODO
Expand Down Expand Up @@ -626,6 +631,7 @@ def settings_button(update: Update, context: CallbackContext):


@kigcmd(command='settings')
@rate_limit(5, 60)
def get_settings(update: Update, context: CallbackContext):
'''#TODO
Expand Down Expand Up @@ -664,6 +670,7 @@ def get_settings(update: Update, context: CallbackContext):


@kigcmd(command='donate')
@rate_limit(5, 60)
def donate(update: Update, _: CallbackContext):
"""#TODO
Expand All @@ -676,6 +683,7 @@ def donate(update: Update, _: CallbackContext):


@kigmsg(Filters.status_update.migrate)
@rate_limit(5, 60)
def migrate_chats(update: Update, context: CallbackContext):
"""#TODO
Params:
Expand Down
10 changes: 9 additions & 1 deletion tg_bot/modules/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
can_promote,
connection_status,
)
from tg_bot.modules.helper_funcs.decorators import kigcmd
from tg_bot.modules.helper_funcs.decorators import kigcmd, rate_limit
from tg_bot.modules.helper_funcs.extraction import extract_user, extract_user_and_text
from tg_bot.modules.language import gs
from tg_bot.modules.log_channel import loggable
Expand All @@ -25,6 +25,7 @@
@can_promote
@user_admin(AdminPerms.CAN_PROMOTE_MEMBERS)
@loggable
@rate_limit(5, 60)
def promote(update: Update, context: CallbackContext) -> Optional[str]:
bot = context.bot
args = context.args
Expand Down Expand Up @@ -109,6 +110,7 @@ def promote(update: Update, context: CallbackContext) -> Optional[str]:
@can_promote
@user_admin(AdminPerms.CAN_PROMOTE_MEMBERS)
@loggable
@rate_limit(5, 60)
def demote(update: Update, context: CallbackContext) -> Optional[str]:
bot = context.bot
args = context.args
Expand Down Expand Up @@ -181,6 +183,7 @@ def demote(update: Update, context: CallbackContext) -> Optional[str]:
"""
@kigcmd(command="admincache", can_disable=False)
@u_admin
@rate_limit(5, 60)
def refresh_admin(update, _):
ADMIN_CACHE.pop(update.effective_chat.id)
update.effective_message.reply_text("Admins cache refreshed!")
Expand All @@ -192,6 +195,7 @@ def refresh_admin(update, _):
@bot_admin
@can_promote
@user_admin(AdminPerms.CAN_PROMOTE_MEMBERS)
@rate_limit(5, 60)
def set_title(update: Update, context: CallbackContext):
bot = context.bot
args = context.args
Expand Down Expand Up @@ -257,6 +261,7 @@ def set_title(update: Update, context: CallbackContext):
@can_pin
@user_admin(AdminPerms.CAN_PIN_MESSAGES)
@loggable
@rate_limit(5, 60)
def pin(update: Update, context: CallbackContext) -> str:
bot = context.bot
args = context.args
Expand Down Expand Up @@ -299,6 +304,7 @@ def pin(update: Update, context: CallbackContext) -> str:
@can_pin
@user_admin(AdminPerms.CAN_PIN_MESSAGES)
@loggable
@rate_limit(5, 60)
def unpin(update: Update, context: CallbackContext) -> str:
bot = context.bot
chat = update.effective_chat
Expand All @@ -325,6 +331,7 @@ def unpin(update: Update, context: CallbackContext) -> str:
@bot_admin
@user_admin(AdminPerms.CAN_INVITE_USERS)
@connection_status
@rate_limit(5, 60)
def invite(update: Update, context: CallbackContext):
bot = context.bot
chat = update.effective_chat
Expand All @@ -347,6 +354,7 @@ def invite(update: Update, context: CallbackContext):


@kigcmd(command=["admin", "admins"])
@rate_limit(5, 60)
def adminlist(update: Update, _):
administrators = update.effective_chat.get_administrators()
text = "Admins in *{}*:".format(update.effective_chat.title or "this chat")
Expand Down
5 changes: 4 additions & 1 deletion tg_bot/modules/afk.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
from telegram.error import BadRequest
from tg_bot.modules.sql import afk_sql as sql
from tg_bot.modules.users import get_user_id
from tg_bot.modules.helper_funcs.decorators import kigcmd, kigmsg
from tg_bot.modules.helper_funcs.decorators import kigcmd, kigmsg, rate_limit

@kigmsg(Filters.regex("(?i)^brb"), friendly="afk", group=3)
@kigcmd(command="afk", group=3)
@rate_limit(5, 60)
def afk(update: Update, context: CallbackContext):
args = update.effective_message.text.split(None, 1)
user = update.effective_user
Expand Down Expand Up @@ -37,6 +38,7 @@ def afk(update: Update, context: CallbackContext):
pass

@kigmsg((Filters.all & Filters.chat_type.groups), friendly='afk', group=1)
@rate_limit(40, 60)
def no_longer_afk(update: Update, context: CallbackContext):
user = update.effective_user
message = update.effective_message
Expand Down Expand Up @@ -68,6 +70,7 @@ def no_longer_afk(update: Update, context: CallbackContext):
return

@kigmsg((Filters.entity(MessageEntity.MENTION) | Filters.entity(MessageEntity.TEXT_MENTION) & Filters.chat_type.groups), friendly='afk', group=8)
@rate_limit(5, 60)
def reply_afk(update: Update, context: CallbackContext):
bot = context.bot
message = update.effective_message
Expand Down
6 changes: 5 additions & 1 deletion tg_bot/modules/anilist.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import requests
import math
import time
from tg_bot.modules.helper_funcs.decorators import kigcmd
from tg_bot.modules.helper_funcs.decorators import kigcmd, rate_limit

def shorten(description, info="anilist.co"):
msg = ""
Expand Down Expand Up @@ -160,6 +160,7 @@ def t(milliseconds: int) -> str:
url = "https://graphql.anilist.co"

@kigcmd(command="airing")
@rate_limit(messages_per_window=5, window_seconds=60)
def airing(update: Update, context: CallbackContext):
message = update.effective_message
search_str = message.text.split(" ", 1)
Expand All @@ -182,6 +183,7 @@ def airing(update: Update, context: CallbackContext):
update.effective_message.reply_text(msg, parse_mode=ParseMode.MARKDOWN)

@kigcmd(command="anime")
@rate_limit(5, 60)
def anime(update: Update, context: CallbackContext): # sourcery no-metrics
message = update.effective_message
search = message.text.split(" ", 1)
Expand Down Expand Up @@ -252,6 +254,7 @@ def anime(update: Update, context: CallbackContext): # sourcery no-metrics
)

@kigcmd(command="character")
@rate_limit(5, 60)
def character(update: Update, context: CallbackContext):
message = update.effective_message
search = message.text.split(" ", 1)
Expand Down Expand Up @@ -285,6 +288,7 @@ def character(update: Update, context: CallbackContext):
)

@kigcmd(command="manga")
@rate_limit(5, 60)
def manga(update: Update, context: CallbackContext):
message = update.effective_message
search = message.text.split(" ", 1)
Expand Down
3 changes: 2 additions & 1 deletion tg_bot/modules/announce.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from telegram.utils.helpers import mention_html

from tg_bot.modules.log_channel import loggable
from tg_bot.modules.helper_funcs.decorators import kigcmd
from tg_bot.modules.helper_funcs.decorators import kigcmd, rate_limit

import tg_bot.modules.sql.logger_sql as sql
from ..modules.helper_funcs.anonymous import user_admin as u_admin, AdminPerms
Expand All @@ -14,6 +14,7 @@
@kigcmd(command="announce", pass_args=True)
@u_admin(AdminPerms.CAN_CHANGE_INFO)
@loggable
@rate_limit(5, 60)
def announcestat(update: Update, context: CallbackContext) -> str:
args = context.args
if len(args) > 0:
Expand Down
3 changes: 2 additions & 1 deletion tg_bot/modules/antichannel.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from telegram.ext.filters import Filters
from tg_bot.modules.helper_funcs.decorators import kigcmd, kigmsg
from tg_bot.modules.helper_funcs.decorators import kigcmd, kigmsg, rate_limit
from telegram import Update, message
from telegram.ext import CallbackContext
from ..modules.helper_funcs.anonymous import user_admin, AdminPerms
Expand All @@ -9,6 +9,7 @@

@kigcmd(command="antichannel", group=100)
@user_admin(AdminPerms.CAN_RESTRICT_MEMBERS)
@rate_limit(5, 60)
def set_antichannel(update: Update, context: CallbackContext):
message = update.effective_message
chat = update.effective_chat
Expand Down
3 changes: 2 additions & 1 deletion tg_bot/modules/antiflood.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from tg_bot.modules.sql import antiflood_sql as sql
from tg_bot.modules.connection import connected
from tg_bot.modules.helper_funcs.alternate import send_message
from tg_bot.modules.helper_funcs.decorators import kigcmd, kigmsg, kigcallback
from tg_bot.modules.helper_funcs.decorators import kigcmd, kigmsg, kigcallback, rate_limit

from ..modules.helper_funcs.anonymous import user_admin, AdminPerms

Expand Down Expand Up @@ -131,6 +131,7 @@ def check_flood(update, context) -> Optional[str]:
@user_admin_no_reply
@bot_admin
@kigcallback(pattern=r"unmute_flooder")
@rate_limit(5, 60)
def flood_button(update: Update, context: CallbackContext):
bot = context.bot
query = update.callback_query
Expand Down
4 changes: 3 additions & 1 deletion tg_bot/modules/antilinkedchannel.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
from telegram.ext.filters import Filters
from tg_bot.modules.helper_funcs.chat_status import bot_admin, bot_can_delete

from tg_bot.modules.helper_funcs.decorators import kigcmd, kigmsg
from tg_bot.modules.helper_funcs.decorators import kigcmd, kigmsg, rate_limit
from ..modules.helper_funcs.anonymous import user_admin, AdminPerms
import tg_bot.modules.sql.antilinkedchannel_sql as sql


@kigcmd(command="antilinkedchan", group=112)
@bot_can_delete
@user_admin(AdminPerms.CAN_RESTRICT_MEMBERS)
@rate_limit(5, 60)
def set_antilinkedchannel(update: Update, context: CallbackContext):
message = update.effective_message
chat = update.effective_chat
Expand Down Expand Up @@ -51,6 +52,7 @@ def eliminate_linked_channel_msg(update: Update, _: CallbackContext):
@kigcmd(command="antichannelpin", group=114)
@bot_admin
@user_admin(AdminPerms.CAN_RESTRICT_MEMBERS)
@rate_limit(5, 60)
def set_antipinchannel(update: Update, context: CallbackContext):
message = update.effective_message
chat = update.effective_chat
Expand Down
7 changes: 6 additions & 1 deletion tg_bot/modules/approve.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import tg_bot.modules.sql.approve_sql as sql
from tg_bot import SUDO_USERS
from tg_bot.modules.helper_funcs.chat_status import user_admin as u_admin
from tg_bot.modules.helper_funcs.decorators import kigcmd, kigcallback
from tg_bot.modules.helper_funcs.decorators import kigcmd, kigcallback, rate_limit
from tg_bot.modules.helper_funcs.extraction import extract_user
from tg_bot.modules.log_channel import loggable
from ..modules.helper_funcs.anonymous import user_admin, AdminPerms
Expand All @@ -17,6 +17,7 @@
@kigcmd(command='approve', filters=Filters.chat_type.groups)
@loggable
@user_admin(AdminPerms.CAN_CHANGE_INFO)
@rate_limit(5, 60)
def approve(update: Update, context: CallbackContext):
message = update.effective_message
chat_title = message.chat.title
Expand Down Expand Up @@ -62,6 +63,7 @@ def approve(update: Update, context: CallbackContext):
@kigcmd(command='unapprove', filters=Filters.chat_type.groups)
@loggable
@user_admin(AdminPerms.CAN_CHANGE_INFO)
@rate_limit(5, 60)
def disapprove(update: Update, context: CallbackContext):
message = update.effective_message
chat_title = message.chat.title
Expand Down Expand Up @@ -98,6 +100,7 @@ def disapprove(update: Update, context: CallbackContext):

@kigcmd(command='approved', filters=Filters.chat_type.groups)
@user_admin(AdminPerms.CAN_CHANGE_INFO)
@rate_limit(5, 60)
def approved(update: Update, _: CallbackContext):
message = update.effective_message
chat_title = message.chat.title
Expand All @@ -116,6 +119,7 @@ def approved(update: Update, _: CallbackContext):

@kigcmd(command='approval', filters=Filters.chat_type.groups)
@user_admin(AdminPerms.CAN_CHANGE_INFO)
@rate_limit(5, 60)
def approval(update, context):
message = update.effective_message
chat = update.effective_chat
Expand All @@ -139,6 +143,7 @@ def approval(update, context):


@kigcmd(command='unapproveall', filters=Filters.chat_type.groups)
@rate_limit(5, 60)
def unapproveall(update: Update, _: CallbackContext):
chat = update.effective_chat
user = update.effective_user
Expand Down
4 changes: 3 additions & 1 deletion tg_bot/modules/backups.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from tg_bot import dispatcher, log as LOGGER, OWNER_ID
from tg_bot.__main__ import DATA_IMPORT
from tg_bot.modules.helper_funcs.alternate import typing_action
from tg_bot.modules.helper_funcs.decorators import kigcmd
from tg_bot.modules.helper_funcs.decorators import kigcmd, rate_limit
# from tg_bot.modules.rules import get_rules
import tg_bot.modules.sql.rules_sql as rulessql
from tg_bot.modules.language import gs
Expand All @@ -29,6 +29,7 @@ def get_help(chat):
@kigcmd(command='import')
@user_admin(AdminPerms.CAN_CHANGE_INFO)
@typing_action
@rate_limit(5, 60)
def import_data(update, context):
msg = update.effective_message
chat = update.effective_chat
Expand Down Expand Up @@ -121,6 +122,7 @@ def import_data(update, context):

@kigcmd(command='export')
@user_admin(AdminPerms.CAN_CHANGE_INFO)
@rate_limit(5, 60)
def export_data(update, context): # sourcery no-metrics
chat_data = context.chat_data
msg = update.effective_message # type: Optional[Message]
Expand Down
Loading

0 comments on commit 7996255

Please sign in to comment.