Skip to content

Commit bab4957

Browse files
improve method flow
1 parent 22aab60 commit bab4957

1 file changed

Lines changed: 63 additions & 70 deletions

File tree

cogs/add_users_to_threads_and_channels.py

Lines changed: 63 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -83,64 +83,85 @@ async def add_users_or_roles_silently(
8383
| discord.Role
8484
| Iterable[discord.Member]
8585
| Iterable[discord.Role],
86-
thread: discord.Thread,
86+
channel_or_thread: discord.Thread | discord.TextChannel,
8787
) -> None:
8888
"""Add a user or role to a thread without pinging them."""
8989
if isinstance(users_or_roles, Iterable):
9090
user_or_role: discord.Role | discord.Member
9191
for user_or_role in users_or_roles:
9292
await self.add_users_or_roles_silently(
93-
users_or_roles=user_or_role, thread=thread
93+
users_or_roles=user_or_role, channel_or_thread=channel_or_thread
9494
)
9595
return
9696

97-
message: discord.Message = await thread.send(
98-
content=f"Adding {users_or_roles!r} to thread...",
99-
silent=True,
97+
if isinstance(channel_or_thread, discord.Thread):
98+
message: discord.Message = await channel_or_thread.send(
99+
content=f"Adding {users_or_roles!r} to thread...",
100+
silent=True,
101+
)
102+
await message.edit(content=f"{users_or_roles.mention}")
103+
await message.delete(delay=1)
104+
105+
await channel_or_thread.set_permissions(
106+
target=users_or_roles,
107+
read_messages=True,
108+
send_messages=True,
109+
reason=f"User {self.bot.user} used TeX-Bot slash-command `add_users_to_channel`.",
100110
)
101-
await message.edit(content=f"{users_or_roles.mention}")
102-
await message.delete(delay=1)
103111

104112
async def add_users_or_roles_with_ping(
105113
self,
106114
users_or_roles: discord.Member
107115
| discord.Role
108-
| list[discord.Member]
109-
| list[discord.Role],
110-
thread: discord.Thread,
116+
| Iterable[discord.Member]
117+
| Iterable[discord.Role],
118+
channel_or_thread: discord.Thread | discord.TextChannel,
111119
) -> None:
112120
"""Add a user or role to a thread and ping them."""
113121
if isinstance(users_or_roles, Iterable):
114122
user_or_role: discord.Role | discord.Member
115123
for user_or_role in users_or_roles:
116124
await self.add_users_or_roles_with_ping(
117-
users_or_roles=user_or_role, thread=thread
125+
users_or_roles=user_or_role, channel_or_thread=channel_or_thread
118126
)
119127
return
120128

121-
if isinstance(users_or_roles, discord.Member):
122-
try:
123-
await thread.add_user(user=users_or_roles)
124-
except discord.NotFound:
125-
logger.debug(
126-
"User: %s has blocked the bot and "
127-
"therefore could not be added to thread: %s.",
128-
users_or_roles,
129-
thread,
130-
)
131-
return
129+
if isinstance(channel_or_thread, discord.Thread):
130+
if isinstance(users_or_roles, discord.Member):
131+
try:
132+
await channel_or_thread.add_user(user=users_or_roles)
133+
except discord.NotFound:
134+
logger.debug(
135+
"User: %s has blocked the bot and "
136+
"therefore could not be added to thread: %s.",
137+
users_or_roles,
138+
channel_or_thread,
139+
)
140+
return
141+
142+
member: discord.Member
143+
for member in users_or_roles.members:
144+
try:
145+
await channel_or_thread.add_user(member)
146+
except discord.NotFound:
147+
logger.debug(
148+
"User: %s has blocked the bot and "
149+
"therefore could not be added to thread: %s.",
150+
member,
151+
channel_or_thread,
152+
)
153+
return
154+
155+
await channel_or_thread.set_permissions(
156+
target=users_or_roles,
157+
read_messages=True,
158+
send_messages=True,
159+
reason=f"User {self.bot.user} used TeX-Bot slash-command `add_users_to_channel`.",
160+
)
132161

133-
member: discord.Member
134-
for member in users_or_roles.members:
135-
try:
136-
await thread.add_user(member)
137-
except discord.NotFound:
138-
logger.debug(
139-
"User: %s has blocked the bot and "
140-
"therefore could not be added to thread: %s.",
141-
member,
142-
thread,
143-
)
162+
await channel_or_thread.send(
163+
content=f"{users_or_roles.mention} has been added to the channel.",
164+
)
144165

145166
@TeXBotBaseCog.listener()
146167
@capture_guild_does_not_exist_error
@@ -159,7 +180,7 @@ async def on_thread_create(self, thread: discord.Thread) -> None:
159180
return
160181

161182
await self.add_users_or_roles_silently(
162-
users_or_roles=(committee_role, committee_elect_role), thread=thread
183+
users_or_roles=(committee_role, committee_elect_role), channel_or_thread=thread
163184
)
164185

165186
@discord.slash_command( # type: ignore[no-untyped-call, misc]
@@ -202,24 +223,10 @@ async def add_user_to_channel( # type: ignore[misc]
202223
await ctx.respond(content=f"The user: {user_id_str} is not valid.")
203224
return
204225

205-
if isinstance(ctx.channel, discord.TextChannel):
206-
await ctx.channel.set_permissions(
207-
target=user_to_add,
208-
read_messages=True,
209-
send_messages=True,
210-
reason=f"User {ctx.user} used TeX-Bot slash-command `add_user_to_channel`.",
211-
)
212-
213-
if not silent:
214-
await ctx.channel.send(
215-
content=f"{user_to_add.mention} has been added to the channel."
216-
)
217-
218-
if isinstance(ctx.channel, discord.Thread):
219-
if silent:
220-
await self.add_users_or_roles_silently(user_to_add, ctx.channel)
221-
else:
222-
await self.add_users_or_roles_with_ping(user_to_add, ctx.channel)
226+
if silent:
227+
await self.add_users_or_roles_silently(user_to_add, ctx.channel)
228+
else:
229+
await self.add_users_or_roles_with_ping(user_to_add, ctx.channel)
223230

224231
await ctx.respond(
225232
content=(
@@ -280,24 +287,10 @@ async def add_role_to_channel( # type: ignore[misc]
280287
)
281288
return
282289

283-
if isinstance(ctx.channel, discord.TextChannel):
284-
await ctx.channel.set_permissions(
285-
target=role_to_add,
286-
read_messages=True,
287-
send_messages=True,
288-
reason=f"User {ctx.user} used TeX-Bot slash-command `add_role_to_channel`.",
289-
)
290-
291-
if not silent:
292-
await ctx.channel.send(
293-
content=f"{role_to_add.mention} has been added to the channel."
294-
)
295-
296-
if isinstance(ctx.channel, discord.Thread):
297-
if silent:
298-
await self.add_users_or_roles_silently(role_to_add, ctx.channel)
299-
else:
300-
await self.add_users_or_roles_with_ping(role_to_add, ctx.channel)
290+
if silent:
291+
await self.add_users_or_roles_silently(role_to_add, ctx.channel)
292+
else:
293+
await self.add_users_or_roles_with_ping(role_to_add, ctx.channel)
301294

302295
await ctx.respond(
303296
content=f"Role {role_to_add.mention} has been added to the channel.",

0 commit comments

Comments
 (0)