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

Last change on this file since 0239940 was 0239940, checked in by Mikhail Kirillov <w96k@…>, on Oct 10, 2024 at 7:50:41 AM

Remove messages when adding more people

  • 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, \
[6623428]17 EXPECTED_ONE_USER, USER_SET, USER_ADD, USER_REMOVE, ADD_MORE_USERS, \
[9be02d9]18 NEXT_MOVIE_USER, USER_NOT_FOUND, USER_CHOOSE, NO_USERS, \
[23bddf3]19 EVENT_USER_HAD_EVENT, NEXT_MOVIE
20from utils import context_init, create_users_string, choose_next_user
[9be02d9]21from predicates import has_finished_event
[cc34991]22
[23bddf3]23
[0d48ea2]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)
[cc34991]36
[9be02d9]37
[cc34991]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
[0d48ea2]50 await update.message.reply_text(USERS_ADDED)
[cc34991]51
52
53async def list_users(
54 update: Update,
55 context: ContextTypes.DEFAULT_TYPE
56) -> None:
57 context_init(context)
58
[6623428]59 users = context.chat_data["users"]
60
[9be02d9]61 if users == []:
62 await update.message.reply_text(NO_USERS)
63 else:
64 if has_finished_event(context, users[0]):
65 users = context.chat_data["users"] = choose_next_user(users)
66
67 await update.message.reply_markdown(create_users_string(users))
[6623428]68
69
70async def who_is_next(
71 update: Update,
72 context: ContextTypes.DEFAULT_TYPE
73) -> None:
[9be02d9]74 """
75 This commands sets the next chooser if needed and shows current
76 """
77
[6623428]78 context_init(context)
79
80 users = context.chat_data["users"]
81
[9be02d9]82 if users == []:
83 raise error.TelegramError(ADD_MORE_USERS)
84
85 if has_finished_event(context, users[0]):
86 users = context.chat_data["users"] = choose_next_user(users)
87
[23bddf3]88 next_user_string = NEXT_MOVIE_USER.format(user=users[0])
89
90 last_movie = context.chat_data["movies"][0] \
91 if context.chat_data["movies"] != [] else None
92
93 if last_movie and last_movie["user"] == users[0]:
94 next_user_string += NEXT_MOVIE.format(movie=last_movie["title"])
95
96 await update.message.reply_text(next_user_string)
[cc34991]97
98
99async def remove_users(
100 update: Update,
101 context: ContextTypes.DEFAULT_TYPE
102) -> None:
103 context_init(context)
104
105 if context.args == []:
106 raise error.TelegramError(USER_NOT_PROVIDED)
107
108 for user in context.args:
109 if user == "*":
110 context.chat_data["users"] = []
111 break
112
113 context.chat_data["users"].remove(user)
[0d48ea2]114 await update.message.reply_text(USER_REMOVE.format(user=user))
[cc34991]115
[0d48ea2]116 await update.message.reply_text(USERS_REMOVED)
[cc34991]117
118
119async def chooser_user(
120 update: Update,
121 context: ContextTypes.DEFAULT_TYPE
122) -> None:
123 context_init(context)
124
125 if context.args == []:
126 raise error.TelegramError(USER_NOT_PROVIDED)
127
128 if len(context.args) > 1:
129 raise error.TelegramError(EXPECTED_ONE_USER)
130
131 chooser = context.args[0]
132 users = deque(context.chat_data["users"])
133
[6623428]134 try:
135 chooser_index = users.index(chooser)
136 except ValueError:
137 raise error.TelegramError(USER_NOT_FOUND.format(user=chooser))
138
[cc34991]139 users.rotate(-chooser_index)
[6623428]140 users = list(users)
141
142 context.chat_data["users"] = users
[cc34991]143
[9be02d9]144 if has_finished_event(context, users[0]):
145 await update.message.reply_text(
146 EVENT_USER_HAD_EVENT.format(user=users[0])
147 )
148 users = context.chat_data["users"] = choose_next_user(users)
149
[6623428]150 await update.message.reply_text(USER_CHOOSE.format(user=users[0]))
[cc34991]151
[6623428]152 await update.message.reply_markdown(create_users_string(users))
Note: See TracBrowser for help on using the repository browser.