From df1f94f3a42eff40d2f1a05cd2e7c5053431a32e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl?= Date: Sat, 11 May 2024 20:20:44 +0200 Subject: [PATCH] feat(ping): improve sorting & use promise.all --- src/commands/ping.tsx | 52 ++++---------------------------- src/message-commands/ping.ts | 52 ++++---------------------------- src/util.ts | 57 +++++++++++++++++++++++++++++++++++- 3 files changed, 66 insertions(+), 95 deletions(-) diff --git a/src/commands/ping.tsx b/src/commands/ping.tsx index 8741790..bfcb5ab 100644 --- a/src/commands/ping.tsx +++ b/src/commands/ping.tsx @@ -1,6 +1,6 @@ import { ApplicationCommand as JSXApplicationCommand } from "@lilybird/jsx"; import { ApplicationCommand } from "@lilybird/handlers"; -import { wolframApiClient } from "../constants.ts"; +import { possibleClosedForm } from "../util.ts"; export default { post: "GLOBAL", @@ -10,8 +10,10 @@ export default { const { ws, rest } = await interaction.client.ping(); - const wsClosedForm = await possibleClosedForm(ws); - const restClosedForm = await possibleClosedForm(rest); + const [wsClosedForm, restClosedForm] = await Promise.all([ + possibleClosedForm(ws), + possibleClosedForm(rest), + ]); await interaction.editReply({ content: [ @@ -22,47 +24,3 @@ export default { }); }, } satisfies ApplicationCommand; - -async function possibleClosedForm( - value: number | string -): Promise { - try { - const res = await wolframApiClient.getFull(value.toString()); - const pod = res?.pods?.find((p) => p.id === "PossibleClosedForm"); - if (!pod) { - return value; - } - - pod.subpods.sort((a, b) => { - if ( - a.plaintext.includes("log") || - a.plaintext.includes("e") || - a.plaintext.includes("π") - ) { - return -1; - } - - if ( - b.plaintext.includes("log") || - b.plaintext.includes("e") || - b.plaintext.includes("π") - ) { - return 1; - } - - return Math.random() - 0.5; - }); - - const randomSubpod = pod.subpods[0]; - - const text = randomSubpod.plaintext; - - if (text.includes("=") && !text.includes("near") && !text.includes("≈")) { - return text.split("=")[0].trim(); - } - - return randomSubpod.plaintext.split("≈")[0].trim(); - } catch { - return value; - } -} diff --git a/src/message-commands/ping.ts b/src/message-commands/ping.ts index bb3ae40..ff54f49 100644 --- a/src/message-commands/ping.ts +++ b/src/message-commands/ping.ts @@ -1,5 +1,5 @@ import { MessageCommand } from "@lilybird/handlers"; -import { wolframApiClient } from "../constants.ts"; +import { possibleClosedForm } from "../util.ts"; export default { name: "ping", @@ -10,8 +10,10 @@ export default { const { ws, rest } = await message.client.ping(); - const wsClosedForm = await possibleClosedForm(ws); - const restClosedForm = await possibleClosedForm(rest); + const [wsClosedForm, restClosedForm] = await Promise.all([ + possibleClosedForm(ws), + possibleClosedForm(rest), + ]); await newMessage.edit({ content: [ @@ -22,47 +24,3 @@ export default { }); }, } satisfies MessageCommand; - -async function possibleClosedForm( - value: number | string -): Promise { - try { - const res = await wolframApiClient.getFull(value.toString()); - const pod = res?.pods?.find((p) => p.id === "PossibleClosedForm"); - if (!pod) { - return value; - } - - pod.subpods.sort((a, b) => { - if ( - a.plaintext.includes("log") || - a.plaintext.includes("e") || - a.plaintext.includes("π") - ) { - return -1; - } - - if ( - b.plaintext.includes("log") || - b.plaintext.includes("e") || - b.plaintext.includes("π") - ) { - return 1; - } - - return Math.random() - 0.5; - }); - - const randomSubpod = pod.subpods[0]; - - const text = randomSubpod.plaintext; - - if (text.includes("=") && !text.includes("near") && !text.includes("≈")) { - return text.split("=")[0].trim(); - } - - return randomSubpod.plaintext.split("≈")[0].trim(); - } catch { - return value; - } -} diff --git a/src/util.ts b/src/util.ts index aa34613..d57303c 100644 --- a/src/util.ts +++ b/src/util.ts @@ -1,5 +1,5 @@ import { GuildMember } from "@lilybird/transformers"; -import { BUN_EMOJIS } from "./constants.ts"; +import { BUN_EMOJIS, wolframApiClient } from "./constants.ts"; import { parseAndRemap, formatMarkdown } from "bun-tracestrings"; const URL_REGEX = /\(\s*(https?:\/\/[^\s\[\]]+)\s*\)/gi; @@ -73,3 +73,58 @@ export async function getBunReportDetailsInMarkdown( return content; } + +export async function possibleClosedForm( + value: number | string +): Promise { + try { + const res = await wolframApiClient.getFull(value.toString()); + const pod = res?.pods?.find((p) => p.id === "PossibleClosedForm"); + if (!pod) { + return value; + } + + pod.subpods.sort((a, b) => { + const aContainsSpecial = + a.plaintext.includes("log") || + a.plaintext.includes("e") || + a.plaintext.includes("π"); + const bContainsSpecial = + b.plaintext.includes("log") || + b.plaintext.includes("e") || + b.plaintext.includes("π"); + const aContainsNear = a.plaintext.includes("near"); + const bContainsNear = b.plaintext.includes("near"); + + if (aContainsSpecial && !aContainsNear) { + return -1; + } + + if (bContainsSpecial && !bContainsNear) { + return 1; + } + + if (aContainsSpecial && aContainsNear) { + return -1; + } + + if (bContainsSpecial && bContainsNear) { + return 1; + } + + return Math.random() - 0.5; + }); + + const randomSubpod = pod.subpods[0]; + + const text = randomSubpod.plaintext; + + if (text.includes("=") && !text.includes("near") && !text.includes("≈")) { + return text.split("=")[0].trim(); + } + + return randomSubpod.plaintext.split("≈")[0].trim(); + } catch { + return value; + } +}