Skip to content

Commit

Permalink
restrict subscriptions commands to admin only (#41)
Browse files Browse the repository at this point in the history
* admin restricting is now usable for other groups and private chats
* restrict subscription commands to admins
  • Loading branch information
rodrigondec authored Jan 17, 2020
1 parent 7f54c84 commit 9c0e0eb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
16 changes: 16 additions & 0 deletions bot/commands/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from telegram.ext import CommandHandler, CallbackContext

from bot.core import BotTelegramCore
from bot.messages import ADMIN_RESTRICTED
from utils.utils import build_menu
from db.subject import Subject
from db.observer import UserObserver
Expand All @@ -19,9 +20,14 @@


def subscribe(update: Update, context: CallbackContext):
user = update.effective_user
chat = update.effective_chat
message = update.effective_message

if not BotTelegramCore.instance().is_admin(user.id, chat.id):
message.reply_text(ADMIN_RESTRICTED)
return

try:
observer = UserObserver.objects.get(chat_id=f"{chat.id}")
except DoesNotExist:
Expand Down Expand Up @@ -49,9 +55,14 @@ def subscribe(update: Update, context: CallbackContext):


def unsubscribe(update: Update, context: CallbackContext):
user = update.effective_user
chat = update.effective_chat
message = update.effective_message

if not BotTelegramCore.instance().is_admin(user.id, chat.id):
message.reply_text(ADMIN_RESTRICTED)
return

try:
observer = UserObserver.objects.get(chat_id=f"{chat.id}")
except DoesNotExist:
Expand Down Expand Up @@ -79,9 +90,14 @@ def unsubscribe(update: Update, context: CallbackContext):


def subscriptions(update: Update, context: CallbackContext):
user = update.effective_user
chat = update.effective_chat
message = update.effective_message

if not BotTelegramCore.instance().is_admin(user.id, chat.id):
message.reply_text(ADMIN_RESTRICTED)
return

try:
observer = UserObserver.objects.get(chat_id=f"{chat.id}")
except DoesNotExist:
Expand Down
22 changes: 15 additions & 7 deletions bot/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from decouple import config
from telegram.ext import Updater, Handler
from telegram import Update, ParseMode
from telegram import Update, ParseMode, TelegramError


logging.basicConfig(
Expand Down Expand Up @@ -49,20 +49,28 @@ def chat_id(self):

@property
def chat(self):
return self._updater.bot.get_chat(self.chat_id)
return self.get_chat(self.chat_id)

def get_chat(self, chat_id):
return self._updater.bot.get_chat(chat_id)

def is_from_official_chat(self, update: Update):
return self.chat_id == update.message.chat.id

@property
def administrators(self):
@staticmethod
def get_administrators_ids(chat):
return [chat_member.user.id for
chat_member in self.chat.get_administrators()]
chat_member in chat.get_administrators()]

@classmethod
def is_admin(cls, user_id):
def is_admin(cls, user_id, chat_id=None):
instance = cls.instance()
return user_id in instance.administrators
try:
chat = instance.get_chat(chat_id)
except TelegramError:
chat = instance.chat
return (chat.type == chat.PRIVATE or
user_id in instance.get_administrators_ids(chat))

@classmethod
def send_message(cls, text, chat_id, parse_mode=None):
Expand Down
1 change: 1 addition & 0 deletions bot/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
"Read my documentation or create a issue on " \
"https://github.com/dcr-guys/JackBot/"

ADMIN_RESTRICTED = "This command is restricted to admins only!"

0 comments on commit 9c0e0eb

Please sign in to comment.