|
| 1 | +import asyncio |
| 2 | +import os |
| 3 | +from copy import deepcopy |
| 4 | +from threading import Lock |
| 5 | + |
| 6 | +import discord |
| 7 | + |
| 8 | +from src.utils.data_types import ( |
| 9 | + ProjectItemEditedAssignees, |
| 10 | + ProjectItemEditedBody, |
| 11 | + ProjectItemEditedSingleSelect, |
| 12 | + ProjectItemEditedTitle, |
| 13 | + ProjectItemEvent, |
| 14 | + SimpleProjectItemEvent, |
| 15 | +) |
| 16 | +from src.utils.error import ForumChannelNotFound |
| 17 | +from src.utils.utils import add_tag_to_thread, get_post_id, retrieve_discord_id |
| 18 | + |
| 19 | + |
| 20 | +class DiscordClient(discord.Client): |
| 21 | + bg_task: asyncio.Task |
| 22 | + |
| 23 | + def __init__(self, *, state, lock, **kwargs): |
| 24 | + super().__init__(**kwargs) |
| 25 | + self.state: dict[str, bool | list[ProjectItemEvent]] = state |
| 26 | + self.lock: Lock = lock |
| 27 | + |
| 28 | + async def on_ready(self): |
| 29 | + print(f"Logged on as {self.user}!") |
| 30 | + self.bg_task = self.loop.create_task(self.process_updates()) |
| 31 | + |
| 32 | + async def process_updates(self): |
| 33 | + forum_channel_id = int(os.getenv("FORUM_CHANNEL_ID")) |
| 34 | + forum_channel: discord.ForumChannel = self.get_channel(forum_channel_id) |
| 35 | + if forum_channel is None: |
| 36 | + raise ForumChannelNotFound(f"Forum channel with ID {forum_channel_id} not found.") |
| 37 | + local_queue_copy: list[ProjectItemEvent] = [] |
| 38 | + |
| 39 | + while True: |
| 40 | + with self.lock: |
| 41 | + if self.state["update-received"]: |
| 42 | + local_queue_copy = deepcopy(self.state["update-queue"]) |
| 43 | + self.state["update-queue"].clear() |
| 44 | + self.state["update-received"] = False |
| 45 | + |
| 46 | + for event in local_queue_copy: |
| 47 | + post_id = await get_post_id(event.name, forum_channel) |
| 48 | + author_discord_id = retrieve_discord_id(event.sender) |
| 49 | + if post_id is None: |
| 50 | + message = f"Nowy task stworzony {event.name} przez <@{author_discord_id}>" |
| 51 | + await forum_channel.create_thread(name=event.name, content=message, auto_archive_duration=10080) |
| 52 | + post_id = await get_post_id(event.name, forum_channel) |
| 53 | + thread: discord.Thread = forum_channel.get_thread(int(post_id)) or await self.fetch_channel( |
| 54 | + int(post_id) |
| 55 | + ) |
| 56 | + if thread is None: |
| 57 | + continue |
| 58 | + |
| 59 | + if isinstance(event, SimpleProjectItemEvent): |
| 60 | + match event.event_type.value: |
| 61 | + case "archived": |
| 62 | + message = f"Task zarchiwizowany przez <@{author_discord_id}>." |
| 63 | + await thread.send(message) |
| 64 | + await thread.edit(archived=True) |
| 65 | + case "restored": |
| 66 | + message = f"Task przywrócony przez <@{author_discord_id}>." |
| 67 | + await thread.send(message) |
| 68 | + await thread.edit(archived=False) |
| 69 | + case "deleted": |
| 70 | + await thread.delete() |
| 71 | + elif isinstance(event, ProjectItemEditedAssignees): |
| 72 | + assignee_mentions: list[str] = [] |
| 73 | + if event.new_assignees: |
| 74 | + for assignee in event.new_assignees: |
| 75 | + discord_id = retrieve_discord_id(assignee) |
| 76 | + if discord_id: |
| 77 | + assignee_mentions.append(f"<@{discord_id}>") |
| 78 | + else: |
| 79 | + assignee_mentions.append("Brak przypisanych osób") |
| 80 | + |
| 81 | + message = ( |
| 82 | + f"Osoby przypisane do taska edytowane, aktualni przypisani: {', '.join(assignee_mentions)}" |
| 83 | + ) |
| 84 | + await thread.send(message) |
| 85 | + elif isinstance(event, ProjectItemEditedBody): |
| 86 | + message = f"Opis taska zaktualizowany przez <@{author_discord_id}>. Nowy opis: {event.new_body}" |
| 87 | + await thread.send(message) |
| 88 | + elif isinstance(event, ProjectItemEditedTitle): |
| 89 | + await thread.edit(name=event.new_title) |
| 90 | + elif isinstance(event, ProjectItemEditedSingleSelect): |
| 91 | + thread_tags = list(thread.applied_tags) |
| 92 | + for tag in thread_tags: |
| 93 | + if tag.name.startswith(f"{event.value_type.value}: "): |
| 94 | + await thread.remove_tags(tag) |
| 95 | + |
| 96 | + await add_tag_to_thread( |
| 97 | + thread, forum_channel, f"{event.value_type.value}: {event.new_value}", event.value_type.value |
| 98 | + ) |
| 99 | + |
| 100 | + local_queue_copy.clear() |
| 101 | + |
| 102 | + await asyncio.sleep(1) |
| 103 | + |
| 104 | + |
| 105 | +def run(state, lock): |
| 106 | + intents = discord.Intents.default() |
| 107 | + |
| 108 | + client = DiscordClient(intents=intents, state=state, lock=lock) |
| 109 | + client.run(os.getenv("DISCORD_BOT_TOKEN")) |
0 commit comments