Skip to content

Commit 069481e

Browse files
Preserve mention permissions in reminders (#11)
* Add explicit allowed_mentions to reminder dispatch Co-Authored-By: Oliver Ni <oliver.ni@gmail.com> * Preserve mention permissions in reminders Co-Authored-By: Oliver Ni <oliver.ni@gmail.com> * Store specific mention user/role IDs, rename columns to mention_* Co-Authored-By: Oliver Ni <oliver.ni@gmail.com> * Always allow pinging reminder creator in dispatch Co-Authored-By: Oliver Ni <oliver.ni@gmail.com> * Remove mention_user_ids, always allow user mentions Co-Authored-By: Oliver Ni <oliver.ni@gmail.com> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Oliver Ni <oliver.ni@gmail.com>
1 parent bd36492 commit 069481e

4 files changed

Lines changed: 28 additions & 8 deletions

File tree

bmt_discord_bot/cogs/reminders.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ async def reminder(
3131
*,
3232
time_and_content: Annotated[
3333
time.FriendlyTimeResult,
34-
time.UserFriendlyTime(commands.clean_content, default="\u2026"),
34+
time.UserFriendlyTime(default="\u2026"),
3535
],
3636
):
3737
"""Sets a reminder for a date or duration of time, e.g.:
@@ -43,11 +43,14 @@ async def reminder(
4343
Times are parsed as US Pacific Time.
4444
"""
4545

46+
mention_everyone = ctx.message.mention_everyone
47+
mention_role_ids = [r.id for r in ctx.message.role_mentions]
48+
4649
reminder = await ctx.bot.database.pool.fetchrow(
4750
"""
48-
INSERT INTO reminders (user_id, event, guild_id, channel_id, message_id, created_at, expires_at)
49-
VALUES ($1, $2, $3, $4, $5, $6, $7)
50-
RETURNING id, user_id, event, channel_id, message_id, created_at, expires_at
51+
INSERT INTO reminders (user_id, event, guild_id, channel_id, message_id, created_at, expires_at, mention_everyone, mention_role_ids)
52+
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
53+
RETURNING id, user_id, event, channel_id, message_id, created_at, expires_at, mention_everyone, mention_role_ids
5154
""",
5255
ctx.author.id,
5356
time_and_content.arg,
@@ -56,10 +59,13 @@ async def reminder(
5659
ctx.message.id,
5760
ctx.message.created_at,
5861
time_and_content.dt,
62+
mention_everyone,
63+
mention_role_ids,
5964
)
6065
self.bot.loop.create_task(self.update_current(reminder))
6166
await ctx.send(
62-
f"Alright, I'll remind you in **{time.human_timedelta(time_and_content.dt, source=ctx.message.created_at)}**: {time_and_content.arg}"
67+
f"Alright, I'll remind you in **{time.human_timedelta(time_and_content.dt, source=ctx.message.created_at)}**: {time_and_content.arg}",
68+
allowed_mentions=discord.AllowedMentions.none(),
6369
)
6470

6571
@reminder.command()
@@ -115,7 +121,7 @@ async def delete(self, ctx: Context, ids: commands.Greedy[int]):
115121
async def get_next_reminder(self):
116122
return await self.bot.database.pool.fetchrow(
117123
"""
118-
SELECT id, user_id, event, channel_id, message_id, created_at, expires_at
124+
SELECT id, user_id, event, channel_id, message_id, created_at, expires_at, mention_everyone, mention_role_ids
119125
FROM reminders
120126
WHERE NOT is_resolved
121127
ORDER BY expires_at
@@ -165,12 +171,19 @@ async def dispatch_reminder(self, reminder):
165171
text = f"<@{reminder['user_id']}> {text}"
166172
reference = None
167173

174+
allowed_mentions = discord.AllowedMentions(
175+
everyone=reminder["mention_everyone"],
176+
roles=[discord.Object(id=r) for r in reminder["mention_role_ids"]],
177+
users=True,
178+
replied_user=True,
179+
)
180+
168181
try:
169-
await channel.send(text, reference=reference)
182+
await channel.send(text, reference=reference, allowed_mentions=allowed_mentions)
170183
except (discord.NotFound, discord.Forbidden):
171184
return await self.bot.database.pool.execute(
172185
"UPDATE reminders SET is_failed = True WHERE id = $1",
173-
reminder.id,
186+
reminder["id"],
174187
)
175188

176189
self.bot.loop.create_task(self.update_current())

bmt_discord_bot/database.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class Database:
2525
Migration.from_files("0001_reminders"),
2626
Migration.from_files("0002_math"),
2727
Migration.from_files("0003_copycat"),
28+
Migration.from_files("0004_reminder_allowed_mentions"),
2829
]
2930

3031
def __init__(self, pool: asyncpg.Pool):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ALTER TABLE reminders
2+
ADD COLUMN mention_everyone BOOLEAN NOT NULL DEFAULT FALSE,
3+
ADD COLUMN mention_role_ids BIGINT[] NOT NULL DEFAULT '{}';
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ALTER TABLE reminders
2+
DROP COLUMN mention_everyone,
3+
DROP COLUMN mention_role_ids;

0 commit comments

Comments
 (0)