-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: remove submodule "bot" from "blackjackbot"
- Loading branch information
1 parent
fe8299a
commit 030e130
Showing
21 changed files
with
149 additions
and
633 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,28 @@ | ||
# -*- coding: utf-8 -*- | ||
from telegram.ext import CommandHandler, CallbackQueryHandler | ||
|
||
from .bot import handlers | ||
from blackjackbot.commands import game, admin, settings, util | ||
|
||
# User commands | ||
start_command_handler = CommandHandler("start", game.start_cmd) | ||
stop_command_handler = CommandHandler("stop", game.stop_cmd) | ||
language_command_handler = CommandHandler("language", settings.language_cmd) | ||
stats_command_handler = CommandHandler("stats", util.stats_cmd) | ||
|
||
# Admin methods | ||
reload_lang_command_handler = CommandHandler("reload_lang", admin.reload_languages_cmd) | ||
users_command_handler = CommandHandler("users", admin.users_cmd) | ||
|
||
# Callback handlers | ||
hit_callback_handler = CallbackQueryHandler(game.hit_callback, pattern=r"^hit$") | ||
stand_callback_handler = CallbackQueryHandler(game.stand_callback, pattern=r"^stand$") | ||
join_callback_handler = CallbackQueryHandler(game.join_callback, pattern=r"^join$") | ||
start_callback_handler = CallbackQueryHandler(game.start_callback, pattern=r"^start$") | ||
newgame_callback_handler = CallbackQueryHandler(game.newgame_callback, pattern=r"^newgame$") | ||
language_callback_handler = CallbackQueryHandler(settings.language_callback, pattern=r"^lang_([a-z]{2}(?:-[a-z]{2})?)$") | ||
|
||
handlers = [start_command_handler, stop_command_handler, join_callback_handler, hit_callback_handler, stand_callback_handler, start_callback_handler, | ||
language_command_handler, stats_command_handler, newgame_callback_handler, reload_lang_command_handler, language_callback_handler, | ||
users_command_handler] | ||
|
||
__all__ = ['handlers'] |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,10 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from .admin import reload_languages_cmd, users_cmd | ||
from .game import start_cmd, rules_cmd, stop_cmd, start_callback, stand_callback, hit_callback, join_callback, newgame_callback, create_game, next_player, \ | ||
players_turn | ||
from .settings import language_cmd, language_callback | ||
from .util import stats_cmd, comment_cmd, comment_text | ||
|
||
__all__ = ['start_cmd', 'stop_cmd', 'stats_cmd', 'language_cmd', 'rules_cmd', 'hit_callback', 'stand_callback', 'join_callback', 'start_callback', | ||
'newgame_callback', 'language_callback', 'reload_languages_cmd', 'users_cmd', 'comment_cmd', 'comment_text'] |
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,5 @@ | ||
# -*- coding: utf-8 -*- | ||
from .commands import answer_comment_cmd, reload_languages_cmd, users_cmd | ||
from .functions import notify_admins | ||
|
||
__all__ = ['answer_comment_cmd', 'reload_languages_cmd', 'users_cmd', 'notify_admins'] |
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,26 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from blackjackbot.commands.util.decorators import admin_method | ||
from blackjackbot.lang import reload_strings | ||
from database import Database | ||
|
||
|
||
@admin_method | ||
def reload_languages_cmd(update, context): | ||
reload_strings() | ||
|
||
|
||
@admin_method | ||
def answer_comment_cmd(upate, context): | ||
pass | ||
|
||
|
||
@admin_method | ||
def users_cmd(update, context): | ||
"""Returns the amount of players in the last 24 hours""" | ||
db = Database() | ||
players = db.get_recent_players() | ||
|
||
text = "Last 24 hours: {}".format(len(players)) | ||
|
||
update.message.reply_text(text=text) |
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,14 @@ | ||
# -*- coding: utf-8 -*- | ||
from database import Database | ||
|
||
|
||
def notify_admins(text, context): | ||
""" | ||
Sends a message to all stored admins | ||
:param text: The text that is sent | ||
:param context: python-telegram-bot context object | ||
:return: | ||
""" | ||
db = Database() | ||
for admin_id in db.get_admins(): | ||
context.bot.sendMessage(admin_id, text=text) |
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,8 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from .commands import start_cmd, rules_cmd, stop_cmd | ||
from .commands import start_callback, stand_callback, hit_callback, join_callback, newgame_callback | ||
from .functions import create_game, next_player, players_turn | ||
|
||
__all__ = ['start_cmd', 'rules_cmd', 'stop_cmd', 'start_callback', 'stand_callback', 'hit_callback', 'join_callback', 'newgame_callback', | ||
'create_game', 'next_player', 'players_turn'] |
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,5 @@ | ||
# -*- coding: utf-8 -*- | ||
from .commands import language_cmd | ||
from .commands import language_callback | ||
|
||
__all__ = ['language_cmd', 'language_callback'] |
File renamed without changes.
5 changes: 4 additions & 1 deletion
5
blackjackbot/bot/commands/util/__init__.py → blackjackbot/commands/util/__init__.py
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 |
---|---|---|
@@ -1,4 +1,7 @@ | ||
# -*- coding: utf-8 -*- | ||
from .functions import remove_inline_keyboard, get_start_keyboard, generate_evaluation_string, html_mention, get_game_keyboard, get_join_keyboard | ||
from .decorators import admin_method, needs_active_game | ||
from .commands import stats_cmd, comment_cmd, comment_text | ||
|
||
__all__ = ['remove_inline_keyboard', 'get_start_keyboard', 'generate_evaluation_string', 'html_mention', 'get_game_keyboard', 'get_join_keyboard'] | ||
__all__ = ['remove_inline_keyboard', 'get_start_keyboard', 'generate_evaluation_string', 'html_mention', 'get_game_keyboard', 'get_join_keyboard', | ||
'stats_cmd', 'comment_cmd', 'comment_text', 'admin_method', 'needs_active_game'] |
File renamed without changes.
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,48 @@ | ||
# -*- coding: utf-8 -*- | ||
import functools | ||
import logging | ||
|
||
from blackjackbot.commands.util import remove_inline_keyboard | ||
from blackjackbot.errors import NoActiveGameException | ||
from blackjackbot.gamestore import GameStore | ||
from blackjackbot.lang import Translator | ||
from database import Database | ||
|
||
|
||
def admin_method(func): | ||
"""Decorator for marking methods as admin-only methods, so that strangers can't use them""" | ||
|
||
def admin_check(update, context): | ||
user = update.effective_user | ||
chat = update.effective_chat | ||
lang_id = Database().get_lang_id(chat.id) | ||
translator = Translator(lang_id=lang_id) | ||
|
||
if user.id in Database().get_admins(): | ||
return func(update, context) | ||
else: | ||
update.message.reply_text(translator("no_permission")) | ||
logging.warning("User {} ({}, @{}) tried to use admin function '{}'!".format(user.id, user.first_name, user.username, func.__name__)) | ||
|
||
return admin_check | ||
|
||
|
||
def needs_active_game(func): | ||
"""Decorator for making sure a game exists for a certain chat""" | ||
|
||
@functools.wraps(func) | ||
def wrapper(update, context, *args, **kwargs): | ||
chat = update.effective_chat | ||
lang_id = Database().get_lang_id(chat.id) | ||
translator = Translator(lang_id=lang_id) | ||
|
||
try: | ||
game = GameStore().get_game(chat.id) | ||
except NoActiveGameException: | ||
remove_inline_keyboard(update, context) | ||
update.effective_message.reply_text(translator("mp_no_created_game_callback")) | ||
return | ||
|
||
return func(update, context) | ||
|
||
return wrapper |
File renamed without changes.
Oops, something went wrong.