From 8243f78f4318153db99fd7ce1fcbce193bfaac53 Mon Sep 17 00:00:00 2001 From: onerandomusername Date: Sat, 8 Nov 2025 18:38:48 -0500 Subject: [PATCH 1/3] fix: add allowed mentions to bot construction --- bot/__main__.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/bot/__main__.py b/bot/__main__.py index a4fe4c7..beb292b 100644 --- a/bot/__main__.py +++ b/bot/__main__.py @@ -58,6 +58,11 @@ async def main() -> None: guild_id=constants.Bot.guild, allowed_roles=allowed_roles, command_prefix=constants.Bot.prefix, + allowed_mentions=discord.AllowedMentions( + everyone=False, + roles=False, + users=True, + ), activity=discord.Game("The Not-Quite-So-Bot-as-Sir-Lancebot"), intents=_intents, ) From 083275bb324720f1b744b7900d7781b3337fc9a9 Mon Sep 17 00:00:00 2001 From: onerandomusername Date: Sat, 8 Nov 2025 18:39:10 -0500 Subject: [PATCH 2/3] enable mentions where needed --- bot/exts/code_jams/_cog.py | 2 +- bot/exts/code_jams/_creation_utils.py | 5 ++++- bot/exts/code_jams/_views.py | 5 +++-- bot/exts/games.py | 3 ++- bot/exts/summer_aoc.py | 1 + bot/utils/__init__.py | 23 ----------------------- 6 files changed, 11 insertions(+), 28 deletions(-) diff --git a/bot/exts/code_jams/_cog.py b/bot/exts/code_jams/_cog.py index d9644ad..a08a218 100644 --- a/bot/exts/code_jams/_cog.py +++ b/bot/exts/code_jams/_cog.py @@ -272,7 +272,7 @@ async def ping_codejam_team(self, ctx: commands.Context) -> None: log.error("Failed to find '%s' in CJMS.", ctx.channel.name) await ctx.send("Failed to find team role id in database.") return - await ctx.send(f"<@&{role_id}>") + await ctx.send(f"<@&{role_id}>", allowed_mentions=discord.AllowedMentions(roles=[discord.Object(role_id)])) @staticmethod def jam_categories(guild: Guild) -> list[discord.CategoryChannel]: diff --git a/bot/exts/code_jams/_creation_utils.py b/bot/exts/code_jams/_creation_utils.py index 165e9e6..d469b00 100644 --- a/bot/exts/code_jams/_creation_utils.py +++ b/bot/exts/code_jams/_creation_utils.py @@ -179,7 +179,10 @@ async def _send_status_update(guild: discord.Guild, message: str) -> None: """Inform the events lead with a status update when the command is ran.""" channel: discord.TextChannel = guild.get_channel(Channels.code_jam_planning) - await channel.send(f"<@&{Roles.events_lead}>\n\n{message}") + await channel.send( + f"<@&{Roles.events_lead}>\n\n{message}", + allowed_mentions=discord.AllowedMentions(roles=[discord.Object(Roles.events_lead)]), + ) async def _add_team_leader_roles(members: list[dict[str: discord.Member, str: bool]], diff --git a/bot/exts/code_jams/_views.py b/bot/exts/code_jams/_views.py index 8354390..78f8190 100644 --- a/bot/exts/code_jams/_views.py +++ b/bot/exts/code_jams/_views.py @@ -66,8 +66,9 @@ async def announce(self, interaction: discord.Interaction, button: discord.ui.Bu await announcements.send( f"<@&{Roles.code_jam_participants}> ! You have been sorted into a team!" " Click the button below to get a detailed description!", - view=JamTeamInfoView(self.bot) - ) + view=JamTeamInfoView(self.bot), + allowed_mentions=discord.AllowedMentions(roles=[discord.Object(id=Roles.code_jam_participants)]) + ) teams = await self.bot.code_jam_mgmt_api.get( "teams/", diff --git a/bot/exts/games.py b/bot/exts/games.py index 35880c8..8f90d80 100644 --- a/bot/exts/games.py +++ b/bot/exts/games.py @@ -284,7 +284,8 @@ async def join(self, ctx: commands.Context) -> None: adjective: str = random.choice(TEAM_ADJECTIVES[team_with_fewest_members]) await ctx.reply( - f"You seem to be extremely {adjective}. You shall be assigned to... {role_with_fewest_members.mention}!" + f"You seem to be extremely {adjective}. You shall be assigned to... {role_with_fewest_members.mention}!", + allowed_mentions=discord.AllowedMentions(roles=False, replied_user=True), ) @games_command_group.command(aliases=("score", "points", "leaderboard", "lb")) diff --git a/bot/exts/summer_aoc.py b/bot/exts/summer_aoc.py index 856cf76..c002807 100644 --- a/bot/exts/summer_aoc.py +++ b/bot/exts/summer_aoc.py @@ -300,6 +300,7 @@ async def post_puzzle(self) -> None: thread_starter = await channel.send( f"<@&{Roles.summer_aoc}>", embed=self.get_puzzle_embed(), + allowed_mentions=discord.AllowedMentions(roles=[discord.Object(Roles.summer_aoc)]) ) await thread_starter.create_thread(name=f"Day {self.current_day} Spoilers") diff --git a/bot/utils/__init__.py b/bot/utils/__init__.py index 5aa6e05..33a08bc 100644 --- a/bot/utils/__init__.py +++ b/bot/utils/__init__.py @@ -1,5 +1,4 @@ import asyncio -import contextlib import re import string from collections.abc import Iterable @@ -153,25 +152,3 @@ def _repl(match: re.Match) -> str: return replacement.lower() return regex.sub(_repl, sentence) - - -@contextlib.asynccontextmanager -async def unlocked_role(role: discord.Role, delay: int = 5) -> None: - """ - Create a context in which `role` is unlocked, relocking it automatically after use. - - A configurable `delay` is added before yielding the context and directly after exiting the - context to allow the role settings change to properly propagate at Discord's end. This - prevents things like role mentions from failing because of synchronization issues. - - Usage: - >>> async with unlocked_role(role, delay=5): - ... await ctx.send(f"Hey {role.mention}, free pings for everyone!") - """ - await role.edit(mentionable=True) - await asyncio.sleep(delay) - try: - yield - finally: - await asyncio.sleep(delay) - await role.edit(mentionable=False) From 9bb5cd80c369cb25f5b39b14b2e3d5b9be9f3e10 Mon Sep 17 00:00:00 2001 From: z Date: Sat, 8 Nov 2025 18:53:20 -0500 Subject: [PATCH 3/3] Update bot/exts/code_jams/_views.py Co-authored-by: Boris Muratov <8bee278@gmail.com> --- bot/exts/code_jams/_views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/exts/code_jams/_views.py b/bot/exts/code_jams/_views.py index 78f8190..701800a 100644 --- a/bot/exts/code_jams/_views.py +++ b/bot/exts/code_jams/_views.py @@ -68,7 +68,7 @@ async def announce(self, interaction: discord.Interaction, button: discord.ui.Bu " Click the button below to get a detailed description!", view=JamTeamInfoView(self.bot), allowed_mentions=discord.AllowedMentions(roles=[discord.Object(id=Roles.code_jam_participants)]) - ) + ) teams = await self.bot.code_jam_mgmt_api.get( "teams/",