Skip to content

Commit ebcfe4e

Browse files
committed
Fix formatter, allow add by roles (resolve #3053)
1 parent d240e56 commit ebcfe4e

File tree

1 file changed

+161
-103
lines changed

1 file changed

+161
-103
lines changed

cogs/modmail.py

+161-103
Original file line numberDiff line numberDiff line change
@@ -671,39 +671,60 @@ async def title(self, ctx, *, name: str):
671671
await ctx.message.pin()
672672
await self.bot.add_reaction(ctx.message, sent_emoji)
673673

674-
@commands.command(usage="<users> [options]", cooldown_after_parsing=True)
674+
@commands.command(usage="<users_or_roles...> [options]", cooldown_after_parsing=True)
675675
@checks.has_permissions(PermissionLevel.SUPPORTER)
676676
@checks.thread_only()
677677
@commands.cooldown(1, 600, BucketType.channel)
678-
async def adduser(self, ctx, *users: Union[discord.Member, str]):
678+
async def adduser(self, ctx, *users_arg: Union[discord.Member, discord.Role, str]):
679679
"""Adds a user to a modmail thread
680680
681681
`options` can be `silent` or `silently`.
682682
"""
683683
silent = False
684-
for u in users:
684+
users = []
685+
for u in users_arg:
685686
if isinstance(u, str):
686687
if "silent" in u or "silently" in u:
687688
silent = True
688-
users.remove(u) # remove all strings so users is a List[Member]
689-
else:
690-
# u is a discord.Member
691-
curr_thread = await self.bot.threads.find(recipient=u)
692-
if curr_thread:
693-
em = discord.Embed(
694-
title="Error",
695-
description=f"{u.mention} is already in a thread: {curr_thread.channel.mention}.",
696-
color=self.bot.error_color,
697-
)
698-
await ctx.send(embed=em)
699-
ctx.command.reset_cooldown(ctx)
700-
return
689+
elif isinstance(u, discord.Role):
690+
users += u.members
691+
elif isinstance(u, discord.Member):
692+
users.append(u)
693+
694+
for u in users:
695+
# u is a discord.Member
696+
curr_thread = await self.bot.threads.find(recipient=u)
697+
if curr_thread == ctx.thread:
698+
users.remove(u)
699+
continue
700+
701+
if curr_thread:
702+
em = discord.Embed(
703+
title="Error",
704+
description=f"{u.mention} is already in a thread: {curr_thread.channel.mention}.",
705+
color=self.bot.error_color,
706+
)
707+
await ctx.send(embed=em)
708+
ctx.command.reset_cooldown(ctx)
709+
return
710+
711+
if not users:
712+
em = discord.Embed(
713+
title="Error",
714+
description="All users are already in the thread.",
715+
color=self.bot.error_color,
716+
)
717+
await ctx.send(embed=em)
718+
ctx.command.reset_cooldown(ctx)
719+
return
720+
701721
if not silent:
722+
description = self.bot.formatter.format(
723+
self.bot.config["private_added_to_group_description"], moderator=ctx.author
724+
)
702725
em = discord.Embed(
703726
title=self.bot.config["private_added_to_group_title"],
704-
description=self.bot.config["private_added_to_group_description"].format(
705-
moderator=ctx.author
706-
),
727+
description=description,
707728
color=self.bot.main_color,
708729
)
709730
if self.bot.config["show_timestamp"]:
@@ -712,11 +733,14 @@ async def adduser(self, ctx, *users: Union[discord.Member, str]):
712733
for u in users:
713734
await u.send(embed=em)
714735

736+
description = self.bot.formatter.format(
737+
self.bot.config["public_added_to_group_description"],
738+
moderator=ctx.author,
739+
users=", ".join(u.name for u in users),
740+
)
715741
em = discord.Embed(
716742
title=self.bot.config["public_added_to_group_title"],
717-
description=self.bot.config["private_added_to_group_description"].format(
718-
moderator=ctx.author, users=", ".join(u.name for u in users)
719-
),
743+
description=description,
720744
color=self.bot.main_color,
721745
)
722746
if self.bot.config["show_timestamp"]:
@@ -731,49 +755,55 @@ async def adduser(self, ctx, *users: Union[discord.Member, str]):
731755
sent_emoji, _ = await self.bot.retrieve_emoji()
732756
await self.bot.add_reaction(ctx.message, sent_emoji)
733757

734-
@commands.command(usage="<users...> [options]", cooldown_after_parsing=True)
758+
@commands.command(usage="<users_or_roles...> [options]", cooldown_after_parsing=True)
735759
@checks.has_permissions(PermissionLevel.SUPPORTER)
736760
@checks.thread_only()
737761
@commands.cooldown(1, 600, BucketType.channel)
738-
async def removeuser(self, ctx, *users: Union[discord.Member, str]):
762+
async def removeuser(self, ctx, *users_arg: Union[discord.Member, discord.Role, str]):
739763
"""Removes a user from a modmail thread
740764
741765
`options` can be `silent` or `silently`.
742766
"""
743767
silent = False
744-
for u in users:
768+
users = []
769+
for u in users_arg:
745770
if isinstance(u, str):
746771
if "silent" in u or "silently" in u:
747772
silent = True
748-
users.remove(u) # remove all strings so users is a List[Member]
749-
else:
750-
# u is a discord.Member
751-
curr_thread = await self.bot.threads.find(recipient=u)
752-
if ctx.thread != curr_thread:
753-
em = discord.Embed(
754-
title="Error",
755-
description=f"{u.mention} is not in this thread.",
756-
color=self.bot.error_color,
757-
)
758-
await ctx.send(embed=em)
759-
ctx.command.reset_cooldown(ctx)
760-
return
761-
elif ctx.thread.recipient == u:
762-
em = discord.Embed(
763-
title="Error",
764-
description=f"{u.mention} is the main recipient of the thread and cannot be removed.",
765-
color=self.bot.error_color,
766-
)
767-
await ctx.send(embed=em)
768-
ctx.command.reset_cooldown(ctx)
769-
return
773+
elif isinstance(u, discord.Role):
774+
users += u.members
775+
elif isinstance(u, discord.Member):
776+
users.append(u)
777+
778+
for u in users:
779+
# u is a discord.Member
780+
curr_thread = await self.bot.threads.find(recipient=u)
781+
if ctx.thread != curr_thread:
782+
em = discord.Embed(
783+
title="Error",
784+
description=f"{u.mention} is not in this thread.",
785+
color=self.bot.error_color,
786+
)
787+
await ctx.send(embed=em)
788+
ctx.command.reset_cooldown(ctx)
789+
return
790+
elif ctx.thread.recipient == u:
791+
em = discord.Embed(
792+
title="Error",
793+
description=f"{u.mention} is the main recipient of the thread and cannot be removed.",
794+
color=self.bot.error_color,
795+
)
796+
await ctx.send(embed=em)
797+
ctx.command.reset_cooldown(ctx)
798+
return
770799

771800
if not silent:
801+
description = self.bot.formatter.format(
802+
self.bot.config["private_removed_from_group_description"], moderator=ctx.author
803+
)
772804
em = discord.Embed(
773805
title=self.bot.config["private_removed_from_group_title"],
774-
description=self.bot.config["private_removed_from_group_description"].format(
775-
moderator=ctx.author
776-
),
806+
description=description,
777807
color=self.bot.main_color,
778808
)
779809
if self.bot.config["show_timestamp"]:
@@ -782,11 +812,14 @@ async def removeuser(self, ctx, *users: Union[discord.Member, str]):
782812
for u in users:
783813
await u.send(embed=em)
784814

815+
description = self.bot.formatter.format(
816+
self.bot.config["public_removed_from_group_description"],
817+
moderator=ctx.author,
818+
users=", ".join(u.name for u in users),
819+
)
785820
em = discord.Embed(
786821
title=self.bot.config["public_removed_from_group_title"],
787-
description=self.bot.config["private_removed_from_group_description"].format(
788-
moderator=ctx.author, users=", ".join(u.name for u in users)
789-
),
822+
description=description,
790823
color=self.bot.main_color,
791824
)
792825
if self.bot.config["show_timestamp"]:
@@ -801,33 +834,52 @@ async def removeuser(self, ctx, *users: Union[discord.Member, str]):
801834
sent_emoji, _ = await self.bot.retrieve_emoji()
802835
await self.bot.add_reaction(ctx.message, sent_emoji)
803836

804-
@commands.command(usage="<users...> [options]", cooldown_after_parsing=True)
837+
@commands.command(usage="<users_or_roles...> [options]", cooldown_after_parsing=True)
805838
@checks.has_permissions(PermissionLevel.SUPPORTER)
806839
@checks.thread_only()
807840
@commands.cooldown(1, 600, BucketType.channel)
808-
async def anonadduser(self, ctx, *users: Union[discord.Member, str]):
841+
async def anonadduser(self, ctx, *users_arg: Union[discord.Member, discord.Role, str]):
809842
"""Adds a user to a modmail thread anonymously
810843
811844
`options` can be `silent` or `silently`.
812845
"""
813846
silent = False
814-
for u in users:
847+
users = []
848+
for u in users_arg:
815849
if isinstance(u, str):
816850
if "silent" in u or "silently" in u:
817851
silent = True
818-
users.remove(u) # remove all strings so users is a List[Member]
819-
else:
820-
# u is a discord.Member
821-
curr_thread = await self.bot.threads.find(recipient=u)
822-
if curr_thread:
823-
em = discord.Embed(
824-
title="Error",
825-
description=f"{u.mention} is already in a thread: {curr_thread.channel.mention}.",
826-
color=self.bot.error_color,
827-
)
828-
await ctx.send(embed=em)
829-
ctx.command.reset_cooldown(ctx)
830-
return
852+
elif isinstance(u, discord.Role):
853+
users += u.members
854+
elif isinstance(u, discord.Member):
855+
users.append(u)
856+
857+
for u in users:
858+
curr_thread = await self.bot.threads.find(recipient=u)
859+
if curr_thread == ctx.thread:
860+
users.remove(u)
861+
continue
862+
863+
if curr_thread:
864+
em = discord.Embed(
865+
title="Error",
866+
description=f"{u.mention} is already in a thread: {curr_thread.channel.mention}.",
867+
color=self.bot.error_color,
868+
)
869+
await ctx.send(embed=em)
870+
ctx.command.reset_cooldown(ctx)
871+
return
872+
873+
if not users:
874+
em = discord.Embed(
875+
title="Error",
876+
description="All users are already in the thread.",
877+
color=self.bot.error_color,
878+
)
879+
await ctx.send(embed=em)
880+
ctx.command.reset_cooldown(ctx)
881+
return
882+
831883
if not silent:
832884
em = discord.Embed(
833885
title=self.bot.config["private_added_to_group_title"],
@@ -851,11 +903,13 @@ async def anonadduser(self, ctx, *users: Union[discord.Member, str]):
851903
for u in users:
852904
await u.send(embed=em)
853905

906+
description = self.bot.formatter.format(
907+
self.bot.config["public_added_to_group_description_anon"],
908+
users=", ".join(u.name for u in users),
909+
)
854910
em = discord.Embed(
855911
title=self.bot.config["public_added_to_group_title"],
856-
description=self.bot.config["private_added_to_group_description_anon"].format(
857-
moderator=ctx.author, users=", ".join(u.name for u in users)
858-
),
912+
description=description,
859913
color=self.bot.main_color,
860914
)
861915
if self.bot.config["show_timestamp"]:
@@ -870,49 +924,51 @@ async def anonadduser(self, ctx, *users: Union[discord.Member, str]):
870924
sent_emoji, _ = await self.bot.retrieve_emoji()
871925
await self.bot.add_reaction(ctx.message, sent_emoji)
872926

873-
@commands.command(usage="<users...> [options]", cooldown_after_parsing=True)
927+
@commands.command(usage="<users_or_roles...> [options]", cooldown_after_parsing=True)
874928
@checks.has_permissions(PermissionLevel.SUPPORTER)
875929
@checks.thread_only()
876930
@commands.cooldown(1, 600, BucketType.channel)
877-
async def anonremoveuser(self, ctx, *users: Union[discord.Member, str]):
931+
async def anonremoveuser(self, ctx, *users_arg: Union[discord.Member, discord.Role, str]):
878932
"""Removes a user from a modmail thread anonymously
879933
880934
`options` can be `silent` or `silently`.
881935
"""
882936
silent = False
883-
for u in users:
937+
users = []
938+
for u in users_arg:
884939
if isinstance(u, str):
885940
if "silent" in u or "silently" in u:
886941
silent = True
887-
users.remove(u) # remove all strings so users is a List[Member]
888-
else:
889-
# u is a discord.Member
890-
curr_thread = await self.bot.threads.find(recipient=u)
891-
if ctx.thread != curr_thread:
892-
em = discord.Embed(
893-
title="Error",
894-
description=f"{u.mention} is not in this thread.",
895-
color=self.bot.error_color,
896-
)
897-
await ctx.send(embed=em)
898-
ctx.command.reset_cooldown(ctx)
899-
return
900-
elif ctx.thread.recipient == u:
901-
em = discord.Embed(
902-
title="Error",
903-
description=f"{u.mention} is the main recipient of the thread and cannot be removed.",
904-
color=self.bot.error_color,
905-
)
906-
await ctx.send(embed=em)
907-
ctx.command.reset_cooldown(ctx)
908-
return
942+
elif isinstance(u, discord.Role):
943+
users += u.members
944+
elif isinstance(u, discord.Member):
945+
users.append(u)
946+
947+
for u in users:
948+
curr_thread = await self.bot.threads.find(recipient=u)
949+
if ctx.thread != curr_thread:
950+
em = discord.Embed(
951+
title="Error",
952+
description=f"{u.mention} is not in this thread.",
953+
color=self.bot.error_color,
954+
)
955+
await ctx.send(embed=em)
956+
ctx.command.reset_cooldown(ctx)
957+
return
958+
elif ctx.thread.recipient == u:
959+
em = discord.Embed(
960+
title="Error",
961+
description=f"{u.mention} is the main recipient of the thread and cannot be removed.",
962+
color=self.bot.error_color,
963+
)
964+
await ctx.send(embed=em)
965+
ctx.command.reset_cooldown(ctx)
966+
return
909967

910968
if not silent:
911969
em = discord.Embed(
912970
title=self.bot.config["private_removed_from_group_title"],
913-
description=self.bot.config["private_removed_from_group_description_anon"].format(
914-
moderator=ctx.author
915-
),
971+
description=self.bot.config["private_removed_from_group_description_anon"],
916972
color=self.bot.main_color,
917973
)
918974
if self.bot.config["show_timestamp"]:
@@ -932,11 +988,13 @@ async def anonremoveuser(self, ctx, *users: Union[discord.Member, str]):
932988
for u in users:
933989
await u.send(embed=em)
934990

991+
description = self.bot.formatter.format(
992+
self.bot.config["public_removed_from_group_description_anon"],
993+
users=", ".join(u.name for u in users),
994+
)
935995
em = discord.Embed(
936996
title=self.bot.config["public_removed_from_group_title"],
937-
description=self.bot.config["private_removed_from_group_description"].format(
938-
moderator=ctx.author, users=", ".join(u.name for u in users)
939-
),
997+
description=description,
940998
color=self.bot.main_color,
941999
)
9421000
if self.bot.config["show_timestamp"]:

0 commit comments

Comments
 (0)