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

Last change on this file since 5ba045c was 23bddf3, checked in by Mikhail Kirillov <w96k@…>, on Oct 10, 2024 at 6:20:52 AM

Task #10. /announce command

  • Property mode set to 100644
File size: 4.3 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, NEXT_MOVIE
20from utils import context_init, create_users_string, choose_next_user
21from predicates import has_finished_event
22
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 next_user_string = NEXT_MOVIE_USER.format(user=users[0])
90
91 last_movie = context.chat_data["movies"][0] \
92 if context.chat_data["movies"] != [] else None
93
94 if last_movie and last_movie["user"] == users[0]:
95 next_user_string += NEXT_MOVIE.format(movie=last_movie["title"])
96
97 await update.message.reply_text(next_user_string)
98
99
100async def remove_users(
101 update: Update,
102 context: ContextTypes.DEFAULT_TYPE
103) -> None:
104 context_init(context)
105
106 if context.args == []:
107 raise error.TelegramError(USER_NOT_PROVIDED)
108
109 for user in context.args:
110 if user == "*":
111 context.chat_data["users"] = []
112 break
113
114 context.chat_data["users"].remove(user)
115 await update.message.reply_text(USER_REMOVE.format(user=user))
116
117 await update.message.reply_text(USERS_REMOVED)
118
119
120async def chooser_user(
121 update: Update,
122 context: ContextTypes.DEFAULT_TYPE
123) -> None:
124 context_init(context)
125
126 if context.args == []:
127 raise error.TelegramError(USER_NOT_PROVIDED)
128
129 if len(context.args) > 1:
130 raise error.TelegramError(EXPECTED_ONE_USER)
131
132 chooser = context.args[0]
133 users = deque(context.chat_data["users"])
134
135 try:
136 chooser_index = users.index(chooser)
137 except ValueError:
138 raise error.TelegramError(USER_NOT_FOUND.format(user=chooser))
139
140 users.rotate(-chooser_index)
141 users = list(users)
142
143 context.chat_data["users"] = users
144
145 if has_finished_event(context, users[0]):
146 await update.message.reply_text(
147 EVENT_USER_HAD_EVENT.format(user=users[0])
148 )
149 users = context.chat_data["users"] = choose_next_user(users)
150
151 await update.message.reply_text(USER_CHOOSE.format(user=users[0]))
152
153 await update.message.reply_markdown(create_users_string(users))
Note: See TracBrowser for help on using the repository browser.