source: python-cinema-club-bot/commands/event.py@ 99cecda

Last change on this file since 99cecda was 23bddf3, checked in by Mikhail Kirillov <w96k@…>, on Oct 10, 2024 at 6:20:52 AM

Task #10. /announce command

  • Property mode set to 100644
File size: 3.6 KB
Line 
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 datetime import datetime
15
16from utils import context_init
17from predicates import is_past, has_finished_event
18from strings import EVENT_ARGS_NOT_PROVIDED, EVENT_INVALID_DATETIME, \
19 EVENT_CREATED, EVENT_EDITED, EVENT_CANT_BE_IN_PAST, \
20 EVENT_WHERE_NOT_PROVIDED, EVENT_MOVIE_NOT_CHOOSEN, EVENTS_LIST, \
21 EVENT_USER_HAD_EVENT, NO_EVENTS
22
23
24async def create_event(
25 update: Update,
26 context: ContextTypes.DEFAULT_TYPE
27) -> None:
28 context_init(context)
29 events = context.chat_data["events"]
30 movies = context.chat_data["movies"]
31
32 username = update.message.from_user.username
33 last_movie = movies[-1] if movies != [] else None
34
35 if has_finished_event(context, username):
36 raise error.TelegramError(EVENT_USER_HAD_EVENT)
37
38 if last_movie is None or last_movie["user"] != username:
39 raise error.TelegramError(EVENT_MOVIE_NOT_CHOOSEN)
40
41 if context.args == []:
42 raise error.TelegramError(EVENT_ARGS_NOT_PROVIDED)
43
44 argument_when = context.args[0]
45
46 try:
47 event_when = datetime.strptime(argument_when, "%d.%m.%Y/%H:%M")
48 except ValueError:
49 raise error.TelegramError(EVENT_INVALID_DATETIME)
50
51 if is_past(event_when):
52 raise error.TelegramError(
53 EVENT_CANT_BE_IN_PAST.format(
54 when=event_when,
55 today=datetime.today(),
56 )
57 )
58
59 arguments_where = context.args[1:]
60
61 if arguments_where == [] and events == []:
62 raise error.TelegramError(EVENT_WHERE_NOT_PROVIDED)
63
64 event_where = " ".join(arguments_where)
65
66 if event_where == "":
67 event_where = events[-1]["where"]
68
69 event_dict = dict(
70 when=event_when,
71 where=event_where,
72 movie=last_movie["title"],
73 user=last_movie["user"]
74 )
75
76 if events == [] or is_past(events[-1]["when"]):
77 events.append(event_dict)
78 await update.message.reply_text(
79 EVENT_CREATED.format(
80 when=event_when,
81 movie=event_dict["movie"],
82 )
83 )
84 else:
85 old_when = events[-1]["when"]
86 old_where = events[-1]["where"]
87 events[-1] = event_dict
88
89 await update.message.reply_text(
90 EVENT_EDITED.format(
91 old_when=old_when,
92 old_where=old_where,
93 when=event_when,
94 where=event_where,
95 movie=event_dict["movie"],
96 )
97 )
98
99
100async def list_events(
101 update: Update,
102 context: ContextTypes.DEFAULT_TYPE
103) -> None:
104 context_init(context)
105
106 events = context.chat_data["events"]
107
108 events_formatted: str = ""
109
110 for event in events:
111 event_finished = "FINISHED" \
112 if is_past(event["when"]) else "PLANNED"
113
114 events_formatted += EVENTS_LIST.format(
115 movie=event["movie"],
116 user=event["user"],
117 when=event["when"],
118 where=event["where"],
119 finished=event_finished
120 )
121
122 if events_formatted == "":
123 events_formatted = NO_EVENTS
124
125 await update.message.reply_text(events_formatted)
Note: See TracBrowser for help on using the repository browser.