mirror of
https://github.com/xHyroM/bun-discord-bot.git
synced 2024-11-22 06:31:06 +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 as JSXApplicationCommand } from "@lilybird/jsx";
|
||||||
import { ApplicationCommand } from "@lilybird/handlers";
|
import { ApplicationCommand } from "@lilybird/handlers";
|
||||||
import { wolframApiClient } from "../constants.ts";
|
import { possibleClosedForm } from "../util.ts";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
post: "GLOBAL",
|
post: "GLOBAL",
|
||||||
|
@ -10,8 +10,10 @@ export default {
|
||||||
|
|
||||||
const { ws, rest } = await interaction.client.ping();
|
const { ws, rest } = await interaction.client.ping();
|
||||||
|
|
||||||
const wsClosedForm = await possibleClosedForm(ws);
|
const [wsClosedForm, restClosedForm] = await Promise.all([
|
||||||
const restClosedForm = await possibleClosedForm(rest);
|
possibleClosedForm(ws),
|
||||||
|
possibleClosedForm(rest),
|
||||||
|
]);
|
||||||
|
|
||||||
await interaction.editReply({
|
await interaction.editReply({
|
||||||
content: [
|
content: [
|
||||||
|
@ -22,47 +24,3 @@ export default {
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
} 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { MessageCommand } from "@lilybird/handlers";
|
import { MessageCommand } from "@lilybird/handlers";
|
||||||
import { wolframApiClient } from "../constants.ts";
|
import { possibleClosedForm } from "../util.ts";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: "ping",
|
name: "ping",
|
||||||
|
@ -10,8 +10,10 @@ export default {
|
||||||
|
|
||||||
const { ws, rest } = await message.client.ping();
|
const { ws, rest } = await message.client.ping();
|
||||||
|
|
||||||
const wsClosedForm = await possibleClosedForm(ws);
|
const [wsClosedForm, restClosedForm] = await Promise.all([
|
||||||
const restClosedForm = await possibleClosedForm(rest);
|
possibleClosedForm(ws),
|
||||||
|
possibleClosedForm(rest),
|
||||||
|
]);
|
||||||
|
|
||||||
await newMessage.edit({
|
await newMessage.edit({
|
||||||
content: [
|
content: [
|
||||||
|
@ -22,47 +24,3 @@ export default {
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
} 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
57
src/util.ts
57
src/util.ts
|
@ -1,5 +1,5 @@
|
||||||
import { GuildMember } from "@lilybird/transformers";
|
import { GuildMember } from "@lilybird/transformers";
|
||||||
import { BUN_EMOJIS } from "./constants.ts";
|
import { BUN_EMOJIS, wolframApiClient } from "./constants.ts";
|
||||||
import { parseAndRemap, formatMarkdown } from "bun-tracestrings";
|
import { parseAndRemap, formatMarkdown } from "bun-tracestrings";
|
||||||
|
|
||||||
const URL_REGEX = /\(\s*(https?:\/\/[^\s\[\]]+)\s*\)/gi;
|
const URL_REGEX = /\(\s*(https?:\/\/[^\s\[\]]+)\s*\)/gi;
|
||||||
|
@ -73,3 +73,58 @@ export async function getBunReportDetailsInMarkdown(
|
||||||
|
|
||||||
return content;
|
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