Skip to content

Commit

Permalink
make deleting a bookmark possible through a context menu command
Browse files Browse the repository at this point in the history
  • Loading branch information
shtlrs committed Mar 20, 2024
1 parent d418e9f commit ff711e5
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions bot/exts/utilities/bookmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,12 @@ def __init__(self, bot: Bot):
name="Bookmark",
callback=self._bookmark_context_menu_callback,
)
self.delete_bookmark_context_menu = discord.app_commands.ContextMenu(
name="Delete bookmark",
callback=self._delete_bookmark_context_menu_callback,
)
self.bot.tree.add_command(self.book_mark_context_menu, guild=discord.Object(bot.guild_id))
self.bot.tree.add_command(self.delete_bookmark_context_menu)

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

async def _delete_bookmark_context_menu_callback(
self,
interaction: discord.Interaction,
message: discord.Message
) -> None:
"""The callback that will handle deleting a bookmark from a context menu command."""
await self._delete_bookmark(message, interaction.channel)
await interaction.response.send_message(
embed=self._build_success_embed("Bookmark successfully deleted."),
ephemeral=True,
)

@commands.group(name="bookmark", aliases=("bm", "pin"), invoke_without_command=True)
@commands.guild_only()
@whitelist_override(roles=(Roles.everyone,))
Expand Down Expand Up @@ -235,12 +252,23 @@ async def delete_bookmark(
The command invocation must be a reply to the message that is to be deleted.
"""
target_message: discord.Message | None = getattr(ctx.message.reference, "resolved", None)
await self._delete_bookmark(target_message, ctx.channel)
await ctx.send(embed=self._build_success_embed("Bookmark successfully deleted."), delete_after=10)

@staticmethod
def _build_success_embed(message: str) -> discord.Embed:
return discord.Embed(
description=message,
colour=Colours.soft_green
)

async def _delete_bookmark(self, target_message: discord.Message | None, channel: discord.abc.Messageable) -> None:
"""Delete a bookmark."""
if target_message is None:
raise commands.UserInputError("You must reply to the message from Sir-Lancebot you wish to delete.")

if not isinstance(ctx.channel, discord.DMChannel):
if not isinstance(channel, discord.DMChannel):
raise commands.UserInputError("You can only run this command your own DMs!")
if target_message.channel != ctx.channel:
if target_message.channel.id != channel.id:
raise commands.UserInputError("You can only delete messages in your own DMs!")
if target_message.author != self.bot.user:
raise commands.UserInputError("You can only delete messages sent by Sir Lancebot!")
Expand Down

0 comments on commit ff711e5

Please sign in to comment.