Skip to content

Commit c95eb91

Browse files
Fixes
1 parent d33d3f2 commit c95eb91

3 files changed

Lines changed: 43 additions & 42 deletions

File tree

cogs/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from typing import TYPE_CHECKING
99

10-
from .add_users_to_threads_and_channels import AddUsersToThreadsAndChannelsCog
10+
from .add_users_to_threads_and_channels import AddUsersToThreadsAndChannelsCommandCog
1111
from .annual_handover_and_reset import (
1212
AnnualRolesResetCommandCog,
1313
AnnualYearChannelsIncrementCommandCog,
@@ -47,7 +47,7 @@
4747
from utils import TeXBot, TeXBotBaseCog
4848

4949
__all__: "Sequence[str]" = (
50-
"AddUsersToThreadsAndChannelsCog",
50+
"AddUsersToThreadsAndChannelsCommandCog",
5151
"AnnualRolesResetCommandCog",
5252
"AnnualYearChannelsIncrementCommandCog",
5353
"ArchiveCommandCog",
@@ -86,7 +86,7 @@
8686
def setup(bot: "TeXBot") -> None:
8787
"""Add all the cogs to the bot, at bot startup."""
8888
cogs: Iterable[type[TeXBotBaseCog]] = (
89-
AddUsersToThreadsAndChannelsCog,
89+
AddUsersToThreadsAndChannelsCommandCog,
9090
AnnualRolesResetCommandCog,
9191
AnnualYearChannelsIncrementCommandCog,
9292
ArchiveCommandCog,

cogs/add_users_to_threads_and_channels.py

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
from utils import TeXBotApplicationContext, TeXBotAutocompleteContext
2121

2222

23-
__all__: "Sequence[str]" = ("AddUsersToThreadsAndChannelsCog",)
23+
__all__: "Sequence[str]" = ("AddUsersToThreadsAndChannelsCommandCog",)
2424

2525

2626
logger: "Final[Logger]" = logging.getLogger("TeX-Bot")
2727

2828

29-
class AddUsersToThreadsAndChannelsCog(TeXBotBaseCog):
29+
class AddUsersToThreadsAndChannelsCommandCog(TeXBotBaseCog):
3030
"""Cog for adding users to threads."""
3131

3232
@staticmethod
@@ -81,8 +81,8 @@ async def add_users_or_roles_silently(
8181
self,
8282
users_or_roles: discord.Member
8383
| discord.Role
84-
| list[discord.Member]
85-
| list[discord.Role],
84+
| Iterable[discord.Member]
85+
| Iterable[discord.Role],
8686
thread: discord.Thread,
8787
) -> None:
8888
"""Add a user or role to a thread without pinging them."""
@@ -149,19 +149,20 @@ async def add_users_or_roles_with_ping(
149149
async def on_thread_create(self, thread: discord.Thread) -> None:
150150
"""Add users to a thread when it is created."""
151151
# 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
152-
if not settings["ADD_COMMITTEE_TO_THREADS"]:
153-
return
154-
155152
committee_role: discord.Role = await self.bot.committee_role
156153
committee_elect_role: discord.Role = await self.bot.committee_elect_role
157154

158-
if thread.parent is None or thread.parent.category is None:
155+
if (
156+
thread.parent is None
157+
or thread.parent.category is None
158+
or "committee" not in thread.parent.category.name.lower()
159+
or not settings["AUTO_ADD_COMMITTEE_TO_THREADS"]
160+
):
159161
return
160162

161-
if "committee" in thread.parent.category.name.lower():
162-
await self.add_users_or_roles_silently(
163-
users_or_roles=[committee_role, committee_elect_role], thread=thread
164-
)
163+
await self.add_users_or_roles_silently(
164+
users_or_roles=[committee_role, committee_elect_role], thread=thread
165+
)
165166

166167
@discord.slash_command( # type: ignore[no-untyped-call, misc]
167168
name="add_users_to_channel",
@@ -189,46 +190,44 @@ async def add_user_to_channel( # type: ignore[misc]
189190
silent: bool, # noqa: FBT001
190191
) -> None:
191192
"""Add users or roles to a channel."""
192-
if not isinstance(ctx.channel, discord.TextChannel) and not isinstance(
193-
ctx.channel, discord.Thread
194-
):
193+
if not isinstance(ctx.channel, (discord.TextChannel, discord.Thread)):
195194
await self.command_send_error(
196195
ctx=ctx,
197196
message="This command currently only supports text channels or threads.",
198197
)
199198
return
200199

201-
channel: discord.TextChannel | discord.Thread = ctx.channel
202-
203200
try:
204201
user_to_add: discord.Member = await self.bot.get_member_from_str_id(user_id_str)
205202
except ValueError:
206203
logger.debug("User ID: %s is not a valid ID.", user_id_str)
207204
await ctx.respond(content=f"The user: {user_id_str} is not valid.")
208205
return
209206

210-
if isinstance(channel, discord.TextChannel):
211-
await channel.set_permissions(
207+
if isinstance(ctx.channel, discord.TextChannel):
208+
await ctx.channel.set_permissions(
212209
target=user_to_add,
213210
read_messages=True,
214211
send_messages=True,
215212
reason=f"User {ctx.user} used TeX-Bot slash-command `add_user_to_channel`.",
216213
)
217214

218215
if not silent:
219-
await channel.send(
216+
await ctx.channel.send(
220217
content=f"{user_to_add.mention} has been added to the channel."
221218
)
222219

223-
if isinstance(channel, discord.Thread):
220+
if isinstance(ctx.channel, discord.Thread):
224221
if silent:
225-
await self.add_users_or_roles_silently(user_to_add, channel)
222+
await self.add_users_or_roles_silently(user_to_add, ctx.channel)
226223
else:
227-
await self.add_users_or_roles_with_ping(user_to_add, channel)
224+
await self.add_users_or_roles_with_ping(user_to_add, ctx.channel)
228225

229226
await ctx.respond(
230-
content=f"User {user_to_add.mention} has been added to the channel.",
231-
ephemeral=True,
227+
content=(
228+
f"Successfully added {user_to_add.mention} "
229+
f"to the channel: {ctx.channel.mention}."
230+
)
232231
)
233232

234233
@discord.slash_command( # type: ignore[no-untyped-call, misc]
@@ -276,9 +275,9 @@ async def add_role_to_channel( # type: ignore[misc]
276275
await ctx.respond(content=f"The role: {role_id_str} is not valid.")
277276
return
278277

279-
role_object: discord.Role | None = discord.utils.get(main_guild.roles, id=role_id)
278+
role_to_add: discord.Role | None = discord.utils.get(main_guild.roles, id=role_id)
280279

281-
if role_object is None:
280+
if role_to_add is None:
282281
await self.command_send_error(
283282
ctx=ctx,
284283
message=f"The role: <@{role_id}> is not valid or couldn't be found.",
@@ -287,24 +286,24 @@ async def add_role_to_channel( # type: ignore[misc]
287286

288287
if isinstance(channel, discord.TextChannel):
289288
await channel.set_permissions(
290-
target=role_object,
289+
target=role_to_add,
291290
read_messages=True,
292291
send_messages=True,
293292
reason=f"User {ctx.user} used TeX-Bot slash-command `add_role_to_channel`.",
294293
)
295294

296295
if not silent:
297296
await channel.send(
298-
content=f"{role_object.mention} has been added to the channel."
297+
content=f"{role_to_add.mention} has been added to the channel."
299298
)
300299

301300
if isinstance(channel, discord.Thread):
302301
if silent:
303-
await self.add_users_or_roles_silently(role_object, channel)
302+
await self.add_users_or_roles_silently(role_to_add, channel)
304303
else:
305-
await self.add_users_or_roles_with_ping(role_object, channel)
304+
await self.add_users_or_roles_with_ping(role_to_add, channel)
306305

307306
await ctx.respond(
308-
content=f"Role {role_object.mention} has been added to the channel.",
307+
content=f"Role {role_to_add.mention} has been added to the channel.",
309308
ephemeral=True,
310309
)

config.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -680,17 +680,19 @@ def _setup_strike_performed_manually_warning_location(cls) -> None:
680680

681681
@classmethod
682682
def _setup_add_committee_to_threads(cls) -> None:
683-
raw_add_committee_to_threads: str = str(
684-
os.getenv("ADD_COMMITTEE_TO_THREADS", "True")
683+
raw_auto_add_committee_to_threads: str = str(
684+
os.getenv("AUTO_ADD_COMMITTEE_TO_THREADS", "True")
685685
).lower()
686686

687-
if raw_add_committee_to_threads not in TRUE_VALUES | FALSE_VALUES:
688-
INVALID_ADD_COMMITTEE_TO_THREADS_MESSAGE: Final[str] = (
689-
"ADD_COMMITTEE_TO_THREADS must be a boolean value."
687+
if raw_auto_add_committee_to_threads not in TRUE_VALUES | FALSE_VALUES:
688+
INVALID_AUTO_ADD_COMMITTEE_TO_THREADS_MESSAGE: Final[str] = (
689+
"AUTO_ADD_COMMITTEE_TO_THREADS must be a boolean value."
690690
)
691-
raise ImproperlyConfiguredError(INVALID_ADD_COMMITTEE_TO_THREADS_MESSAGE)
691+
raise ImproperlyConfiguredError(INVALID_AUTO_ADD_COMMITTEE_TO_THREADS_MESSAGE)
692692

693-
cls._settings["ADD_COMMITTEE_TO_THREADS"] = raw_add_committee_to_threads in TRUE_VALUES
693+
cls._settings["AUTO_ADD_COMMITTEE_TO_THREADS"] = (
694+
raw_auto_add_committee_to_threads in TRUE_VALUES
695+
)
694696

695697
@classmethod
696698
def _setup_env_variables(cls) -> None:

0 commit comments

Comments
 (0)