2022-07-21 21:27:47 +02:00
|
|
|
import re
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from naff import (
|
|
|
|
Extension,
|
|
|
|
slash_command,
|
|
|
|
slash_option,
|
|
|
|
InteractionContext,
|
|
|
|
OptionTypes,
|
|
|
|
Client,
|
|
|
|
)
|
2022-07-21 20:50:23 +02:00
|
|
|
from naff.models.discord.enums import ButtonStyles, Permissions
|
2022-07-21 21:27:47 +02:00
|
|
|
from naff.models.discord.embed import Embed, EmbedAuthor, EmbedField
|
2022-07-21 20:50:23 +02:00
|
|
|
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)
|
2022-07-21 21:27:47 +02:00
|
|
|
@slash_option(
|
|
|
|
name="url",
|
|
|
|
description="Message URL",
|
|
|
|
required=True,
|
|
|
|
opt_type=OptionTypes.STRING,
|
|
|
|
)
|
2022-07-21 20:50:23 +02:00
|
|
|
async def quote_command(self, ctx: InteractionContext, url: str):
|
|
|
|
|
2022-07-21 21:27:47 +02:00
|
|
|
url_match = re.match(
|
|
|
|
r"https?://discord(?:app)?\.com/channels/(\d+)/(\d+)/(\d+)", url
|
|
|
|
)
|
2022-07-21 20:50:23 +02:00
|
|
|
if url_match is None:
|
|
|
|
ctx.ephemeral = True
|
|
|
|
await ctx.send("Invalid URL")
|
|
|
|
return
|
2022-07-21 21:27:47 +02:00
|
|
|
|
|
|
|
_, channel_id, message_id = url_match.groups()
|
|
|
|
|
2022-07-21 20:50:23 +02:00
|
|
|
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
|
|
|
|
|
2022-07-21 21:27:47 +02:00
|
|
|
if not (
|
|
|
|
ctx.author.channel_permissions(chan)
|
|
|
|
& (Permissions.VIEW_CHANNEL | Permissions.READ_MESSAGE_HISTORY)
|
|
|
|
) == (Permissions.VIEW_CHANNEL | Permissions.READ_MESSAGE_HISTORY):
|
2022-07-21 20:50:23 +02:00
|
|
|
|
|
|
|
ctx.ephemeral = True
|
2022-07-21 21:27:47 +02:00
|
|
|
await ctx.send(
|
|
|
|
"You don't have permission to view this message", ephemeral=True
|
|
|
|
)
|
2022-07-21 20:50:23 +02:00
|
|
|
return
|
|
|
|
|
2022-07-21 21:27:47 +02:00
|
|
|
author_embed = EmbedAuthor(
|
|
|
|
name=msg.author.display_name, icon_url=msg.author.display_avatar.as_url()
|
|
|
|
)
|
2022-07-21 20:50:23 +02:00
|
|
|
embed = Embed(
|
2022-07-21 21:27:47 +02:00
|
|
|
# title=msg.author.display_name,
|
2022-07-21 20:50:23 +02:00
|
|
|
author=author_embed,
|
2022-07-21 21:27:47 +02:00
|
|
|
description=msg.content,
|
|
|
|
color=0x00FF00,
|
|
|
|
timestamp=msg.created_at,
|
|
|
|
url=url,
|
|
|
|
)
|
2022-07-21 20:50:23 +02:00
|
|
|
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:
|
2022-07-21 21:27:47 +02:00
|
|
|
field = EmbedField(
|
|
|
|
name="**Attachments**",
|
|
|
|
value="\n".join(
|
|
|
|
[f"• [{a.filename}]({a.url})" for a in msg.attachments]
|
|
|
|
),
|
|
|
|
)
|
|
|
|
embed.fields.append(field) # pylint: disable=E1101
|
2022-07-21 20:50:23 +02:00
|
|
|
|
2022-07-21 21:27:47 +02:00
|
|
|
# pylint: disable=E1123
|
2022-07-21 20:50:23 +02:00
|
|
|
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")
|