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

Last change on this file since 0d48ea2 was 0d48ea2, checked in by Mikhail Kirillov <w96k@…>, on Oct 9, 2024 at 3:40:27 AM

Add /set command; Add error_handler;

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