Skip to content

Commit df53007

Browse files
Implement multiple roles and users adding at once
1 parent 933e462 commit df53007

1 file changed

Lines changed: 47 additions & 18 deletions

File tree

cogs/add_users_to_threads_and_channels.py

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Contains Cog classes for adding users and roles to threads."""
22

33
import logging
4+
from collections.abc import Iterable
45
from typing import TYPE_CHECKING
56

67
import discord
@@ -76,36 +77,62 @@ async def autocomplete_get_roles(
7677
for role in main_guild.roles
7778
}
7879

79-
async def add_user_or_role_silently(
80-
self, user_or_role: discord.Member | discord.Role, thread: discord.Thread
80+
async def add_users_or_roles_silently(
81+
self,
82+
users_or_roles: discord.Member
83+
| discord.Role
84+
| list[discord.Member]
85+
| list[discord.Role],
86+
thread: discord.Thread,
8187
) -> None:
8288
"""Add a user or role to a thread without pinging them."""
89+
if isinstance(users_or_roles, Iterable):
90+
user_or_role: discord.Role | discord.Member
91+
for user_or_role in users_or_roles:
92+
await self.add_users_or_roles_silently(
93+
users_or_roles=user_or_role, thread=thread
94+
)
95+
return
96+
8397
message: discord.Message = await thread.send(
84-
content=f"Adding {user_or_role!r} to thread...",
98+
content=f"Adding {users_or_roles!r} to thread...",
8599
silent=True,
86100
)
87-
await message.edit(content=f"{user_or_role.mention}")
101+
await message.edit(content=f"{users_or_roles.mention}")
88102
await message.delete(delay=1)
89103

90-
async def add_user_or_role_with_ping(
91-
self, user_or_role: discord.Member | discord.Role, thread: discord.Thread
104+
async def add_users_or_roles_with_ping(
105+
self,
106+
users_or_roles: discord.Member
107+
| discord.Role
108+
| list[discord.Member]
109+
| list[discord.Role],
110+
thread: discord.Thread,
92111
) -> None:
93112
"""Add a user or role to a thread and ping them."""
94-
if isinstance(user_or_role, discord.Member):
113+
if isinstance(users_or_roles, Iterable):
114+
user_or_role: discord.Role | discord.Member
115+
for user_or_role in users_or_roles:
116+
await self.add_users_or_roles_with_ping(
117+
users_or_roles=user_or_role, thread=thread
118+
)
119+
return
120+
121+
if isinstance(users_or_roles, discord.Member):
95122
try:
96-
await thread.add_user(user=user_or_role)
123+
await thread.add_user(user=users_or_roles)
97124
except discord.NotFound:
98125
logger.debug(
99126
"User: %s has blocked the bot and "
100127
"therefore could not be added to thread: %s.",
101-
user_or_role,
128+
users_or_roles,
102129
thread,
103130
)
104131
return
105132

106-
if isinstance(user_or_role, discord.Role):
133+
if isinstance(users_or_roles, discord.Role):
107134
member: discord.Member
108-
for member in user_or_role.members:
135+
for member in users_or_roles.members:
109136
try:
110137
await thread.add_user(member)
111138
except discord.NotFound:
@@ -132,9 +159,11 @@ async def on_thread_create(self, thread: discord.Thread) -> None:
132159
return
133160

134161
if "committee" in thread.parent.category.name.lower():
135-
await self.add_user_or_role_silently(user_or_role=committee_role, thread=thread)
136-
await self.add_user_or_role_silently(
137-
user_or_role=committee_elect_role,
162+
await self.add_users_or_roles_silently(
163+
users_or_roles=committee_role, thread=thread
164+
)
165+
await self.add_users_or_roles_silently(
166+
users_or_roles=committee_elect_role,
138167
thread=thread,
139168
)
140169

@@ -197,9 +226,9 @@ async def add_user_to_channel( # type: ignore[misc]
197226

198227
if isinstance(channel, discord.Thread):
199228
if silent:
200-
await self.add_user_or_role_silently(user_to_add, channel)
229+
await self.add_users_or_roles_silently(user_to_add, channel)
201230
else:
202-
await self.add_user_or_role_with_ping(user_to_add, channel)
231+
await self.add_users_or_roles_with_ping(user_to_add, channel)
203232

204233
await ctx.respond(
205234
content=f"User {user_to_add.mention} has been added to the channel.",
@@ -275,9 +304,9 @@ async def add_role_to_channel( # type: ignore[misc]
275304

276305
if isinstance(channel, discord.Thread):
277306
if silent:
278-
await self.add_user_or_role_silently(role_object, channel)
307+
await self.add_users_or_roles_silently(role_object, channel)
279308
else:
280-
await self.add_user_or_role_with_ping(role_object, channel)
309+
await self.add_users_or_roles_with_ping(role_object, channel)
281310

282311
await ctx.respond(
283312
content=f"Role {role_object.mention} has been added to the channel.",

0 commit comments

Comments
 (0)