-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathannouncer.py
88 lines (69 loc) · 2.61 KB
/
announcer.py
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
# -*- coding: utf-8 -*-
"""Discord announcement publisher"""
__copyright__ = "Copyright (c) 2024 STAIR. All Rights Reserved."
__email__ = "[email protected]"
import discord
from integration.discord.persona import PersonaSender
from integration.discord.util import base64_image_to_discord
from integration.iannouncer import IAnnouncer
from common.constants import STAIR_GREEN
from common.publish_data import PublishData
from common.result import Result
from db.db import Database
from .stan import Stan as DiscordStan
class Announcer(IAnnouncer):
"""Announcement publisher"""
def __init__(self, db: Database, stan: DiscordStan):
self._db = db
self._discord = stan
async def publish_announcement( # pylint: disable=too-many-locals
self, data: PublishData
) -> Result[None]:
"""Publish an announcement to Discord."""
announcement = self._db.get_announcement(data.announcement_id)
if not announcement:
return Result.err(error="Announcement not found", status=404)
discord_servers = self._discord.servers
if data.server not in discord_servers:
return Result.err(
"Server not found",
status=404,
)
server = discord_servers[data.server]
discord_channel = server.get_announcement_channel(data.announcement_type)
if not discord_channel:
return Result.err(
"Channel not found",
status=404,
)
role = server.get_announcement_role(data.announcement_type)
embeds: list[discord.Embed] = []
if announcement.message_de:
embeds.append(
discord.Embed(
title=f":flag_de: {announcement.title}",
description=announcement.message_de,
color=STAIR_GREEN,
)
)
if announcement.message_en:
embeds.append(
discord.Embed(
title=f":flag_gb: {announcement.title}",
description=announcement.message_en,
color=STAIR_GREEN,
)
)
for embed in embeds:
if data.user:
embed.set_footer(text=f"by {data.user}")
else:
embed.set_footer(text="automated announcement")
file = base64_image_to_discord(data.image)
await PersonaSender(discord_channel, data.persona).send(
message=role.mention,
embeds=embeds,
file=file,
publish=True,
)
return Result.ok(value=None)