feat: bun.report reports

This commit is contained in:
Jozef Steinhübl 2024-05-01 13:08:13 +02:00
parent e26941c056
commit c858d66a3f
No known key found for this signature in database
GPG key ID: E6BC90C91973B08F
4 changed files with 43 additions and 1 deletions

BIN
bun.lockb

Binary file not shown.

View file

@ -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"
}

View file

@ -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\/(?<repo>[a-zA-Z0-9-_]+\/[A-Za-z0-9_.-]+)\/blob\/(?<path>.+?)#L(?<first_line_number>\d+)[-~]?L?(?<second_line_number>\d*)/i;
const TWITTER_TWEET_URL_REGEX =
/https:\/\/(?:www\.)?(?:twitter|x)\.com\/(?<user>[a-zA-Z0-9-_]+)\/status\/(?<id>\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<void> {
});
}
async function handleBunReportLink(message: Message): Promise<void> {
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;

View file

@ -1,5 +1,6 @@
import { GuildMember } from "@lilybird/transformers";
import { BUN_EMOJIS } from "./constants.ts";
import { parse, formatMarkdown } from "bun-tracestrings";
export function safeSlice<T extends string | Array<any>>(
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<string> {
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,
});
}