2020 from utils import TeXBotApplicationContext , TeXBotAutocompleteContext
2121
2222
23- __all__ : "Sequence[str]" = ("AddUsersToThreadsAndChannelsCog " ,)
23+ __all__ : "Sequence[str]" = ("AddUsersToThreadsAndChannelsCommandCog " ,)
2424
2525
2626logger : "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 )
0 commit comments