From 362e4888a0320c43956ddc88151f7ef50f3a7fff Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Fri, 22 Nov 2024 17:07:11 +0100 Subject: [PATCH 1/2] Add command for pinning/unpinning messages --- src/sr/discord_bot/bot.py | 3 ++ src/sr/discord_bot/commands/pin.py | 51 ++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 src/sr/discord_bot/commands/pin.py diff --git a/src/sr/discord_bot/bot.py b/src/sr/discord_bot/bot.py index 1bd2e1f..74325b5 100644 --- a/src/sr/discord_bot/bot.py +++ b/src/sr/discord_bot/bot.py @@ -32,6 +32,7 @@ repair_permissions, create_team_channel, ) +from sr.discord_bot.commands.pin import pin_message, unpin_message from sr.discord_bot.commands.stats import ( Stats, post_stats, @@ -87,6 +88,8 @@ def __init__( self.tree.add_command(stats, guild=self.guild) self.tree.add_command(join, guild=self.guild) self.tree.add_command(logs, guild=self.guild) + self.tree.add_command(pin_message, guild=self.guild) + self.tree.add_command(unpin_message, guild=self.guild) self.load_passwords() load_subscribed_messages(self) diff --git a/src/sr/discord_bot/commands/pin.py b/src/sr/discord_bot/commands/pin.py new file mode 100644 index 0000000..5fa2ba0 --- /dev/null +++ b/src/sr/discord_bot/commands/pin.py @@ -0,0 +1,51 @@ +from typing import cast, TYPE_CHECKING + +import discord +from discord import app_commands + +if TYPE_CHECKING: + from sr.discord_bot.bot import BotClient + +@app_commands.command( + name='pin', + description='Pin a message to the channel' +) +@app_commands.describe( + message_url='Link of the message to pin', +) +@app_commands.rename(message_url='message') +async def pin_message( + interaction: discord.interactions.Interaction["BotClient"], + message_url: str, +) -> None: + """Pin a message to the channel.""" + channel = cast(discord.TextChannel, interaction.channel) + message_id = int(message_url.split('/')[-1]) + message = await channel.fetch_message(message_id) + if message is None: + return + reason = "Pinned by " + interaction.user.mention + await message.pin(reason=reason) + await interaction.response.send_message(f"_{reason}_") + +@app_commands.command( + name='unpin', + description='Unpin a message from the channel' +) +@app_commands.describe( + message_url='Link of the message to unpin', +) +@app_commands.rename(message_url='message') +async def unpin_message( + interaction: discord.interactions.Interaction["BotClient"], + message_url: str, +) -> None: + """Unpin a message from the channel.""" + channel = cast(discord.TextChannel, interaction.channel) + message_id = int(message_url.split('/')[-1]) + message = await channel.fetch_message(message_id) + if message is None: + return + reason = "Unpinned by " + interaction.user.mention + await message.unpin(reason=reason) + await interaction.response.send_message(f"_{reason}_") From 708e51bda53174f0cf9c93999b5385f9064411b4 Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Sun, 15 Dec 2024 20:46:40 +0000 Subject: [PATCH 2/2] Fix lint errors --- src/sr/discord_bot/bot.py | 2 +- src/sr/discord_bot/commands/pin.py | 14 ++++++-------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/sr/discord_bot/bot.py b/src/sr/discord_bot/bot.py index 74325b5..6917ecc 100644 --- a/src/sr/discord_bot/bot.py +++ b/src/sr/discord_bot/bot.py @@ -21,6 +21,7 @@ ANNOUNCE_CHANNEL_NAME, WELCOME_CATEGORY_NAME, ) +from sr.discord_bot.commands.pin import pin_message, unpin_message from sr.discord_bot.commands.join import join from sr.discord_bot.commands.logs import logs from sr.discord_bot.commands.team import ( @@ -32,7 +33,6 @@ repair_permissions, create_team_channel, ) -from sr.discord_bot.commands.pin import pin_message, unpin_message from sr.discord_bot.commands.stats import ( Stats, post_stats, diff --git a/src/sr/discord_bot/commands/pin.py b/src/sr/discord_bot/commands/pin.py index 5fa2ba0..ee752ce 100644 --- a/src/sr/discord_bot/commands/pin.py +++ b/src/sr/discord_bot/commands/pin.py @@ -6,9 +6,10 @@ if TYPE_CHECKING: from sr.discord_bot.bot import BotClient -@app_commands.command( + +@app_commands.command( # type:ignore[arg-type] name='pin', - description='Pin a message to the channel' + description='Pin a message to the channel', ) @app_commands.describe( message_url='Link of the message to pin', @@ -22,15 +23,14 @@ async def pin_message( channel = cast(discord.TextChannel, interaction.channel) message_id = int(message_url.split('/')[-1]) message = await channel.fetch_message(message_id) - if message is None: - return reason = "Pinned by " + interaction.user.mention await message.pin(reason=reason) await interaction.response.send_message(f"_{reason}_") -@app_commands.command( + +@app_commands.command( # type:ignore[arg-type] name='unpin', - description='Unpin a message from the channel' + description='Unpin a message from the channel', ) @app_commands.describe( message_url='Link of the message to unpin', @@ -44,8 +44,6 @@ async def unpin_message( channel = cast(discord.TextChannel, interaction.channel) message_id = int(message_url.split('/')[-1]) message = await channel.fetch_message(message_id) - if message is None: - return reason = "Unpinned by " + interaction.user.mention await message.unpin(reason=reason) await interaction.response.send_message(f"_{reason}_")