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, USERS_ADDED, USERS_REMOVED, \
|
---|
17 | EXPECTED_ONE_USER, USER_SET, USER_ADD, USER_REMOVE
|
---|
18 | from utils import context_init
|
---|
19 |
|
---|
20 |
|
---|
21 | async def set_users(
|
---|
22 | update: Update,
|
---|
23 | context: ContextTypes.DEFAULT_TYPE
|
---|
24 | ) -> None:
|
---|
25 | context_init(context)
|
---|
26 |
|
---|
27 | if context.args == []:
|
---|
28 | raise error.TelegramError(USER_NOT_PROVIDED)
|
---|
29 |
|
---|
30 | context.chat_data["users"] = context.args
|
---|
31 |
|
---|
32 | await update.message.reply_text(USER_SET)
|
---|
33 |
|
---|
34 | async def add_users(
|
---|
35 | update: Update,
|
---|
36 | context: ContextTypes.DEFAULT_TYPE
|
---|
37 | ) -> None:
|
---|
38 | context_init(context)
|
---|
39 |
|
---|
40 | if context.args == []:
|
---|
41 | raise error.TelegramError(USER_NOT_PROVIDED)
|
---|
42 |
|
---|
43 | for user in context.args:
|
---|
44 | context.chat_data["users"].append(user)
|
---|
45 | await update.message.reply_text(USER_ADD.format(user=user))
|
---|
46 |
|
---|
47 | await update.message.reply_text(USERS_ADDED)
|
---|
48 |
|
---|
49 |
|
---|
50 | async def list_users(
|
---|
51 | update: Update,
|
---|
52 | context: ContextTypes.DEFAULT_TYPE
|
---|
53 | ) -> None:
|
---|
54 | context_init(context)
|
---|
55 |
|
---|
56 | await update.message.reply_text(context.chat_data["users"])
|
---|
57 |
|
---|
58 |
|
---|
59 | async def remove_users(
|
---|
60 | update: Update,
|
---|
61 | context: ContextTypes.DEFAULT_TYPE
|
---|
62 | ) -> None:
|
---|
63 | context_init(context)
|
---|
64 |
|
---|
65 | if context.args == []:
|
---|
66 | raise error.TelegramError(USER_NOT_PROVIDED)
|
---|
67 |
|
---|
68 | for user in context.args:
|
---|
69 | if user == "*":
|
---|
70 | context.chat_data["users"] = []
|
---|
71 | break
|
---|
72 |
|
---|
73 | context.chat_data["users"].remove(user)
|
---|
74 | await update.message.reply_text(USER_REMOVE.format(user=user))
|
---|
75 |
|
---|
76 | await update.message.reply_text(USERS_REMOVED)
|
---|
77 |
|
---|
78 |
|
---|
79 | async def chooser_user(
|
---|
80 | update: Update,
|
---|
81 | context: ContextTypes.DEFAULT_TYPE
|
---|
82 | ) -> None:
|
---|
83 | context_init(context)
|
---|
84 |
|
---|
85 | if context.args == []:
|
---|
86 | raise error.TelegramError(USER_NOT_PROVIDED)
|
---|
87 |
|
---|
88 | if len(context.args) > 1:
|
---|
89 | raise error.TelegramError(EXPECTED_ONE_USER)
|
---|
90 |
|
---|
91 | chooser = context.args[0]
|
---|
92 | users = deque(context.chat_data["users"])
|
---|
93 |
|
---|
94 | chooser_index = users.index(chooser)
|
---|
95 | users.rotate(-chooser_index)
|
---|
96 |
|
---|
97 | context.chat_data["users"] = list(users)
|
---|
98 |
|
---|
99 | await update.message.reply_text(context.chat_data["users"])
|
---|