diff --git a/bun.lockb b/bun.lockb index 670577f..9da7931 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/globals.d.ts b/globals.d.ts index 857af7f..0fd6b45 100644 --- a/globals.d.ts +++ b/globals.d.ts @@ -3,5 +3,27 @@ declare module "bun" { DISCORD_BOT_TOKEN: string; BUN_ONLY_CHANNEL_ID: string; MESSAGE_PREFIX: string; + WOLFRAM_ALPHA: string; + } +} + +declare module "@wolfram-alpha/wolfram-alpha-api" { + export default function WolframAlphaAPI(appId: string): WolframAPI; + + interface WolframAPI { + getFull(input: string): Promise; + } + + interface WolframResponse { + pods: WolframPod[]; + } + + interface WolframPod { + id: string; + subpods: WolframSubpod[]; + } + + interface WolframSubpod { + plaintext: string; } } diff --git a/package.json b/package.json index 6ceaf47..f348d58 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "@lilybird/jsx": "0.2.0", "@lilybird/transformers": "^0.2.0", "@paperdave/logger": "^3.0.1", + "@wolfram-alpha/wolfram-alpha-api": "^23.1004.144821-RELEASE", "algoliasearch": "^4.23.2", "bun-tracestrings": "github:oven-sh/bun.report", "gray-matter": "^4.0.3", diff --git a/src/commands/ping.tsx b/src/commands/ping.tsx index 7bcdc0a..b6db28e 100644 --- a/src/commands/ping.tsx +++ b/src/commands/ping.tsx @@ -1,5 +1,6 @@ import { ApplicationCommand as JSXApplicationCommand } from "@lilybird/jsx"; import { ApplicationCommand } from "@lilybird/handlers"; +import { wolframApiClient } from "../constants.ts"; export default { post: "GLOBAL", @@ -9,8 +10,60 @@ export default { const { ws, rest } = await interaction.client.ping(); + const wsClosedForm = await possibleClosedForm(ws); + const restClosedForm = await possibleClosedForm(rest); + await interaction.editReply({ - content: `🏓 WebSocket: \`${ws}ms\` | Rest: \`${rest}ms\``, + content: [ + `🏓`, + `WebSocket: \`${wsClosedForm} ms\``, + `Rest: \`${restClosedForm} ms\``, + `||(\`${ws} ms\`, \`${rest} ms\`)||`, + ].join("\n"), }); }, } 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/constants.ts b/src/constants.ts index b71eed9..e835042 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,6 +1,7 @@ -import { spawnSync } from "bun"; +import WolframAlphaAPI from "@wolfram-alpha/wolfram-alpha-api"; import { dependencies } from "../package.json"; import { sliceIfStartsWith } from "./util.ts"; +import { spawnSync } from "bun"; export const COMMIT_HASH = spawnSync({ cmd: ["git", "log", "--pretty=format:%h", "-n", "1"], @@ -50,3 +51,5 @@ export const BUN_EMOJIS = [ export const MDN_API = "https://developer.mozilla.org"; export const MDN_DISCORD_EMOJI = "mdn:1236028636826566758"; + +export const wolframApiClient = WolframAlphaAPI(process.env.WOLFRAM_ALPHA); diff --git a/src/message-commands/ping.ts b/src/message-commands/ping.ts index 1cafb5a..7225372 100644 --- a/src/message-commands/ping.ts +++ b/src/message-commands/ping.ts @@ -1,4 +1,5 @@ import { MessageCommand } from "@lilybird/handlers"; +import { wolframApiClient } from "../constants.ts"; export default { name: "ping", @@ -9,8 +10,60 @@ export default { const { ws, rest } = await message.client.ping(); + const wsClosedForm = await possibleClosedForm(ws); + const restClosedForm = await possibleClosedForm(rest); + await newMessage.edit({ - content: `🏓 WebSocket: \`${ws}ms\` | Rest: \`${rest}ms\``, + content: [ + `🏓`, + `WebSocket: \`${wsClosedForm} ms\``, + `Rest: \`${restClosedForm} ms\``, + `||(\`${ws} ms\`, \`${rest} ms\`)||`, + ].join("\n"), }); }, } 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; + } +}