diff --git a/bun.lockb b/bun.lockb index c248908..4a46b8d 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index db274fb..d7519d1 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "@lilybird/transformers": "0.2.0-alpha.1", "@paperdave/logger": "^3.0.1", "algoliasearch": "^4.23.2", + "bun-tracestrings": "github:oven-sh/bun.report", "gray-matter": "^4.0.3", "lilybird": "^0.6.0-alpha.16" } diff --git a/src/listeners/message_create.tsx b/src/listeners/message_create.tsx index f629326..3a548d5 100644 --- a/src/listeners/message_create.tsx +++ b/src/listeners/message_create.tsx @@ -3,12 +3,18 @@ import { Message } from "@lilybird/transformers"; import { ActionRow, Button } from "@lilybird/jsx"; import { extname, basename } from "node:path"; import { Event } from "@lilybird/handlers"; -import { getRandomBunEmoji, isBunOnlyLikeMessage, safeSlice } from "../util.ts"; +import { + getBunReportDetailsInMarkdown, + getRandomBunEmoji, + isBunOnlyLikeMessage, + safeSlice, +} from "../util.ts"; const GITHUB_LINE_URL_REGEX = /(?:https?:\/\/)?(?:www\.)?(?:github)\.com\/(?[a-zA-Z0-9-_]+\/[A-Za-z0-9_.-]+)\/blob\/(?.+?)#L(?\d+)[-~]?L?(?\d*)/i; const TWITTER_TWEET_URL_REGEX = /https:\/\/(?:www\.)?(?:twitter|x)\.com\/(?[a-zA-Z0-9-_]+)\/status\/(?\d+)/i; +const BUN_REPORT_URL_REGEX = /(https:\/\/bun\.report\/\d+\.\d+(\.\d+)?\/\S+)/g; export default { event: "messageCreate", @@ -21,6 +27,7 @@ export default { function handleOthers(message: Message): void { handleGithubLink(message); + handleBunReportLink(message); //handleTwitterLink(message); // discord finnaly has embeds } @@ -106,6 +113,16 @@ async function handleGithubLink(message: Message): Promise { }); } +async function handleBunReportLink(message: Message): Promise { + if (!message.content) return; + + const match = message.content.match(BUN_REPORT_URL_REGEX); + if (!match?.[0]) return; + + const data = await getBunReportDetailsInMarkdown(match[0]); + message.reply(data); +} + function handleTwitterLink(message: Message): void { if (!message.content) return; diff --git a/src/util.ts b/src/util.ts index 97fc010..c2f0a77 100644 --- a/src/util.ts +++ b/src/util.ts @@ -1,5 +1,6 @@ import { GuildMember } from "@lilybird/transformers"; import { BUN_EMOJIS } from "./constants.ts"; +import { parse, formatMarkdown } from "bun-tracestrings"; export function safeSlice>( input: T, @@ -46,3 +47,26 @@ export function getRandomBunEmoji() { export function sliceIfStartsWith(input: string, startsWith: string) { return input.startsWith(startsWith) ? input.slice(startsWith.length) : input; } + +export async function getBunReportDetailsInMarkdown( + url: string +): Promise { + const parsed = await parse(url); + + const res = await fetch("https://bun.report/remap", { + method: "POST", + body: url, + }); + + if (!res.ok) { + return `Failed to get details from bun.report: ${res.statusText}`; + } + + const json = await res.json(); + console.log(json); + + return formatMarkdown({ + ...parsed, + ...json, + }); +}