-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·118 lines (88 loc) · 3.51 KB
/
main.py
File metadata and controls
executable file
·118 lines (88 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import asyncio
from nio.events import RedactionEvent
from src.globals_instance import get_bot, initialize_globals, get_config
from pathlib import Path
from dotenv import load_dotenv
from nio.events.room_events import ReactionEvent, RoomMessageText
from nio.rooms import MatrixRoom
import simplematrixbotlib as botlib
from src.command_system import load_commands
from src.commands.command import Command
from src.utils.logging_config import setup_logger
from src.utils.once_decorator import once
from src.poll_manager import PollManager
from src.command_manager import CommandManager
# Setup logger
logger = setup_logger(__name__)
load_dotenv()
initialize_globals()
config = get_config()
session_file_path = Path(config["session_file"])
# Remove session.txt file if required by configuration
if session_file_path.exists() and config.get("delete_session_file_on_start"):
session_file_path.unlink()
PREFIX = config["prefix"]
bot = get_bot()
@bot.listener.on_startup # type: ignore
@once
async def on_startup(w) -> None:
logger.info("Bot started successfully.")
@bot.listener.on_message_event # type: ignore
async def on_message(room: MatrixRoom, message: RoomMessageText) -> None:
match = botlib.MessageMatch(room, message, bot, PREFIX)
if match.is_not_from_this_bot():
await handle_message(match, config)
@bot.listener.on_reaction_event # type: ignore
async def on_reaction(room: MatrixRoom, event: ReactionEvent, reaction: str) -> None:
if event.sender == bot.async_client.user:
return
# payment reminder
p = poll_manager.get_last_closed_poll(room.room_id)
if p is None:
return
poll_summary_message_event_id = p.status_messages[-1]
if poll_summary_message_event_id is None:
return
if poll_summary_message_event_id == event.reacts_to and reaction == config.get(
"paying_feature", {}
).get("emoji", "💸"):
logger.info(f"Payment received from user {event.sender}")
await p.add_payment_for_user(event.sender, event.event_id)
@bot.listener.on_custom_event(RedactionEvent) # type: ignore
async def on_redaction(room: MatrixRoom, event: RedactionEvent) -> None:
# payment reminder
p = poll_manager.get_last_closed_poll(room.room_id)
if p is None:
return
for user in p.involved_users:
if user.pay_reaction_event_id == event.redacts:
# user removed payment reaction bash him for it
logger.info(f"Payment removed for user {user.username}")
await p.bash_user_for_not_paying(user.username)
return
async def handle_message(match: botlib.MessageMatch, config):
"""Process incoming messages and execute the corresponding command."""
command_match = command_manager.get_matching_command(match)
if command_match is None:
return
# if command_match is None:
# if not config["use_add_command"]:
# await poll_manager.add_from_match_item(match)
# return
# return
command, struct = command_match
await command.execute(struct)
def main():
try:
bot.run()
except KeyboardInterrupt:
logger.info("Keyboard interrupt detected, shutting down...")
asyncio.run(bot.async_client.close())
finally:
logger.info("Bot shut down successfully")
if __name__ == "__main__":
poll_manager = PollManager()
commands: list[Command] = load_commands(Path("src/commands"), config)
command_manager = CommandManager(commands, config.get("prefix", "!"))
logger.info("Starting bot...")
main()