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

Last change on this file since 9be02d9 was 9be02d9, checked in by Mikhail Kirillov <w96k@…>, on Oct 10, 2024 at 4:12:00 AM

Fix #9. Add events commands

  • Property mode set to 100644
File size: 4.0 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, USERS_ADDED, USERS_REMOVED, \
17 EXPECTED_ONE_USER, USER_SET, USER_ADD, USER_REMOVE, ADD_MORE_USERS, \
18 NEXT_MOVIE_USER, USER_NOT_FOUND, USER_CHOOSE, NO_USERS, \
19 EVENT_USER_HAD_EVENT
20from utils import context_init, create_users_string, normalize_username, \
21 choose_next_user
22from predicates import has_finished_event
23
24async def set_users(
25 update: Update,
26 context: ContextTypes.DEFAULT_TYPE
27) -> None:
28 context_init(context)
29
30 if context.args == []:
31 raise error.TelegramError(USER_NOT_PROVIDED)
32
33 context.chat_data["users"] = context.args
34
35 await update.message.reply_text(USER_SET)
36
37
38async def add_users(
39 update: Update,
40 context: ContextTypes.DEFAULT_TYPE
41) -> None:
42 context_init(context)
43
44 if context.args == []:
45 raise error.TelegramError(USER_NOT_PROVIDED)
46
47 for user in context.args:
48 context.chat_data["users"].append(user)
49 await update.message.reply_text(USER_ADD.format(user=user))
50
51 await update.message.reply_text(USERS_ADDED)
52
53
54async def list_users(
55 update: Update,
56 context: ContextTypes.DEFAULT_TYPE
57) -> None:
58 context_init(context)
59
60 users = context.chat_data["users"]
61
62 if users == []:
63 await update.message.reply_text(NO_USERS)
64 else:
65 if has_finished_event(context, users[0]):
66 users = context.chat_data["users"] = choose_next_user(users)
67
68 await update.message.reply_markdown(create_users_string(users))
69
70
71async def who_is_next(
72 update: Update,
73 context: ContextTypes.DEFAULT_TYPE
74) -> None:
75 """
76 This commands sets the next chooser if needed and shows current
77 """
78
79 context_init(context)
80
81 users = context.chat_data["users"]
82
83 if users == []:
84 raise error.TelegramError(ADD_MORE_USERS)
85
86 if has_finished_event(context, users[0]):
87 users = context.chat_data["users"] = choose_next_user(users)
88
89 await update.message.reply_text(NEXT_MOVIE_USER.format(user=users[0]))
90
91
92async def remove_users(
93 update: Update,
94 context: ContextTypes.DEFAULT_TYPE
95) -> None:
96 context_init(context)
97
98 if context.args == []:
99 raise error.TelegramError(USER_NOT_PROVIDED)
100
101 for user in context.args:
102 if user == "*":
103 context.chat_data["users"] = []
104 break
105
106 context.chat_data["users"].remove(user)
107 await update.message.reply_text(USER_REMOVE.format(user=user))
108
109 await update.message.reply_text(USERS_REMOVED)
110
111
112async def chooser_user(
113 update: Update,
114 context: ContextTypes.DEFAULT_TYPE
115) -> None:
116 context_init(context)
117
118 if context.args == []:
119 raise error.TelegramError(USER_NOT_PROVIDED)
120
121 if len(context.args) > 1:
122 raise error.TelegramError(EXPECTED_ONE_USER)
123
124 chooser = context.args[0]
125 users = deque(context.chat_data["users"])
126
127 try:
128 chooser_index = users.index(chooser)
129 except ValueError:
130 raise error.TelegramError(USER_NOT_FOUND.format(user=chooser))
131
132 users.rotate(-chooser_index)
133 users = list(users)
134
135 context.chat_data["users"] = users
136
137 if has_finished_event(context, users[0]):
138 await update.message.reply_text(
139 EVENT_USER_HAD_EVENT.format(user=users[0])
140 )
141 users = context.chat_data["users"] = choose_next_user(users)
142
143 await update.message.reply_text(USER_CHOOSE.format(user=users[0]))
144
145 await update.message.reply_markdown(create_users_string(users))
Note: See TracBrowser for help on using the repository browser.