Heimdallr/heimdallr/commands/quote.py

99 lines
2.9 KiB
Python

import re
import logging
from naff import (
Extension,
slash_command,
slash_option,
InteractionContext,
OptionTypes,
Client,
)
from naff.models.discord.enums import ButtonStyles, Permissions
from naff.models.discord.embed import Embed, EmbedAuthor, EmbedField
from naff.models.discord.components import ActionRow, Button
class QuoteExtension(Extension):
def __init__(self, client: Client):
self.client = client
@slash_command(name="quote", description="Quote a message", dm_permission=False)
@slash_option(
name="url",
description="Message URL",
required=True,
opt_type=OptionTypes.STRING,
)
async def quote_command(self, ctx: InteractionContext, url: str):
url_match = re.match(
r"https?://discord(?:app)?\.com/channels/(\d+)/(\d+)/(\d+)", url
)
if url_match is None:
ctx.ephemeral = True
await ctx.send("Invalid URL")
return
_, channel_id, message_id = url_match.groups()
chan = await self.client.fetch_channel(channel_id)
msg = await chan.fetch_message(message_id)
if chan is None or msg is None:
await ctx.send("Failed to get message", ephemeral=True)
print(chan)
print(msg)
return
if not (
ctx.author.channel_permissions(chan)
& (Permissions.VIEW_CHANNEL | Permissions.READ_MESSAGE_HISTORY)
) == (Permissions.VIEW_CHANNEL | Permissions.READ_MESSAGE_HISTORY):
ctx.ephemeral = True
await ctx.send(
"You don't have permission to view this message", ephemeral=True
)
return
author_embed = EmbedAuthor(
name=msg.author.display_name, icon_url=msg.author.display_avatar.as_url()
)
embed = Embed(
# title=msg.author.display_name,
author=author_embed,
description=msg.content,
color=0x00FF00,
timestamp=msg.created_at,
url=url,
)
if len(msg.attachments) == 1 and msg.attachments[0].width is not None:
embed.set_image(url=msg.attachments[0].url)
elif len(msg.attachments) > 1:
field = EmbedField(
name="**Attachments**",
value="\n".join(
[f"• [{a.filename}]({a.url})" for a in msg.attachments]
),
)
embed.fields.append(field) # pylint: disable=E1101
# pylint: disable=E1123
components: list[ActionRow] = [
ActionRow(
Button(
style=ButtonStyles.LINK,
emoji="🔗",
url=url,
)
)
]
await ctx.send(embed=embed, components=components)
def setup(bot):
QuoteExtension(bot)
logging.info("Quote extension loaded")