Skip to content

Commit ff711e5

Browse files
committed
make deleting a bookmark possible through a context menu command
1 parent d418e9f commit ff711e5

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

bot/exts/utilities/bookmark.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,12 @@ def __init__(self, bot: Bot):
120120
name="Bookmark",
121121
callback=self._bookmark_context_menu_callback,
122122
)
123+
self.delete_bookmark_context_menu = discord.app_commands.ContextMenu(
124+
name="Delete bookmark",
125+
callback=self._delete_bookmark_context_menu_callback,
126+
)
123127
self.bot.tree.add_command(self.book_mark_context_menu, guild=discord.Object(bot.guild_id))
128+
self.bot.tree.add_command(self.delete_bookmark_context_menu)
124129

125130
@staticmethod
126131
def build_success_reply_embed(target_message: discord.Message) -> discord.Embed:
@@ -177,6 +182,18 @@ async def _bookmark_context_menu_callback(self, interaction: discord.Interaction
177182
bookmark_title_form = BookmarkForm(message=message)
178183
await interaction.response.send_modal(bookmark_title_form)
179184

185+
async def _delete_bookmark_context_menu_callback(
186+
self,
187+
interaction: discord.Interaction,
188+
message: discord.Message
189+
) -> None:
190+
"""The callback that will handle deleting a bookmark from a context menu command."""
191+
await self._delete_bookmark(message, interaction.channel)
192+
await interaction.response.send_message(
193+
embed=self._build_success_embed("Bookmark successfully deleted."),
194+
ephemeral=True,
195+
)
196+
180197
@commands.group(name="bookmark", aliases=("bm", "pin"), invoke_without_command=True)
181198
@commands.guild_only()
182199
@whitelist_override(roles=(Roles.everyone,))
@@ -235,12 +252,23 @@ async def delete_bookmark(
235252
The command invocation must be a reply to the message that is to be deleted.
236253
"""
237254
target_message: discord.Message | None = getattr(ctx.message.reference, "resolved", None)
255+
await self._delete_bookmark(target_message, ctx.channel)
256+
await ctx.send(embed=self._build_success_embed("Bookmark successfully deleted."), delete_after=10)
257+
258+
@staticmethod
259+
def _build_success_embed(message: str) -> discord.Embed:
260+
return discord.Embed(
261+
description=message,
262+
colour=Colours.soft_green
263+
)
264+
265+
async def _delete_bookmark(self, target_message: discord.Message | None, channel: discord.abc.Messageable) -> None:
266+
"""Delete a bookmark."""
238267
if target_message is None:
239268
raise commands.UserInputError("You must reply to the message from Sir-Lancebot you wish to delete.")
240-
241-
if not isinstance(ctx.channel, discord.DMChannel):
269+
if not isinstance(channel, discord.DMChannel):
242270
raise commands.UserInputError("You can only run this command your own DMs!")
243-
if target_message.channel != ctx.channel:
271+
if target_message.channel.id != channel.id:
244272
raise commands.UserInputError("You can only delete messages in your own DMs!")
245273
if target_message.author != self.bot.user:
246274
raise commands.UserInputError("You can only delete messages sent by Sir Lancebot!")

0 commit comments

Comments
 (0)