[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 |
|
---|
| 12 | from telegram import Update, error
|
---|
| 13 | from telegram.ext import ContextTypes
|
---|
| 14 | from collections import deque
|
---|
| 15 |
|
---|
[0d48ea2] | 16 | from 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
|
---|
| 20 | from utils import context_init, create_users_string, choose_next_user
|
---|
[9be02d9] | 21 | from predicates import has_finished_event
|
---|
[cc34991] | 22 |
|
---|
[23bddf3] | 23 |
|
---|
[0d48ea2] | 24 | async 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] | 38 | async 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)
|
---|
[0d48ea2] | 49 | await update.message.reply_text(USER_ADD.format(user=user))
|
---|
[cc34991] | 50 |
|
---|
[0d48ea2] | 51 | await update.message.reply_text(USERS_ADDED)
|
---|
[cc34991] | 52 |
|
---|
| 53 |
|
---|
| 54 | async 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)
|
---|
| 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))
|
---|
[6623428] | 69 |
|
---|
| 70 |
|
---|
| 71 | async def who_is_next(
|
---|
| 72 | update: Update,
|
---|
| 73 | context: ContextTypes.DEFAULT_TYPE
|
---|
| 74 | ) -> None:
|
---|
[9be02d9] | 75 | """
|
---|
| 76 | This commands sets the next chooser if needed and shows current
|
---|
| 77 | """
|
---|
| 78 |
|
---|
[6623428] | 79 | context_init(context)
|
---|
| 80 |
|
---|
| 81 | users = context.chat_data["users"]
|
---|
| 82 |
|
---|
[9be02d9] | 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 |
|
---|
[23bddf3] | 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)
|
---|
[cc34991] | 98 |
|
---|
| 99 |
|
---|
| 100 | async 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)
|
---|
[0d48ea2] | 115 | await update.message.reply_text(USER_REMOVE.format(user=user))
|
---|
[cc34991] | 116 |
|
---|
[0d48ea2] | 117 | await update.message.reply_text(USERS_REMOVED)
|
---|
[cc34991] | 118 |
|
---|
| 119 |
|
---|
| 120 | async 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 |
|
---|
[6623428] | 135 | try:
|
---|
| 136 | chooser_index = users.index(chooser)
|
---|
| 137 | except ValueError:
|
---|
| 138 | raise error.TelegramError(USER_NOT_FOUND.format(user=chooser))
|
---|
| 139 |
|
---|
[cc34991] | 140 | users.rotate(-chooser_index)
|
---|
[6623428] | 141 | users = list(users)
|
---|
| 142 |
|
---|
| 143 | context.chat_data["users"] = users
|
---|
[cc34991] | 144 |
|
---|
[9be02d9] | 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 |
|
---|
[6623428] | 151 | await update.message.reply_text(USER_CHOOSE.format(user=users[0]))
|
---|
[cc34991] | 152 |
|
---|
[6623428] | 153 | await update.message.reply_markdown(create_users_string(users))
|
---|