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 |
|
---|
12 | from telegram import Update, error
|
---|
13 | from telegram.ext import ContextTypes
|
---|
14 | from collections import deque
|
---|
15 |
|
---|
16 | from strings import USER_NOT_PROVIDED, USER_ADDED, USER_REMOVED, EXPECTED_ONE_USER
|
---|
17 | from utils import context_init
|
---|
18 |
|
---|
19 | from rich import inspect
|
---|
20 |
|
---|
21 |
|
---|
22 | async 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 |
|
---|
39 | async 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 |
|
---|
48 | async 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 |
|
---|
69 | async 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)
|
---|