Skip to content

Commit 6c1b3a3

Browse files
committed
feat: implement banning of users
Important: This doesn't handle banned users correctly yet - it's just the commands for banning users
1 parent d0c1864 commit 6c1b3a3

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

blackjackbot/commands/admin/commands.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import logging
44
import re
55

6+
from telegram import ParseMode
7+
68
from blackjackbot.commands.admin import notify_admins
79
from blackjackbot.commands.util.decorators import admin_method
810
from blackjackbot.errors import NoActiveGameException
@@ -13,6 +15,48 @@
1315
logger = logging.getLogger(__name__)
1416

1517

18+
@admin_method
19+
def ban_user_cmd(update, context):
20+
"""Bans a user from using the bot"""
21+
# Try to get user_id from command
22+
if len(context.args) != 1:
23+
update.message.reply_text("Please provide a valid user_id - `/ban <user_id>`", parse_mode=ParseMode.MARKDOWN_V2)
24+
return
25+
26+
match = re.search(r"^\d+$", context.args[0])
27+
if not match:
28+
logger.error(f"The user_id did not match: {context.args}")
29+
return
30+
user_id = match.group(0)
31+
32+
db = Database()
33+
db.ban_user(user_id=user_id)
34+
35+
logger.info(f"Admin '{update.effective_user.id}' banned user '{user_id}'!")
36+
notify_admins(f"Admin '{update.effective_user.id}' banned user '{user_id}'!")
37+
38+
39+
@admin_method
40+
def unban_user_cmd(update, context):
41+
"""Unbans a user from using the bot"""
42+
# Try to get user_id from command
43+
if len(context.args) != 1:
44+
update.message.reply_text("Please provide a valid user_id - `/unban <user_id>`", parse_mode=ParseMode.MARKDOWN_V2)
45+
return
46+
47+
match = re.search(r"^\d+$", context.args[0])
48+
if not match:
49+
logger.error(f"The user_id did not match: {context.args}")
50+
return
51+
user_id = match.group(0)
52+
53+
db = Database()
54+
db.unban_user(user_id=user_id)
55+
56+
logger.info(f"Admin '{update.effective_user.id}' unbanned user '{user_id}'!")
57+
notify_admins(f"Admin '{update.effective_user.id}' unbanned user '{user_id}'!")
58+
59+
1660
@admin_method
1761
def kill_game_cmd(update, context):
1862
"""Kills the game for a certain chat/group"""

database/database.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import sqlite3
55
from time import time
6+
67
from util import Cache
78

89

@@ -63,6 +64,7 @@ def create_database(database_path):
6364
"'games_won' INTEGER DEFAULT 0,"
6465
"'games_tie' INTEGER DEFAULT 0,"
6566
"'last_played' INTEGER DEFAULT 0,"
67+
"'banned' INTEGER DEFAULT 0,"
6668
"PRIMARY KEY('user_id'));")
6769

6870
cursor.execute("CREATE TABLE IF NOT EXISTS 'chats'"
@@ -73,14 +75,29 @@ def create_database(database_path):
7375
connection.close()
7476

7577
def get_user(self, user_id):
76-
self.cursor.execute("SELECT user_id, first_name, last_name, username, games_played, games_won, games_tie, last_played"
78+
self.cursor.execute("SELECT user_id, first_name, last_name, username, games_played, games_won, games_tie, last_played, banned"
7779
" FROM users WHERE user_id=?;", [str(user_id)])
7880

7981
result = self.cursor.fetchone()
8082
if not result or len(result) == 0:
8183
return None
8284
return result
8385

86+
def is_user_banned(self, user_id):
87+
"""Checks if a user was banned by the admin of the bot from using it"""
88+
user = self.get_user(user_id)
89+
return user is not None and user[8] == 1
90+
91+
def ban_user(self, user_id):
92+
"""Bans a user from using a the bot"""
93+
self.cursor.execute("UPDATE users SET banned=1 WHERE user_id=?;", [str(user_id)])
94+
self.connection.commit()
95+
96+
def unban_user(self, user_id):
97+
"""Unbans a user from using a the bot"""
98+
self.cursor.execute("UPDATE users SET banned=0 WHERE user_id=?;", [str(user_id)])
99+
self.connection.commit()
100+
84101
def get_recent_players(self):
85102
one_day_in_secs = 60 * 60 * 24
86103
current_time = int(time())

0 commit comments

Comments
 (0)