source: python-cinema-club-bot/commands/users.py@ cc34991

Last change on this file since cc34991 was cc34991, checked in by Mikhail Kirillov <w96k@…>, on Oct 9, 2024 at 3:09:50 AM

Add commands modules; Added /chooser command

  • Property mode set to 100644
File size: 2.6 KB
Line 
1# This file is part of python-cinema-club-bot
2# contributed in 2024 by Mikhail Kirillov (~w96k) <w96k@runbox.com>
3
4# To the extent possible under law, the author(s) have dedicated all copyright
5# and related and neighboring rights to this software to the public domain
6# worldwide. This software is distributed without any warranty.
7
8# You should have received a copy of the CC0 Public Domain Dedication along
9# with this software. If not, see:
10# <http://creativecommons.org/publicdomain/zero/1.0/>
11
12from telegram import Update, error
13from telegram.ext import ContextTypes
14from collections import deque
15
16from strings import USER_NOT_PROVIDED, USER_ADDED, USER_REMOVED, EXPECTED_ONE_USER
17from utils import context_init
18
19from rich import inspect
20
21
22async def add_users(
23 update: Update,
24 context: ContextTypes.DEFAULT_TYPE
25) -> None:
26 context_init(context)
27
28 if context.args == []:
29 await update.message.reply_text(USER_NOT_PROVIDED)
30 raise error.TelegramError(USER_NOT_PROVIDED)
31
32 for user in context.args:
33 context.chat_data["users"].append(user)
34 await update.message.reply_text(USER_ADDED.format(user=user))
35
36 await update.message.reply_text(context.chat_data["users"])
37
38
39async def list_users(
40 update: Update,
41 context: ContextTypes.DEFAULT_TYPE
42) -> None:
43 context_init(context)
44
45 await update.message.reply_text(context.chat_data["users"])
46
47
48async def remove_users(
49 update: Update,
50 context: ContextTypes.DEFAULT_TYPE
51) -> None:
52 context_init(context)
53
54 if context.args == []:
55 await update.message.reply_text(USER_NOT_PROVIDED)
56 raise error.TelegramError(USER_NOT_PROVIDED)
57
58 for user in context.args:
59 if user == "*":
60 context.chat_data["users"] = []
61 break
62
63 context.chat_data["users"].remove(user)
64 await update.message.reply_text(USER_REMOVED.format(user=user))
65
66 await update.message.reply_text(context.chat_data["users"])
67
68
69async def chooser_user(
70 update: Update,
71 context: ContextTypes.DEFAULT_TYPE
72) -> None:
73 context_init(context)
74
75 if context.args == []:
76 await update.message.reply_text(USER_NOT_PROVIDED)
77 raise error.TelegramError(USER_NOT_PROVIDED)
78
79 if len(context.args) > 1:
80 await update.message.reply_text(EXPECTED_ONE_USER)
81 raise error.TelegramError(EXPECTED_ONE_USER)
82
83 chooser = context.args[0]
84 users = deque(context.chat_data["users"])
85
86 chooser_index = users.index(chooser)
87 users.rotate(-chooser_index)
88
89 context.chat_data["users"] = list(users)
90
91 await update.message.reply_text(context.chat_data)
Note: See TracBrowser for help on using the repository browser.