feat: improve ping command

This commit is contained in:
Jozef Steinhübl 2024-05-11 19:36:18 +02:00
parent 78a393130a
commit 109398beea
No known key found for this signature in database
GPG key ID: E6BC90C91973B08F
6 changed files with 135 additions and 3 deletions

BIN
bun.lockb

Binary file not shown.

22
globals.d.ts vendored
View file

@ -3,5 +3,27 @@ declare module "bun" {
DISCORD_BOT_TOKEN: string; DISCORD_BOT_TOKEN: string;
BUN_ONLY_CHANNEL_ID: string; BUN_ONLY_CHANNEL_ID: string;
MESSAGE_PREFIX: 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<WolframResponse>;
}
interface WolframResponse {
pods: WolframPod[];
}
interface WolframPod {
id: string;
subpods: WolframSubpod[];
}
interface WolframSubpod {
plaintext: string;
} }
} }

View file

@ -15,6 +15,7 @@
"@lilybird/jsx": "0.2.0", "@lilybird/jsx": "0.2.0",
"@lilybird/transformers": "^0.2.0", "@lilybird/transformers": "^0.2.0",
"@paperdave/logger": "^3.0.1", "@paperdave/logger": "^3.0.1",
"@wolfram-alpha/wolfram-alpha-api": "^23.1004.144821-RELEASE",
"algoliasearch": "^4.23.2", "algoliasearch": "^4.23.2",
"bun-tracestrings": "github:oven-sh/bun.report", "bun-tracestrings": "github:oven-sh/bun.report",
"gray-matter": "^4.0.3", "gray-matter": "^4.0.3",

View file

@ -1,5 +1,6 @@
import { ApplicationCommand as JSXApplicationCommand } from "@lilybird/jsx"; import { ApplicationCommand as JSXApplicationCommand } from "@lilybird/jsx";
import { ApplicationCommand } from "@lilybird/handlers"; import { ApplicationCommand } from "@lilybird/handlers";
import { wolframApiClient } from "../constants.ts";
export default { export default {
post: "GLOBAL", post: "GLOBAL",
@ -9,8 +10,60 @@ export default {
const { ws, rest } = await interaction.client.ping(); const { ws, rest } = await interaction.client.ping();
const wsClosedForm = await possibleClosedForm(ws);
const restClosedForm = await possibleClosedForm(rest);
await interaction.editReply({ 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; } satisfies ApplicationCommand;
async function possibleClosedForm(
value: number | string
): Promise<string | number> {
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;
}
}

View file

@ -1,6 +1,7 @@
import { spawnSync } from "bun"; import WolframAlphaAPI from "@wolfram-alpha/wolfram-alpha-api";
import { dependencies } from "../package.json"; import { dependencies } from "../package.json";
import { sliceIfStartsWith } from "./util.ts"; import { sliceIfStartsWith } from "./util.ts";
import { spawnSync } from "bun";
export const COMMIT_HASH = spawnSync({ export const COMMIT_HASH = spawnSync({
cmd: ["git", "log", "--pretty=format:%h", "-n", "1"], 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_API = "https://developer.mozilla.org";
export const MDN_DISCORD_EMOJI = "mdn:1236028636826566758"; export const MDN_DISCORD_EMOJI = "mdn:1236028636826566758";
export const wolframApiClient = WolframAlphaAPI(process.env.WOLFRAM_ALPHA);

View file

@ -1,4 +1,5 @@
import { MessageCommand } from "@lilybird/handlers"; import { MessageCommand } from "@lilybird/handlers";
import { wolframApiClient } from "../constants.ts";
export default { export default {
name: "ping", name: "ping",
@ -9,8 +10,60 @@ export default {
const { ws, rest } = await message.client.ping(); const { ws, rest } = await message.client.ping();
const wsClosedForm = await possibleClosedForm(ws);
const restClosedForm = await possibleClosedForm(rest);
await newMessage.edit({ 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; } satisfies MessageCommand;
async function possibleClosedForm(
value: number | string
): Promise<string | number> {
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;
}
}