-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage_worker.py
More file actions
83 lines (73 loc) · 3.66 KB
/
message_worker.py
File metadata and controls
83 lines (73 loc) · 3.66 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
import discord
from logger_config import get_logger
import message_send
import message_reply
import message_forward
import helpers
logger = get_logger(__name__)
class MessageWorker:
def __init__(self, bot, forum_sync=None):
self.bot = bot
self.forum_sync = forum_sync
def _should_ignore_message(self, message: discord.Message) -> bool:
"""
Check if the message should be ignored based on various criteria.
"""
# Ignore messages from the bot itself
if message.author == self.bot.user:
return True
# Ignore messages from webhooks
if message.webhook_id:
return True
# Ignore empty thread creation messages
if isinstance(message.channel, discord.Thread) and not message.content:
return True
return False
async def process_message(self, message: discord.Message):
"""
Main entry point for processing messages. Routes messages to appropriate handlers
based on their type (regular, reply, thread, reply in thread).
"""
if self._should_ignore_message(message):
return
# Ignore messages in channels that are not part of any linked group.
if isinstance(message.channel, discord.Thread):
group_name = helpers.get_group_name(str(message.channel.parent_id))
else:
group_name = helpers.get_group_name(str(message.channel.id))
if not group_name:
logger.debug("Ignoring message outside of any group: %s in %s", message.author, message.channel)
return
try:
if isinstance(message.channel, discord.Thread):
if self.forum_sync and self.forum_sync.is_forum_thread(message.channel):
logger.info(f"Processing forum thread message from {message.author} in {message.channel.name}")
if message.reference:
await message_reply.handle_forum_thread_reply_message(self.bot, message)
else:
await message_send.handle_forum_thread_message(self.bot, message)
return
if message.reference:
# Reply in thread
logger.info(f"Processing reply in thread from {message.author} in {message.channel.name}")
await message_reply.handle_reply_message_in_thread(self.bot, message)
else:
# Regular thread message
logger.info(f"Processing thread message from {message.author} in {message.channel.name}")
await message_send.handle_thread_message(self.bot, message)
else:
if message.reference:
if message.reference.type == discord.MessageReferenceType.forward:
# Forward message
logger.info(f"Processing forward message from {message.author} in {message.channel.name}")
await message_forward.handle_forward_message(self.bot, message)
else:
# Reply in regular channel
logger.info(f"Processing reply message from {message.author} in {message.channel.name}")
await message_reply.handle_reply_message_in_channel(self.bot, message)
else:
# Regular message
logger.info(f"Processing regular message from {message.author} in {message.channel.name}")
await message_send.handle_message(self.bot, message)
except Exception as e:
logger.error(f"Error processing message: {e}", exc_info=True)