Skip to content
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

Add command for pinning/unpinning messages #43

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions src/sr/discord_bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)

Expand Down
51 changes: 51 additions & 0 deletions src/sr/discord_bot/commands/pin.py
Original file line number Diff line number Diff line change
@@ -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])
Copy link
Member

Choose a reason for hiding this comment

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

What sort of error handling do we want here if the input doesn't match the expected format? (i.e: do we want to handle the resulting ValueError?)
Related: should we e.g: ensure that the given url is actually to Discord?

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}_")
Loading