From 0a0c0caa8eca9aab4f6e49cceca3e8e2d833364e Mon Sep 17 00:00:00 2001 From: Ovler Date: Thu, 13 Jun 2024 17:12:56 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E2=9C=A8=20wip:=20Add=20support=20for=20ha?= =?UTF-8?q?ndling=20Bilibili=20links?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/server/index.ts | 59 ++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 27 deletions(-) diff --git a/src/lib/server/index.ts b/src/lib/server/index.ts index 3c548e5..f695ad4 100644 --- a/src/lib/server/index.ts +++ b/src/lib/server/index.ts @@ -35,31 +35,7 @@ bot.command("help", (ctx) => ) ); -bot.command("bili", async (ctx) => { - // if (ctx.chat.id !== -1001773704746) { - // return; - // } - if (!ctx.message) { - return; - } - let text = ctx.message.text; - // @ts-ignore - console.info(ctx.message.reply_to_message); - // @ts-ignore - if (ctx.message.reply_to_message && ctx.message.reply_to_message["text"]) { - // @ts-ignore - text = ctx.message.reply_to_message["text"] + "\n" + text; - } - // console.log(ctx.message); - const urls: string[] = []; - - ctx.message.entities?.forEach((entity) => { - if (entity.type === "text_link" && entity.url) { - urls.push(entity.url); - } - }); - - text = urls.join(" ") + text; +const handleBiliLink = async (ctx, text) => { text = await resolveB23(text); const matches = /BV[a-zA-Z0-9]+/i.exec(text); if (!matches) { @@ -91,8 +67,7 @@ bot.command("bili", async (ctx) => { try { const url = result.unwrap().toString(); await ctx.reply( - `\u{1F389} Archive of ${bv} was done, item uploaded to -${url}`, + `\u{1F389} Archive of ${bv} was done, item uploaded to\n${url}`, { reply_to_message_id: ctx.message.message_id, reply_markup: { @@ -130,6 +105,36 @@ ${url}`, return; } })(); +}; + +bot.command("bili", async (ctx) => { + // if (ctx.chat.id !== -1001773704746) { + // return; + // } + if (!ctx.message) { + return; + } + let text = ctx.message.text; + // @ts-ignore + console.info(ctx.message.reply_to_message); + // @ts-ignore + if (ctx.message.reply_to_message && ctx.message.reply_to_message.text) { + // @ts-ignore + text = ctx.message.reply_to_message.text + "\n" + text; + } + // console.log(ctx.message); + await handleBiliLink(ctx, text); +}); + +bot.hears(/https:\/\/b23.tv\/\S+|https:\/\/www.bilibili.com\/video\/\S+/i, async (ctx) => { + if (!ctx.message) { + return; + } + let text = ctx.message.text; + if (ctx.message.reply_to_message && ctx.message.reply_to_message.text) { + text = ctx.message.reply_to_message.text + "\n" + text; + } + await handleBiliLink(ctx, text); }); bot.command("bilist", async (ctx) => { From 103d2e0472783fc6e8f185e86f8c432e53a20608 Mon Sep 17 00:00:00 2001 From: Ovler Date: Thu, 13 Jun 2024 18:47:15 +0800 Subject: [PATCH 2/3] chore: enable direct AV number or BVID --- src/lib/server/index.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/lib/server/index.ts b/src/lib/server/index.ts index f695ad4..936a3d6 100644 --- a/src/lib/server/index.ts +++ b/src/lib/server/index.ts @@ -125,8 +125,7 @@ bot.command("bili", async (ctx) => { // console.log(ctx.message); await handleBiliLink(ctx, text); }); - -bot.hears(/https:\/\/b23.tv\/\S+|https:\/\/www.bilibili.com\/video\/\S+/i, async (ctx) => { +bot.hears(/(BV[a-zA-Z0-9]+)|(av\d+)|https:\/\/b23.tv\/\S+|https:\/\/www.bilibili.com\/video\/\S+/i, async (ctx) => { if (!ctx.message) { return; } From ceaa022899ec4fed7877a9519be5c6b9790937b8 Mon Sep 17 00:00:00 2001 From: Ovler Date: Thu, 13 Jun 2024 18:53:10 +0800 Subject: [PATCH 3/3] fix: Incomplete regular expression for hostnames The last regular expression has an unescaped '.' before 'bilibili.com', so it might match more hosts than expected. Thanks https://github.com/apps/github-advanced-security --- src/lib/server/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/server/index.ts b/src/lib/server/index.ts index 936a3d6..e587928 100644 --- a/src/lib/server/index.ts +++ b/src/lib/server/index.ts @@ -125,7 +125,7 @@ bot.command("bili", async (ctx) => { // console.log(ctx.message); await handleBiliLink(ctx, text); }); -bot.hears(/(BV[a-zA-Z0-9]+)|(av\d+)|https:\/\/b23.tv\/\S+|https:\/\/www.bilibili.com\/video\/\S+/i, async (ctx) => { +bot.hears(/(BV[a-zA-Z0-9]+)|(av\d+)|https:\/\/b23\.tv\/\S+|https:\/\/www\.bilibili\.com\/video\/\S+/i, async (ctx) => { if (!ctx.message) { return; }