Skip to content

Commit

Permalink
refactor: remove submodule "bot" from "blackjackbot"
Browse files Browse the repository at this point in the history
  • Loading branch information
d-Rickyy-b committed Aug 24, 2020
1 parent fe8299a commit 030e130
Show file tree
Hide file tree
Showing 21 changed files with 149 additions and 633 deletions.
25 changes: 24 additions & 1 deletion blackjackbot/__init__.py
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']
30 changes: 0 additions & 30 deletions blackjackbot/bot/__init__.py

This file was deleted.

9 changes: 0 additions & 9 deletions blackjackbot/bot/commands/__init__.py

This file was deleted.

1 change: 0 additions & 1 deletion blackjackbot/bot/commands/admin/__init__.py

This file was deleted.

55 changes: 0 additions & 55 deletions blackjackbot/bot/commands/admin/commands.py

This file was deleted.

1 change: 0 additions & 1 deletion blackjackbot/bot/commands/game/__init__.py

This file was deleted.

1 change: 0 additions & 1 deletion blackjackbot/bot/commands/settings/__init__.py

This file was deleted.

10 changes: 10 additions & 0 deletions blackjackbot/commands/__init__.py
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']
5 changes: 5 additions & 0 deletions blackjackbot/commands/admin/__init__.py
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']
26 changes: 26 additions & 0 deletions blackjackbot/commands/admin/commands.py
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)
14 changes: 14 additions & 0 deletions blackjackbot/commands/admin/functions.py
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)
8 changes: 8 additions & 0 deletions blackjackbot/commands/game/__init__.py
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']
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import blackjack.errors as errors
from blackjack.game import BlackJackGame
from blackjackbot.bot.commands.util import html_mention, get_game_keyboard, get_join_keyboard, get_start_keyboard, remove_inline_keyboard
from blackjackbot.commands.util import html_mention, get_game_keyboard, get_join_keyboard, get_start_keyboard, remove_inline_keyboard
from blackjackbot.commands.util.decorators import needs_active_game
from blackjackbot.errors import NoActiveGameException
from blackjackbot.gamestore import GameStore
from blackjackbot.lang import Translator
from blackjackbot.util import get_cards_string
from database import Database
from .functions import create_game, needs_active_game, players_turn, next_player
from .functions import create_game, players_turn, next_player


def start_cmd(update, context):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# -*- coding: utf-8 -*-
import functools
import logging

from telegram import InlineKeyboardButton, InlineKeyboardMarkup

from blackjack.errors import NoPlayersLeftException
from blackjack.game import BlackJackGame
from blackjackbot.bot.commands.util import remove_inline_keyboard, html_mention, get_game_keyboard, get_join_keyboard, generate_evaluation_string
from blackjackbot.errors import NoActiveGameException
from blackjackbot.commands.util.decorators import needs_active_game
from blackjackbot.commands.util import remove_inline_keyboard, html_mention, get_game_keyboard, get_join_keyboard, generate_evaluation_string
from blackjackbot.gamestore import GameStore
from blackjackbot.lang import Translator
from blackjackbot.util import get_cards_string
Expand All @@ -18,28 +17,8 @@
logging.getLogger("telegram").setLevel(logging.ERROR)


def needs_active_game(func):
@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


def players_turn(update, context):
"""Execute a player's turn"""
user = update.effective_user
chat = update.effective_chat
game = GameStore().get_game(chat.id)
player = game.get_current_player()
Expand Down
5 changes: 5 additions & 0 deletions blackjackbot/commands/settings/__init__.py
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.
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.
48 changes: 48 additions & 0 deletions blackjackbot/commands/util/decorators.py
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.
Loading

0 comments on commit 030e130

Please sign in to comment.