Skip to content

Commit

Permalink
tgbot: major change
Browse files Browse the repository at this point in the history
  • Loading branch information
soulr344 committed Nov 1, 2020
1 parent 95d476d commit 794519d
Show file tree
Hide file tree
Showing 13 changed files with 299 additions and 75 deletions.
2 changes: 2 additions & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
worker: python -m tg_bot
web: python -m tg_bot
43 changes: 32 additions & 11 deletions tg_bot/modules/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from telegram.ext.dispatcher import run_async
from telegram.utils.helpers import escape_markdown, mention_html

from tg_bot import dispatcher
from tg_bot import dispatcher, SUDO_USERS
from tg_bot.modules.disable import DisableAbleCommandHandler
from tg_bot.modules.helper_funcs.chat_status import bot_admin, can_promote, user_admin, can_pin
from tg_bot.modules.helper_funcs.extraction import extract_user
Expand All @@ -27,7 +27,12 @@ def promote(bot: Bot, update: Update, args: List[str]) -> str:
user = update.effective_user # type: Optional[User]

user_id = extract_user(message, args)
if not user_id or int(user_id) == 1087968824:
admin = chat.get_member(int(user.id))
if ( not admin.can_promote_members ) and ( not int(user.id) in SUDO_USERS ):
message.reply_text("You don't have sufficient permissions to promote users!")
return ""

if not user_id or int(user_id) == 777000 or int(user_id) == 1087968824:
message.reply_text("You don't seem to be referring to a user.")
return ""

Expand Down Expand Up @@ -73,7 +78,12 @@ def demote(bot: Bot, update: Update, args: List[str]) -> str:
user = update.effective_user # type: Optional[User]

user_id = extract_user(message, args)
if not user_id or int(user_id) == 1087968824 or int(user_id) == 777000:
admin = chat.get_member(int(user.id))
if ( not admin.can_promote_members ) and ( not int(user.id) in SUDO_USERS ):
message.reply_text("You don't have sufficient permissions to demote users!")
return ""

if not user_id or int(user_id) == 777000 or int(user_id) == 1087968824:
message.reply_text("You don't seem to be referring to a user.")
return ""

Expand All @@ -92,14 +102,15 @@ def demote(bot: Bot, update: Update, args: List[str]) -> str:

try:
bot.promoteChatMember(int(chat.id), int(user_id),
can_change_info=False,
can_post_messages=False,
can_edit_messages=False,
can_delete_messages=False,
can_invite_users=False,
can_restrict_members=False,
can_pin_messages=False,
can_promote_members=False)
can_change_info=False,
can_post_messages=False,
can_edit_messages=False,
can_delete_messages=False,
can_invite_users=False,
can_restrict_members=False,
can_pin_messages=False,
can_promote_members=False)

message.reply_text("Successfully demoted!")
return "<b>{}:</b>" \
"\n#DEMOTED" \
Expand All @@ -123,6 +134,11 @@ def pin(bot: Bot, update: Update, args: List[str]) -> str:
user = update.effective_user # type: Optional[User]
chat = update.effective_chat # type: Optional[Chat]

admin = chat.get_member(int(user.id))
if ( not admin.can_pin_messages ) and ( not int(user.id) in SUDO_USERS ):
update.effective_message.reply_text("You don't have sufficient permissions to pin messages!")
return ""

is_group = chat.type != "private" and chat.type != "channel"

prev_message = update.effective_message.reply_to_message
Expand Down Expand Up @@ -155,6 +171,11 @@ def unpin(bot: Bot, update: Update) -> str:
chat = update.effective_chat
user = update.effective_user # type: Optional[User]

admin = chat.get_member(int(user.id))
if ( not admin.can_pin_messages ) and ( not int(user.id) in SUDO_USERS ):
update.effective_message.reply_text("You don't have sufficient permissions to unpin messages!")
return ""

try:
bot.unpinChatMessage(chat.id)
except BadRequest as excp:
Expand Down
6 changes: 5 additions & 1 deletion tg_bot/modules/android.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@
def magisk(bot, update):
url = 'https://raw.githubusercontent.com/topjohnwu/magisk_files/'
releases = ""
for type, branch in {"Stable":["master/stable","master"], "Beta":["master/beta","master"], "Canary":["canary/debug","canary"]}.items():
for type, branch in {"Stable":["master/stable","master"], "Beta":["master/beta","master"], "Canary":["canary/canary","canary"]}.items():
data = get(url + branch[0] + '.json').json()
if str(type) == "Canary":
data["magisk"]["link"] = "https://github.com/topjohnwu/magisk_files/raw/canary/" + data["magisk"]["link"]
data["app"]["link"] = "https://github.com/topjohnwu/magisk_files/raw/canary/" + data["app"]["link"]
data["uninstaller"]["link"] = "https://github.com/topjohnwu/magisk_files/raw/canary/" + data["uninstaller"]["link"]
releases += f'*{type}*: \n' \
f'• [Changelog](https://github.com/topjohnwu/magisk_files/blob/{branch[1]}/notes.md)\n' \
f'• Zip - [{data["magisk"]["version"]}-{data["magisk"]["versionCode"]}]({data["magisk"]["link"]}) \n' \
Expand Down
89 changes: 89 additions & 0 deletions tg_bot/modules/antiarabic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import html
from typing import Optional, List
from telegram import Message, Chat, Update, Bot, User, ParseMode
from telegram.ext import CommandHandler, MessageHandler, run_async, Filters
from telegram.utils.helpers import mention_html
from tg_bot import dispatcher, LOGGER, SUDO_USERS
from tg_bot.modules.helper_funcs.chat_status import user_not_admin, user_admin, can_delete, is_user_admin
from tg_bot.modules.log_channel import loggable
from tg_bot.modules.helper_funcs.extraction import extract_text
from tg_bot.modules.sql import antiarabic_sql as sql

ANTIARABIC_GROUPS = 11

@run_async
@user_admin
def antiarabic_setting(bot: Bot, update: Update, args: List[str]):
chat = update.effective_chat # type: Optional[Chat]
msg = update.effective_message # type: Optional[Message]
user = update.effective_user
member = chat.get_member(int(user.id))

if chat.type != chat.PRIVATE:
if len(args) >= 1:
if args[0] in ("yes", "on"):
sql.set_chat_setting(chat.id, True)
msg.reply_text("Turned on AntiArabic! Messages sent by any non-admin which contains arabic text "
"will be deleted.")

elif args[0] in ("no", "off"):
sql.set_chat_setting(chat.id, False)
msg.reply_text("Turned off AntiArabic! Messages containing arabic text won't be deleted.")
else:
msg.reply_text("This chat's current setting is: `{}`".format(sql.chat_antiarabic(chat.id)),
parse_mode=ParseMode.MARKDOWN)

@user_not_admin
@run_async
def antiarabic(bot: Bot, update: Update):
chat = update.effective_chat # type: Optional[Chat]
msg = update.effective_message # type: Optional[Message]
to_match = extract_text(msg)
user = update.effective_user # type: Optional[User]
has_arabic = False

if not sql.chat_antiarabic(chat.id):
return ""

if not user.id or int(user.id) == 777000 or int(user.id) == 1087968824:
return ""

if not to_match:
return

if chat.type != chat.PRIVATE:
for c in to_match:
if ('\u0600' <= c <= '\u06FF' or
'\u0750' <= c <= '\u077F' or
'\u08A0' <= c <= '\u08FF' or
'\uFB50' <= c <= '\uFDFF' or
'\uFE70' <= c <= '\uFEFF' or
'\U00010E60' <= c <= '\U00010E7F' or
'\U0001EE00' <= c <= '\U0001EEFF'):
if can_delete(chat, bot.id):
update.effective_message.delete()
return ""


def __migrate__(old_chat_id, new_chat_id):
sql.migrate_chat(old_chat_id, new_chat_id)


def __chat_settings__(chat_id, user_id):
return "This chat is setup to delete messages containing Arabic: `{}`".format(
sql.chat_antiarabic(chat_id))


__mod_name__ = "AntiArabicScript"

__help__ = """
Just Anti-arabic script
"""

SETTING_HANDLER = CommandHandler("antiarabic", antiarabic_setting, pass_args=True)
ANTI_ARABIC = MessageHandler(
(Filters.text | Filters.command | Filters.sticker | Filters.photo) & Filters.group, antiarabic, edited_updates=True)


dispatcher.add_handler(SETTING_HANDLER)
dispatcher.add_handler(ANTI_ARABIC, group=ANTIARABIC_GROUPS)
12 changes: 11 additions & 1 deletion tg_bot/modules/antiflood.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def check_flood(bot: Bot, update: Update) -> str:
chat = update.effective_chat # type: Optional[Chat]
msg = update.effective_message # type: Optional[Message]

if not user: # ignore channels
if int(user.id) == 777000 or int(user.id) == 1087968824 or not user: # ignore channels
return ""

# ignore admins
Expand Down Expand Up @@ -68,6 +68,11 @@ def set_flood(bot: Bot, update: Update, args: List[str]) -> str:
user = update.effective_user # type: Optional[User]
message = update.effective_message # type: Optional[Message]

admin = chat.get_member(int(user.id))
if ( not admin.can_restrict_members ) and ( not int(user.id) in SUDO_USERS ):
update.effective_message.reply_text("You don't have sufficient permissions to restrict users!")
return ""

if len(args) >= 1:
val = args[0].lower()
if val == "off" or val == "no" or val == "0":
Expand Down Expand Up @@ -129,6 +134,11 @@ def set_flood_strength(bot: Bot, update: Update, args: List[str]):
user = update.effective_user # type: Optional[User]
msg = update.effective_message # type: Optional[Message]

admin = chat.get_member(int(user.id))
if ( not admin.can_restrict_members ) and ( not int(user.id) in SUDO_USERS ):
update.effective_message.reply_text("You don't have sufficient permissions to restrict users!")
return ""

if args:
if args[0].lower() in ("on", "yes"):
sql.set_flood_strength(chat.id, False)
Expand Down
26 changes: 23 additions & 3 deletions tg_bot/modules/bans.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from telegram.utils.helpers import mention_html
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, ParseMode, User, CallbackQuery

from tg_bot import dispatcher, BAN_STICKER, LOGGER
from tg_bot import dispatcher, BAN_STICKER, LOGGER, SUDO_USERS
from tg_bot.modules.disable import DisableAbleCommandHandler
from tg_bot.modules.helper_funcs.chat_status import bot_admin, user_admin, is_user_ban_protected, can_restrict, \
is_user_admin, is_user_in_chat, is_bot_admin
Expand All @@ -27,6 +27,11 @@ def ban(bot: Bot, update: Update, args: List[str]) -> str:
user = update.effective_user # type: Optional[User]
message = update.effective_message # type: Optional[Message]

admin = chat.get_member(int(user.id))
if ( not admin.can_restrict_members ) and ( not int(user.id) in SUDO_USERS ):
update.effective_message.reply_text("You don't have sufficient permissions to restrict users!")
return ""

banType = 0
userIds = extract_multiple_users(message, args)
allTrue = True
Expand Down Expand Up @@ -134,7 +139,12 @@ def temp_ban(bot: Bot, update: Update, args: List[str]) -> str:

user_id, reason = extract_user_and_text(message, args)

if not user_id or int(user_id)==777000 or int(user_id) == 1087968824:
admin = chat.get_member(int(user.id))
if ( not admin.can_restrict_members ) and ( not int(user.id) in SUDO_USERS ):
update.effective_message.reply_text("You don't have sufficient permissions to restrict users!")
return ""

if not user_id or int(user_id) == 777000 or int(user_id) == 1087968824:
message.reply_text("You don't seem to be referring to a user.")
return ""

Expand Down Expand Up @@ -214,6 +224,11 @@ def kick(bot: Bot, update: Update, args: List[str]) -> str:
user = update.effective_user # type: Optional[User]
message = update.effective_message # type: Optional[Message]

admin = chat.get_member(int(user.id))
if ( not admin.can_restrict_members ) and ( not int(user.id) in SUDO_USERS ):
update.effective_message.reply_text("You don't have sufficient permissions to restrict users!")
return ""

banType = 0
userIds = extract_multiple_users(message, args)
allTrue = True
Expand Down Expand Up @@ -349,7 +364,12 @@ def unban(bot: Bot, update: Update, args: List[str]) -> str:

user_id, reason = extract_user_and_text(message, args)

if not user_id or int(user_id)==777000 or int(user_id) == 1087968824:
admin = chat.get_member(int(user.id))
if ( not admin.can_restrict_members ) and ( not int(user.id) in SUDO_USERS ):
update.effective_message.reply_text("You don't have sufficient permissions to restrict users!")
return ""

if not user_id or int(user_id) == 777000 or int(user_id) == 1087968824:
message.reply_text("You don't seem to be referring to a user.")
return ""

Expand Down
19 changes: 18 additions & 1 deletion tg_bot/modules/blacklist.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from telegram.ext import CommandHandler, MessageHandler, Filters, run_async

import tg_bot.modules.sql.blacklist_sql as sql
from tg_bot import dispatcher, LOGGER
from tg_bot import dispatcher, LOGGER, SUDO_USERS
from tg_bot.modules.disable import DisableAbleCommandHandler
from tg_bot.modules.helper_funcs.chat_status import user_admin, user_not_admin
from tg_bot.modules.helper_funcs.extraction import extract_text
Expand Down Expand Up @@ -48,6 +48,12 @@ def add_blacklist(bot: Bot, update: Update):
msg = update.effective_message # type: Optional[Message]
chat = update.effective_chat # type: Optional[Chat]
words = msg.text.split(None, 1)

admin = chat.get_member(int(user.id))
if ( not admin.can_delete_messages ) and ( not int(user.id) in SUDO_USERS ):
update.effective_message.reply_text("You don't have sufficient permissions to restrict users!")
return ""

if len(words) > 1:
text = words[1]
to_blacklist = list(set(trigger.strip() for trigger in text.split("\n") if trigger.strip()))
Expand All @@ -72,6 +78,12 @@ def unblacklist(bot: Bot, update: Update):
msg = update.effective_message # type: Optional[Message]
chat = update.effective_chat # type: Optional[Chat]
words = msg.text.split(None, 1)

admin = chat.get_member(int(user.id))
if ( not admin.can_delete_messages ) and ( not int(user.id) in SUDO_USERS ):
update.effective_message.reply_text("You don't have sufficient permissions to restrict users!")
return ""

if len(words) > 1:
text = words[1]
to_unblacklist = list(set(trigger.strip() for trigger in text.split("\n") if trigger.strip()))
Expand Down Expand Up @@ -113,6 +125,11 @@ def del_blacklist(bot: Bot, update: Update):
chat = update.effective_chat # type: Optional[Chat]
message = update.effective_message # type: Optional[Message]
to_match = extract_text(message)
user = update.effective_user # type: Optional[User]

if not user.id or int(user.id) == 777000 or int(user.id) == 1087968824:
return ""

if not to_match:
return

Expand Down
Loading

0 comments on commit 794519d

Please sign in to comment.