Skip to content

Commit c0f2aa2

Browse files
committed
feat: add method for bot maintainer to kill a game
1 parent 0219be4 commit c0f2aa2

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

blackjackbot/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
reload_lang_command_handler = CommandHandler("reload_lang", admin.reload_languages_cmd)
1717
users_command_handler = CommandHandler("users", admin.users_cmd)
1818
answer_command_handler = CommandHandler("answer", admin.answer_comment_cmd, Filters.reply)
19+
kill_command_handler = CommandHandler("kill", admin.kill_game_cmd, Filters.text)
1920

2021
# Callback handlers
2122
hit_callback_handler = CallbackQueryHandler(game.hit_callback, pattern=r"^hit_[0-9]{7}$")
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -*- coding: utf-8 -*-
22
from .functions import notify_admins
3-
from .commands import answer_comment_cmd, reload_languages_cmd, users_cmd
3+
from .commands import answer_comment_cmd, reload_languages_cmd, users_cmd, kill_game_cmd
44

5-
__all__ = ['answer_comment_cmd', 'reload_languages_cmd', 'users_cmd', 'notify_admins']
5+
__all__ = ["answer_comment_cmd", "reload_languages_cmd", "users_cmd", "notify_admins", "kill_game_cmd"]

blackjackbot/commands/admin/commands.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,40 @@
55

66
from blackjackbot.commands.admin import notify_admins
77
from blackjackbot.commands.util.decorators import admin_method
8+
from blackjackbot.errors import NoActiveGameException
9+
from blackjackbot.gamestore import GameStore
810
from blackjackbot.lang import reload_strings, Translator
911
from database import Database
1012

1113
logger = logging.getLogger(__name__)
1214

1315

16+
@admin_method
17+
def kill_game_cmd(update, context):
18+
"""Kills the game for a certain chat/group"""
19+
if len(context.args) == 0:
20+
update.message.reply_text("Please provide a chat_id!")
21+
22+
chat_id = context.args[0]
23+
# Input validation for chat_id
24+
if not re.match(r"^-?[0-9]+$", chat_id):
25+
update.message.reply_text("Sorry, the chat_id is invalid!")
26+
return
27+
28+
chat_id = int(chat_id)
29+
30+
try:
31+
_ = GameStore().get_game(chat_id=chat_id)
32+
except NoActiveGameException:
33+
update.message.reply_text("Sorry, there is no running game in a chat with that ID!")
34+
return
35+
36+
logger.info("Admin '{0}' removed game in chat '{1}'".format(update.effective_user.id, chat_id))
37+
GameStore().remove_game(chat_id=chat_id)
38+
update.message.reply_text("Alright, I killed the running game in '{0}'!".format(chat_id))
39+
context.bot.send_message(chat_id=chat_id, text="The creator of this bot stopped your current game of BlackJack.")
40+
41+
1442
@admin_method
1543
def reload_languages_cmd(update, context):
1644
reload_strings()

0 commit comments

Comments
 (0)