Skip to content

Make deleting a bookmark a context menu command #1197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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!")
Copy link
Preview

Copilot AI Apr 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update the error message to include 'in' for clarity, e.g., 'You can only run this command in your own DMs!'.

Suggested change
raise commands.UserInputError("You can only run this command your own DMs!")
raise commands.UserInputError("You can only run this command in your own DMs!")

Copilot uses AI. Check for mistakes.

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