source: python-cinema-club-bot/commands/event.py@ 8d2d5ac

Last change on this file since 8d2d5ac was 8d2d5ac, checked in by Mikhail Kirillov <w96k@…>, on Oct 10, 2024 at 4:26:56 AM

fixup! Fix #9. Add events commands

  • Property mode set to 100644
File size: 3.2 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(EVENT_CREATED.format(when=event_when))
79 else:
80 events[-1] = event_dict
81 await update.message.reply_text(EVENT_EDITED.format(when=event_when))
82
83
84async def list_events(
85 update: Update,
86 context: ContextTypes.DEFAULT_TYPE
87) -> None:
88 context_init(context)
89
90 events = context.chat_data["events"]
91
92 events_formatted: str = ""
93
94 for event in events:
95 event_finished = "FINISHED" \
96 if is_past(event["when"]) else "PLANNED"
97
98 events_formatted += EVENTS_LIST.format(
99 movie=event["movie"],
100 user=event["user"],
101 when=event["when"],
102 where=event["where"],
103 finished=event_finished
104 )
105
106 if events_formatted == "":
107 events_formatted = NO_EVENTS
108
109 await update.message.reply_text(events_formatted)
Note: See TracBrowser for help on using the repository browser.