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

Last change on this file since 6623428 was 6623428, checked in by Mikhail Kirillov <w96k@…>, on Oct 9, 2024 at 10:46:19 PM

Add movies commands

  • Property mode set to 100644
File size: 3.3 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, \
18 NEXT_MOVIE_USER, USER_NOT_FOUND, USER_CHOOSE
19from utils import context_init, create_users_string
[cc34991]20
21
[0d48ea2]22async def set_users(
23 update: Update,
24 context: ContextTypes.DEFAULT_TYPE
25) -> None:
26 context_init(context)
27
28 if context.args == []:
29 raise error.TelegramError(USER_NOT_PROVIDED)
30
31 context.chat_data["users"] = context.args
32
33 await update.message.reply_text(USER_SET)
[cc34991]34
35async def add_users(
36 update: Update,
37 context: ContextTypes.DEFAULT_TYPE
38) -> None:
39 context_init(context)
40
41 if context.args == []:
42 raise error.TelegramError(USER_NOT_PROVIDED)
43
44 for user in context.args:
45 context.chat_data["users"].append(user)
[0d48ea2]46 await update.message.reply_text(USER_ADD.format(user=user))
[cc34991]47
[0d48ea2]48 await update.message.reply_text(USERS_ADDED)
[cc34991]49
50
51async def list_users(
52 update: Update,
53 context: ContextTypes.DEFAULT_TYPE
54) -> None:
55 context_init(context)
56
[6623428]57 users = context.chat_data["users"]
58
59 await update.message.reply_markdown(create_users_string(users))
60
61
62async def who_is_next(
63 update: Update,
64 context: ContextTypes.DEFAULT_TYPE
65) -> None:
66 context_init(context)
67
68 users = context.chat_data["users"]
69
70 if len(users) > 0:
71 await update.message.reply_text(NEXT_MOVIE_USER.format(user=users[0]))
72 else:
73 await update.message.reply_text(ADD_MORE_USERS)
[cc34991]74
75
76async def remove_users(
77 update: Update,
78 context: ContextTypes.DEFAULT_TYPE
79) -> None:
80 context_init(context)
81
82 if context.args == []:
83 raise error.TelegramError(USER_NOT_PROVIDED)
84
85 for user in context.args:
86 if user == "*":
87 context.chat_data["users"] = []
88 break
89
90 context.chat_data["users"].remove(user)
[0d48ea2]91 await update.message.reply_text(USER_REMOVE.format(user=user))
[cc34991]92
[0d48ea2]93 await update.message.reply_text(USERS_REMOVED)
[cc34991]94
95
96async def chooser_user(
97 update: Update,
98 context: ContextTypes.DEFAULT_TYPE
99) -> None:
100 context_init(context)
101
102 if context.args == []:
103 raise error.TelegramError(USER_NOT_PROVIDED)
104
105 if len(context.args) > 1:
106 raise error.TelegramError(EXPECTED_ONE_USER)
107
108 chooser = context.args[0]
109 users = deque(context.chat_data["users"])
110
[6623428]111 try:
112 chooser_index = users.index(chooser)
113 except ValueError:
114 raise error.TelegramError(USER_NOT_FOUND.format(user=chooser))
115
[cc34991]116 users.rotate(-chooser_index)
[6623428]117 users = list(users)
118
119 context.chat_data["users"] = users
[cc34991]120
[6623428]121 await update.message.reply_text(USER_CHOOSE.format(user=users[0]))
[cc34991]122
[6623428]123 await update.message.reply_markdown(create_users_string(users))
Note: See TracBrowser for help on using the repository browser.