-
Notifications
You must be signed in to change notification settings - Fork 6
/
archive.py
216 lines (191 loc) · 8.36 KB
/
archive.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
"""Contains cog classes for any archival interactions."""
from collections.abc import Sequence
__all__: Sequence[str] = ("ArchiveCommandCog",)
import logging
import re
from collections.abc import Set
from logging import Logger
from typing import Final
import discord
from exceptions import DiscordMemberNotInMainGuildError
from exceptions.base import BaseDoesNotExistError
from utils import (
AllChannelTypes,
CommandChecks,
TeXBotApplicationContext,
TeXBotAutocompleteContext,
TeXBotBaseCog,
)
logger: Final[Logger] = logging.getLogger("TeX-Bot")
class ArchiveCommandCog(TeXBotBaseCog):
"""Cog class that defines the "/archive" command and its call-back method."""
@staticmethod
async def autocomplete_get_categories(ctx: TeXBotAutocompleteContext) -> Set[discord.OptionChoice] | Set[str]: # noqa: E501
"""
Autocomplete callable that generates the set of available selectable categories.
The list of available selectable categories is unique to each member and is used in
any of the "archive" slash-command options that have a category input-type.
"""
if not ctx.interaction.user:
return set()
try:
if not await ctx.bot.check_user_has_committee_role(ctx.interaction.user):
return set()
main_guild: discord.Guild = ctx.bot.main_guild
interaction_user: discord.Member = await ctx.bot.get_main_guild_member(
ctx.interaction.user,
)
except (BaseDoesNotExistError, DiscordMemberNotInMainGuildError):
return set()
return {
discord.OptionChoice(name=category.name, value=str(category.id))
for category
in main_guild.categories
if category.permissions_for(interaction_user).is_superset(
discord.Permissions(send_messages=True, view_channel=True),
)
}
@discord.slash_command( # type: ignore[no-untyped-call, misc]
name="archive",
description="Archives the selected category.",
)
@discord.option( # type: ignore[no-untyped-call, misc]
name="category",
description="The category to archive.",
input_type=str,
autocomplete=discord.utils.basic_autocomplete(autocomplete_get_categories), # type: ignore[arg-type]
required=True,
parameter_name="str_category_id",
)
@CommandChecks.check_interaction_user_has_committee_role
@CommandChecks.check_interaction_user_in_main_guild
async def archive(self, ctx: TeXBotApplicationContext, str_category_id: str) -> None:
"""
Definition & callback response of the "archive" command.
The "archive" command hides a given category from view of casual members unless they
have the "Archivist" role.
"""
# NOTE: Shortcut accessors are placed at the top of the function, so that the exceptions they raise are displayed before any further errors may be sent
main_guild: discord.Guild = self.bot.main_guild
interaction_member: discord.Member = await self.bot.get_main_guild_member(ctx.user)
committee_role: discord.Role = await self.bot.committee_role
guest_role: discord.Role = await self.bot.guest_role
member_role: discord.Role = await self.bot.member_role
archivist_role: discord.Role = await self.bot.archivist_role
everyone_role: discord.Role = await self.bot.get_everyone_role()
if not re.fullmatch(r"\A\d{17,20}\Z", str_category_id):
await self.command_send_error(
ctx,
message=f"{str_category_id!r} is not a valid category ID.",
)
return
category_id: int = int(str_category_id)
category: discord.CategoryChannel | None = discord.utils.get(
main_guild.categories,
id=category_id,
)
if not category:
await self.command_send_error(
ctx,
message=f"Category with ID {str(category_id)!r} does not exist.",
)
return
if "archive" in category.name:
await ctx.respond(
(
":information_source: No changes made. "
"Category has already been archived. :information_source:"
),
ephemeral=True,
)
return
channel: AllChannelTypes
for channel in category.channels:
try:
CHANNEL_NEEDS_COMMITTEE_ARCHIVING: bool = (
channel.permissions_for(committee_role).is_superset(
discord.Permissions(view_channel=True),
) and not channel.permissions_for(guest_role).is_superset(
discord.Permissions(view_channel=True),
)
)
CHANNEL_NEEDS_NORMAL_ARCHIVING: bool = (
channel.permissions_for(guest_role).is_superset(
discord.Permissions(view_channel=True),
)
)
if CHANNEL_NEEDS_COMMITTEE_ARCHIVING:
await channel.set_permissions(
everyone_role,
reason=f"{interaction_member.display_name} used \"/archive\".",
view_channel=False,
)
await channel.set_permissions(
guest_role,
overwrite=None,
reason=f"{interaction_member.display_name} used \"/archive\".",
)
await channel.set_permissions(
member_role,
overwrite=None,
reason=f"{interaction_member.display_name} used \"/archive\".",
)
await channel.set_permissions(
committee_role,
overwrite=None,
reason=f"{interaction_member.display_name} used \"/archive\".",
)
elif CHANNEL_NEEDS_NORMAL_ARCHIVING:
await channel.set_permissions(
everyone_role,
reason=f"{interaction_member.display_name} used \"/archive\".",
view_channel=False,
)
await channel.set_permissions(
guest_role,
overwrite=None,
reason=f"{interaction_member.display_name} used \"/archive\".",
)
await channel.set_permissions(
member_role,
overwrite=None,
reason=f"{interaction_member.display_name} used \"/archive\".",
)
await channel.set_permissions(
committee_role,
reason=f"{interaction_member.display_name} used \"/archive\".",
view_channel=False,
)
await channel.set_permissions(
archivist_role,
reason=f"{interaction_member.display_name} used \"/archive\".",
view_channel=True,
)
else:
await self.command_send_error(
ctx,
message=f"Channel {channel.mention} had invalid permissions",
)
logger.error(
"Channel %s had invalid permissions, so could not be archived.",
channel.name,
)
return
except discord.Forbidden:
await self.command_send_error(
ctx,
message=(
"TeX-Bot does not have access to "
"the channels in the selected category."
),
)
logger.error( # noqa: TRY400
(
"TeX-Bot did not have access to "
"the channels in the selected category: "
"%s."
),
category.name,
)
return
await ctx.respond("Category successfully archived", ephemeral=True)