-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from iAliF/main
Add 'admin_only' decorator
- Loading branch information
Showing
4 changed files
with
42 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from functools import wraps | ||
from typing import Callable | ||
|
||
from telegram import Update, ChatAction | ||
from telegram.ext import CallbackContext | ||
|
||
from users.models import User | ||
|
||
|
||
def admin_only(func: Callable): | ||
""" | ||
Admin only decorator | ||
Used for handlers that only admins have access to | ||
""" | ||
|
||
@wraps(func) | ||
def wrapper(update: Update, context: CallbackContext, *args, **kwargs): | ||
user = User.get_user(update, context) | ||
|
||
if not user.is_admin: | ||
return | ||
|
||
return func(update, context, *args, **kwargs) | ||
|
||
return wrapper | ||
|
||
|
||
def send_typing_action(func: Callable): | ||
"""Sends typing action while processing func command.""" | ||
|
||
@wraps(func) | ||
def command_func(update: Update, context: CallbackContext, *args, **kwargs): | ||
update.effective_chat.send_chat_action(ChatAction.TYPING) | ||
return func(update, context, *args, **kwargs) | ||
|
||
return command_func |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters