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

Last change on this file was 69dd60c, checked in by Mikhail Kirillov <w96k@…>, on Oct 11, 2024 at 1:53:24 AM

Task #30. Allow anyone create or edit upcoming event

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