Skip to content
Merged
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
12 changes: 12 additions & 0 deletions .changeset/fuzzy-bikes-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
"@embedly/builder": minor
"@embedly/parser": minor
"@embedly/bot": minor
"@embedly/api": minor
"@embedly/config": minor
"@embedly/logging": minor
"@embedly/platforms": minor
"@embedly/types": minor
---

add a spoiler flag and support masked links in messages
40 changes: 13 additions & 27 deletions apps/bot/src/commands/embed.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { treaty } from "@elysiajs/eden";
import type { App } from "@embedly/api";
import { Embed } from "@embedly/builder";
import { Embed, EmbedFlags } from "@embedly/builder";
import {
EMBEDLY_NO_LINK_IN_MESSAGE,
EMBEDLY_NO_VALID_LINK,
Expand All @@ -14,11 +14,7 @@ import {
} from "@embedly/parser";
import Platforms from "@embedly/platforms";
import { Command } from "@sapphire/framework";
import {
type APIInteractionResponseChannelMessageWithSource,
ApplicationCommandType,
InteractionResponseType
} from "discord.js";
import { ApplicationCommandType } from "discord.js";

const app = treaty<App>(process.env.EMBEDLY_API_DOMAIN!);

Expand Down Expand Up @@ -52,6 +48,11 @@ export class EmbedCommand extends Command {
.setName("media_only")
.setDescription("Display the media only")
)
.addBooleanOption((opt) =>
opt
.setName("spoiler")
.setDescription("Hide embed content behind spoiler")
)
)
.registerContextMenuCommand((command) =>
command
Expand All @@ -60,23 +61,12 @@ export class EmbedCommand extends Command {
);
}

isInteractionResponse(
data: Record<string, any>
): data is APIInteractionResponseChannelMessageWithSource {
return (
data.type &&
data.type === InteractionResponseType.ChannelMessageWithSource
);
}

async fetchEmbed(
interaction:
| Command.ChatInputCommandInteraction
| Command.ContextMenuCommandInteraction,
content: string,
flags?: {
MediaOnly: boolean;
}
flags?: Partial<Record<EmbedFlags, boolean>>
) {
const log_ctx = {
interaction_id: interaction.id,
Expand Down Expand Up @@ -122,15 +112,8 @@ export class EmbedCommand extends Command {

try {
const embed = Platforms[platform.type].createEmbed(data);

if (flags?.MediaOnly) {
return await interaction.editReply({
components: [{ type: 12, items: embed.media }],
flags: ["IsComponentsV2"]
});
}
return await interaction.editReply({
components: [Embed.getDiscordEmbed(embed)],
components: [Embed.getDiscordEmbed(embed, flags)],
flags: ["IsComponentsV2"],
allowedMentions: {
parse: [],
Expand Down Expand Up @@ -166,7 +149,10 @@ export class EmbedCommand extends Command {
) {
const url = interaction.options.getString("url", true);
this.fetchEmbed(interaction, url, {
MediaOnly: interaction.options.getBoolean("media_only") ?? false
[EmbedFlags.MediaOnly]:
interaction.options.getBoolean("media_only") ?? false,
[EmbedFlags.Spoiler]:
interaction.options.getBoolean("spoiler") ?? false
});
}
}
11 changes: 8 additions & 3 deletions apps/bot/src/listeners/messageCreate.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { treaty } from "@elysiajs/eden";
import type { App } from "@embedly/api";
import { Embed } from "@embedly/builder";
import { Embed, EmbedFlags } from "@embedly/builder";
import {
GENERIC_LINK_REGEX,
getPlatformFromURL,
hasLink
hasLink,
isSpoiler
} from "@embedly/parser";
import Platforms from "@embedly/platforms";
import { Events, Listener } from "@sapphire/framework";
Expand Down Expand Up @@ -55,7 +56,11 @@ export class MessageListener extends Listener<

const embed = Platforms[platform.type].createEmbed(data);
const msg = {
components: [Embed.getDiscordEmbed(embed)],
components: [
Embed.getDiscordEmbed(embed, {
[EmbedFlags.Spoiler]: isSpoiler(url, message.content)
})
],
flags: MessageFlags.IsComponentsV2,
allowedMentions: {
parse: [],
Expand Down
24 changes: 22 additions & 2 deletions packages/builder/src/Embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ export interface EmbedData extends BaseEmbedData {
reply?: BaseEmbedDataWithoutPlatform;
}

export enum EmbedFlags {
MediaOnly = "MediaOnly",
Spoiler = "Spoiler"
}

export class Embed implements EmbedData {
public platform!: EmbedlyPlatformType;
public name!: string;
Expand Down Expand Up @@ -68,9 +73,18 @@ export class Embed implements EmbedData {
this.reply = reply_tweet;
}

static getDiscordEmbed(embed: Embed) {
static getDiscordEmbed(
embed: Embed,
flags?: Partial<Record<EmbedFlags, boolean>>
) {
const media_only = flags?.[EmbedFlags.MediaOnly];
const hidden = flags?.[EmbedFlags.Spoiler];

const container = new ContainerBuilder();
container.setAccentColor(EmbedlyPlatformColors[embed.platform]);
if (hidden) {
container.setSpoiler(true);
}
const text_section = new SectionBuilder()
.addTextDisplayComponents((builder) =>
builder.setContent(
Expand All @@ -95,14 +109,20 @@ export class Embed implements EmbedData {
);
}

container.addSectionComponents(text_section);
if (!media_only) {
container.addSectionComponents(text_section);
}

if (embed.media) {
container.addMediaGalleryComponents((builder) =>
builder.addItems(embed.media!)
);
}

if (media_only) {
return container.toJSON();
}

if (embed.reply || embed.quote) {
container.addSeparatorComponents((builder) =>
builder.setDivider(true).setSpacing(SeparatorSpacingSize.Large)
Expand Down
5 changes: 5 additions & 0 deletions packages/parser/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
CBC_REGEX,
GENERIC_LINK_REGEX,
IG_REGEX,
SPOILER_LINK_REGEX,
THREADS_REGEX,
TIKTOK_REGEX_MAIN,
TWITTER_REGEX
Expand All @@ -12,6 +13,10 @@ export function hasLink(content: string) {
return GENERIC_LINK_REGEX.test(content);
}

export function isSpoiler(url: string, content: string) {
return SPOILER_LINK_REGEX(url).test(content);
}

export function getPlatformFromURL(
url: string
): null | { type: EmbedlyPlatformType } {
Expand Down
2 changes: 2 additions & 0 deletions packages/parser/src/regex.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export const GENERIC_LINK_REGEX =
/\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()[\]{};:'".,<>?«»“”‘’]))/i;
export const SPOILER_LINK_REGEX = (url: string) =>
RegExp(`||\\s?${url}\\s?||`);
export const TWITTER_REGEX =
/(?:twitter|x).com\/.*\/status(?:es)?\/(?<tweet_id>[^/?]+)/;
export const IG_REGEX =
Expand Down
3 changes: 2 additions & 1 deletion turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"$schema": "./node_modules/turbo/schema.json",
"tasks": {
"build": {
"dependsOn": ["^build"]
"dependsOn": ["^build"],
"outputs": ["dist/**"]
},
"lint": {},
"dev": {
Expand Down