-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathstartup.py
More file actions
152 lines (127 loc) · 5.65 KB
/
startup.py
File metadata and controls
152 lines (127 loc) · 5.65 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
"""Contains cog classes for any startup interactions."""
import logging
from typing import TYPE_CHECKING
import discord
from discord_logging.handler import DiscordHandler
import utils
from config import settings
from exceptions import (
ArchivistRoleDoesNotExistError,
CommitteeRoleDoesNotExistError,
GeneralChannelDoesNotExistError,
GuestRoleDoesNotExistError,
GuildDoesNotExistError,
MemberRoleDoesNotExistError,
MSLMembershipError,
RolesChannelDoesNotExistError,
)
from utils import TeXBotBaseCog
from utils.msl import fetch_community_group_members_list
if TYPE_CHECKING:
from collections.abc import Sequence
from logging import Logger
from typing import Final
__all__: "Sequence[str]" = ("StartupCog",)
logger: "Final[Logger]" = logging.getLogger("TeX-Bot")
class StartupCog(TeXBotBaseCog):
"""Cog class that defines additional code to execute upon startup."""
@TeXBotBaseCog.listener()
async def on_ready(self) -> None:
"""
Populate the shortcut accessors of TeX-Bot after initialisation.
Shortcut accessors should only be populated once TeX-Bot is ready to make API requests.
"""
if settings["DISCORD_LOG_CHANNEL_WEBHOOK_URL"]:
discord_logging_handler: logging.Handler = DiscordHandler(
service_name=self.bot.user.name if self.bot.user else "TeX-Bot",
webhook_url=settings["DISCORD_LOG_CHANNEL_WEBHOOK_URL"],
avatar_url=(
self.bot.user.avatar.url
if self.bot.user and self.bot.user.avatar
else None
),
)
discord_logging_handler.setLevel(logging.WARNING)
discord_logging_handler.setFormatter(
logging.Formatter("{levelname} | {message}", style="{")
)
logger.addHandler(discord_logging_handler)
else:
logger.warning(
"DISCORD_LOG_CHANNEL_WEBHOOK_URL was not set, "
"so error logs will not be sent to the Discord log channel."
)
try:
main_guild: discord.Guild | None = self.bot.main_guild
except GuildDoesNotExistError:
main_guild = self.bot.get_guild(settings["_DISCORD_MAIN_GUILD_ID"])
if main_guild:
self.bot.set_main_guild(main_guild)
if not main_guild:
if self.bot.application_id:
logger.info(
"Invite URL: %s",
utils.generate_invite_url(
self.bot.application_id, settings["_DISCORD_MAIN_GUILD_ID"]
),
)
logger.critical(
GuildDoesNotExistError(guild_id=settings["_DISCORD_MAIN_GUILD_ID"])
)
await self.bot.close()
if self.bot.application_id:
logger.debug(
"Invite URL: %s",
utils.generate_invite_url(
self.bot.application_id, settings["_DISCORD_MAIN_GUILD_ID"]
),
)
if not discord.utils.get(main_guild.roles, name="Committee"):
logger.warning(CommitteeRoleDoesNotExistError())
if not discord.utils.get(main_guild.roles, name="Guest"):
logger.warning(GuestRoleDoesNotExistError())
if not discord.utils.get(main_guild.roles, name="Member"):
logger.warning(MemberRoleDoesNotExistError())
if not discord.utils.get(main_guild.roles, name="Archivist"):
logger.warning(ArchivistRoleDoesNotExistError())
if not discord.utils.get(main_guild.text_channels, name="roles"):
logger.warning(RolesChannelDoesNotExistError())
if not discord.utils.get(main_guild.text_channels, name="general"):
logger.warning(GeneralChannelDoesNotExistError())
try:
await fetch_community_group_members_list()
except MSLMembershipError as msl_membership_error:
logger.debug(
"Failed to update community group member list cache on startup: %s",
msl_membership_error,
)
if settings["STRIKE_PERFORMED_MANUALLY_WARNING_LOCATION"] != "DM":
manual_moderation_warning_message_location_exists: bool = bool(
discord.utils.get(
main_guild.text_channels,
name=settings["STRIKE_PERFORMED_MANUALLY_WARNING_LOCATION"],
)
)
if not manual_moderation_warning_message_location_exists:
logger.critical(
(
"The channel %s does not exist, so cannot be used as the location "
"for sending manual-moderation warning messages"
),
repr(settings["STRIKE_PERFORMED_MANUALLY_WARNING_LOCATION"]),
)
manual_moderation_warning_message_location_similar_to_dm: bool = settings[
"STRIKE_PERFORMED_MANUALLY_WARNING_LOCATION"
].lower() in ("dm", "dms")
if manual_moderation_warning_message_location_similar_to_dm:
logger.info(
(
"If you meant to set the location "
"for sending manual-moderation warning messages to be "
"the DMs of the committee member that applied "
"the manual moderation action, use the value of %s"
),
repr("DM"),
)
await self.bot.close()
logger.info("Ready! Logged in as %s", self.bot.user)