mirror of
https://github.com/xHyroM/bun-discord-bot.git
synced 2024-11-21 22:21:05 +01:00
feat(ping): improve sorting & use promise.all
This commit is contained in:
parent
1f7ac53e72
commit
df1f94f3a4
3 changed files with 66 additions and 95 deletions
|
@ -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<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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<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;
|
||||
}
|
||||
}
|
||||
|
|
57
src/util.ts
57
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<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) => {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue