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

Last change on this file since 69dd60c 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
RevLine 
[cc34991]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
[0d48ea2]16from strings import USER_NOT_PROVIDED, USERS_ADDED, USERS_REMOVED, \
[694d823]17 EXPECTED_ONE_USER, USER_SET, USER_REMOVE, ADD_MORE_USERS, \
[9be02d9]18 NEXT_MOVIE_USER, USER_NOT_FOUND, USER_CHOOSE, NO_USERS, \
[69dd60c]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
[cc34991]23
[23bddf3]24
[0d48ea2]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)
[cc34991]37
[9be02d9]38
[cc34991]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
[0d48ea2]51 await update.message.reply_text(USERS_ADDED)
[cc34991]52
53
54async def list_users(
55 update: Update,
56 context: ContextTypes.DEFAULT_TYPE
57) -> None:
58 context_init(context)
59
[6623428]60 users = context.chat_data["users"]
61
[9be02d9]62 if users == []:
63 await update.message.reply_text(NO_USERS)
[69dd60c]64 return
[9be02d9]65
[69dd60c]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))
[6623428]70
71
72async def who_is_next(
73 update: Update,
74 context: ContextTypes.DEFAULT_TYPE
75) -> None:
[9be02d9]76 """
77 This commands sets the next chooser if needed and shows current
78 """
79
[6623428]80 context_init(context)
81
82 users = context.chat_data["users"]
83
[9be02d9]84 if users == []:
85 raise error.TelegramError(ADD_MORE_USERS)
86
[69dd60c]87 if has_finished_movie(context):
88 users = context.chat_data["users"] = choose_next_user(context)
[9be02d9]89
[23bddf3]90 next_user_string = NEXT_MOVIE_USER.format(user=users[0])
91
[69dd60c]92 last_movie = context.chat_data["movies"][-1] \
[23bddf3]93 if context.chat_data["movies"] != [] else None
94
[69dd60c]95 if last_movie and last_movie["user"] == normalize_username(users[0]):
[23bddf3]96 next_user_string += NEXT_MOVIE.format(movie=last_movie["title"])
97
98 await update.message.reply_text(next_user_string)
[cc34991]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)
[0d48ea2]116 await update.message.reply_text(USER_REMOVE.format(user=user))
[cc34991]117
[0d48ea2]118 await update.message.reply_text(USERS_REMOVED)
[cc34991]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
[6623428]136 try:
137 chooser_index = users.index(chooser)
138 except ValueError:
139 raise error.TelegramError(USER_NOT_FOUND.format(user=chooser))
140
[cc34991]141 users.rotate(-chooser_index)
[6623428]142 users = list(users)
143
144 context.chat_data["users"] = users
[cc34991]145
[69dd60c]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]
[9be02d9]151
[6623428]152 await update.message.reply_text(USER_CHOOSE.format(user=users[0]))
[cc34991]153
[6623428]154 await update.message.reply_markdown(create_users_string(users))
Note: See TracBrowser for help on using the repository browser.