[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, \
|
---|
| 19 | EVENT_USER_HAD_EVENT
|
---|
| 20 | from utils import context_init, create_users_string, normalize_username, \
|
---|
| 21 | choose_next_user
|
---|
| 22 | from predicates import has_finished_event
|
---|
[cc34991] | 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 |
|
---|
| 89 | await update.message.reply_text(NEXT_MOVIE_USER.format(user=users[0]))
|
---|
[cc34991] | 90 |
|
---|
| 91 |
|
---|
| 92 | async def remove_users(
|
---|
| 93 | update: Update,
|
---|
| 94 | context: ContextTypes.DEFAULT_TYPE
|
---|
| 95 | ) -> None:
|
---|
| 96 | context_init(context)
|
---|
| 97 |
|
---|
| 98 | if context.args == []:
|
---|
| 99 | raise error.TelegramError(USER_NOT_PROVIDED)
|
---|
| 100 |
|
---|
| 101 | for user in context.args:
|
---|
| 102 | if user == "*":
|
---|
| 103 | context.chat_data["users"] = []
|
---|
| 104 | break
|
---|
| 105 |
|
---|
| 106 | context.chat_data["users"].remove(user)
|
---|
[0d48ea2] | 107 | await update.message.reply_text(USER_REMOVE.format(user=user))
|
---|
[cc34991] | 108 |
|
---|
[0d48ea2] | 109 | await update.message.reply_text(USERS_REMOVED)
|
---|
[cc34991] | 110 |
|
---|
| 111 |
|
---|
| 112 | async def chooser_user(
|
---|
| 113 | update: Update,
|
---|
| 114 | context: ContextTypes.DEFAULT_TYPE
|
---|
| 115 | ) -> None:
|
---|
| 116 | context_init(context)
|
---|
| 117 |
|
---|
| 118 | if context.args == []:
|
---|
| 119 | raise error.TelegramError(USER_NOT_PROVIDED)
|
---|
| 120 |
|
---|
| 121 | if len(context.args) > 1:
|
---|
| 122 | raise error.TelegramError(EXPECTED_ONE_USER)
|
---|
| 123 |
|
---|
| 124 | chooser = context.args[0]
|
---|
| 125 | users = deque(context.chat_data["users"])
|
---|
| 126 |
|
---|
[6623428] | 127 | try:
|
---|
| 128 | chooser_index = users.index(chooser)
|
---|
| 129 | except ValueError:
|
---|
| 130 | raise error.TelegramError(USER_NOT_FOUND.format(user=chooser))
|
---|
| 131 |
|
---|
[cc34991] | 132 | users.rotate(-chooser_index)
|
---|
[6623428] | 133 | users = list(users)
|
---|
| 134 |
|
---|
| 135 | context.chat_data["users"] = users
|
---|
[cc34991] | 136 |
|
---|
[9be02d9] | 137 | if has_finished_event(context, users[0]):
|
---|
| 138 | await update.message.reply_text(
|
---|
| 139 | EVENT_USER_HAD_EVENT.format(user=users[0])
|
---|
| 140 | )
|
---|
| 141 | users = context.chat_data["users"] = choose_next_user(users)
|
---|
| 142 |
|
---|
[6623428] | 143 | await update.message.reply_text(USER_CHOOSE.format(user=users[0]))
|
---|
[cc34991] | 144 |
|
---|
[6623428] | 145 | await update.message.reply_markdown(create_users_string(users))
|
---|