Skip to content

Commit 05e8daa

Browse files
committed
feat: quality of life updates
Merge pull request #101 from d-Rickyy-b/dev
2 parents 5f9c8e3 + d0c1864 commit 05e8daa

File tree

18 files changed

+322
-27
lines changed

18 files changed

+322
-27
lines changed

.dockerignore

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
env/
12+
build/
13+
develop-eggs/
14+
dist/
15+
downloads/
16+
eggs/
17+
.eggs/
18+
lib/
19+
lib64/
20+
parts/
21+
sdist/
22+
var/
23+
*.egg-info/
24+
.installed.cfg
25+
*.egg
26+
27+
# PyInstaller
28+
# Usually these files are written by a python script from a template
29+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
30+
*.manifest
31+
*.spec
32+
33+
# Installer logs
34+
pip-log.txt
35+
pip-delete-this-directory.txt
36+
37+
# Unit test / coverage reports
38+
htmlcov/
39+
.tox/
40+
.coverage
41+
.coverage.*
42+
.cache
43+
nosetests.xml
44+
coverage.xml
45+
*,cover
46+
.hypothesis/
47+
48+
# Translations
49+
*.mo
50+
*.pot
51+
52+
# Django stuff:
53+
*.log
54+
local_settings.py
55+
56+
# Flask stuff:
57+
instance/
58+
.webassets-cache
59+
60+
# Scrapy stuff:
61+
.scrapy
62+
63+
# Sphinx documentation
64+
docs/_build/
65+
66+
# PyBuilder
67+
target/
68+
69+
# IPython Notebook
70+
.ipynb_checkpoints
71+
72+
# pyenv
73+
.python-version
74+
75+
# celery beat schedule file
76+
celerybeat-schedule
77+
78+
# dotenv
79+
.env
80+
81+
# virtualenv
82+
venv/
83+
ENV/
84+
.venv
85+
86+
# Spyder project settings
87+
.spyderproject
88+
89+
# Rope project settings
90+
.ropeproject
91+
92+
93+
.idea
94+
.svn
95+
.git
96+
__pycache__
97+
*.db
98+
.vs
99+
.github
100+
101+
# Keep logrotated files out of git
102+
logs/*.log*
103+
config.py
104+
105+
*.md
106+
LICENSE
107+
.travis.yml
108+
*_test.py
File renamed without changes.

Dockerfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM python:3-slim
2+
3+
LABEL maintainer="d-Rickyy-b <[email protected]>"
4+
LABEL site="https://github.com/d-Rickyy-b/Python-BlackJackBot"
5+
6+
RUN mkdir -p /blackjackbot/logs
7+
COPY . /blackjackbot
8+
WORKDIR /blackjackbot
9+
RUN pip install --no-cache-dir -r /blackjackbot/requirements.txt
10+
11+
CMD ["python", "/blackjackbot/bot.py"]

README.md

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,31 @@
1-
[![Build Status](https://travis-ci.com/d-Rickyy-b/Python-BlackJackBot.svg?branch=master)](https://travis-ci.com/d-Rickyy-b/Python-BlackJackBot)
1+
2+
[![Build Status](https://github.com/d-Rickyy-b/Python-BlackJackBot/actions/workflows/python-lint-test.yml/badge.svg)](https://github.com/d-Rickyy-b/Python-BlackJackBot/actions/workflows/python-lint-test.yml)
23
[![Coverage Status](https://coveralls.io/repos/github/d-Rickyy-b/Python-BlackJackBot/badge.svg?branch=rebuild)](https://coveralls.io/github/d-Rickyy-b/Python-BlackJackBot?branch=rebuild)
34
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/12996d68fc0f436085221ac6b1f525f9)](https://www.codacy.com/manual/d-Rickyy-b/Python-BlackJackBot?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=d-Rickyy-b/Python-BlackJackBot&amp;utm_campaign=Badge_Grade)
45

56
# Python-BlackJackBot
67

7-
This is the code for my Telegram Bot with which you can play the game Black Jack. You can find it here: https://telegram.me/BlackJackBot
8-
9-
The main file, which needs to be executed is 'main.py'. Please create a copy of the config.sample.py, name it config.py and enter the config data (e.g. bot
10-
token).
8+
This is the code for my Telegram Bot with which you can play the game Black Jack.
9+
You can find the hosted version of it here: https://telegram.me/BlackJackBot
1110

1211
## Setup
12+
This project is really easy to set up. No matter which of the following ways you'll use, you'll always need a config file.
13+
To create one, simply copy the existing `config.sample.py` file and name it `config.py`. Enter your bot token and make your changes accordingly.
1314

14-
The bot uses the [python-telegram-bot](https://python-telegram-bot.org/) framework to make Telegram API calls. You can install it like that:
15+
Then you're left with several ways to run this bot.
16+
17+
### 1.) Cloning the repo
18+
If you want to run this code from source, you can just `git clone` this repo.
19+
It's recommended to create a new virtual environment (`python3 -m venv /path/to/venv`).
20+
This bot uses the [python-telegram-bot](https://python-telegram-bot.org/) framework to make Telegram API calls.
21+
You can install it (and potential other requlrements) like that:
1522

1623
``pip install -r requirements.txt``
1724

18-
## Database
25+
Afterwards just run `python3 bot.py` and if done right, you'll be left with a working bot.
26+
27+
### 2.) Docker
28+
This project also contains a `Dockerfile` as well as a pre-built [Docker image](https://hub.docker.com/repository/docker/0rickyy0/blackjackbot) hosted on the official Docker Hub.
1929

20-
The bot uses a SQLite database. The database file is in the "database" directory. It is called 'users.db'. The database gets auto-generated, if it doesn't exist. Make sure the program has write access to the database directory.
30+
You will also find the `docker-compose.yml` file with which you can easily set up your own instance of the bot.
31+
Just specify the path to your config etc. in said docker-compose file.

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: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,44 @@
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()
45+
update.message.reply_text("Reloaded languages & strings!")
1746

1847

1948
@admin_method
@@ -48,7 +77,7 @@ def answer_comment_cmd(update, context):
4877

4978
chat_id = user[0]
5079

51-
if not re.match(r"^\d+$", chat_id):
80+
if not re.match(r"^-?\d+$", chat_id):
5281
update.message.reply_text("⚠ Malformed chat_id!")
5382
logger.error("Malformed chat_id: {}".format(chat_id))
5483
return

blackjackbot/commands/game/commands.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,16 @@ def stop_cmd(update, context):
8181

8282
game = GameStore().get_game(chat.id)
8383

84+
user_id = user.id
8485
try:
85-
game.stop(user.id)
86+
if chat.type == "group" or chat.type == "supergroup":
87+
# If yes, get the chat admins
88+
admins = context.bot.get_chat_administrators(chat_id=chat.id)
89+
# if user.id in chat admin IDs, let them end the game with admin powers
90+
if user.id in [x.user.id for x in admins]:
91+
user_id = -1
92+
93+
game.stop(user_id)
8694
update.effective_message.reply_text(translator("game_ended"))
8795
except errors.InsufficientPermissionsException:
8896
update.effective_message.reply_text(translator("mp_only_creator_can_end"))
@@ -178,8 +186,6 @@ def stand_callback(update, context):
178186
if not is_button_affiliated(update, context, game, lang_id):
179187
return
180188

181-
remove_inline_keyboard(update, context)
182-
183189
next_player(update, context)
184190

185191

blackjackbot/commands/game/functions.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
from database import Database
1414

1515
logger = logging.getLogger(__name__)
16-
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.DEBUG)
17-
logging.getLogger("telegram").setLevel(logging.ERROR)
1816

1917

2018
def is_button_affiliated(update, context, game, lang_id):
@@ -72,6 +70,7 @@ def next_player(update, context):
7270
update.callback_query.answer(translator("mp_not_your_turn_callback").format(user.first_name))
7371
return
7472

73+
remove_inline_keyboard(update, context)
7574
game.next_player()
7675
except NoPlayersLeftException:
7776
# TODO merge messages

blackjackbot/commands/util/commands.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ def comment_text(update, context):
3737
chat = update.effective_chat
3838
lang_id = Database().get_lang_id(chat.id)
3939

40-
data = [chat.id, user.id, user.first_name, user.last_name, "@" + user.username, user.language_code]
40+
# username can be None, so we need to use str()
41+
data = [chat.id, user.id, user.first_name, user.last_name, "@" + str(user.username), user.language_code]
4142

4243
userdata = " | ".join([str(item) for item in data])
4344
userdata = userdata.replace("\r", "").replace("\n", "")

0 commit comments

Comments
 (0)