gistfile1.txt
· 2.4 KiB · Text
Originalformat
import { ChatInputCommandInteraction } from "discord.js";
import { Updates } from "@systems/updates";
import { Discord } from "@discord";
export async function handleUpdatesPost(interaction: ChatInputCommandInteraction): Promise<void> {
await Discord.Interaction.deferReply(interaction, { ephemeral: true });
const opts = Discord.Interaction.options(interaction);
const version = opts.string({ key: "version" }) ?? Updates.latest();
if (!version) {
await Discord.Interaction.editReply(interaction, "❌ No versions found.");
return;
}
try {
await Updates.post({ version, client: interaction.client });
await Discord.Interaction.editReply(interaction, `✅ Update \`${version}\` posted.`);
} catch (err: any) {
await Discord.Interaction.editReply(interaction, `❌ Failed: ${err.message}`);
}
}
export async function handleUpdatesPreview(interaction: ChatInputCommandInteraction): Promise<void> {
await Discord.Interaction.deferReply(interaction, { ephemeral: true });
const opts = Discord.Interaction.options(interaction);
const version = opts.string({ key: "version" }) ?? Updates.latest();
if (!version) {
await Discord.Interaction.editReply(interaction, "❌ No versions found.");
return;
}
await Updates.preview({ version, interaction });
}
export async function handleUpdatesList(interaction: ChatInputCommandInteraction): Promise<void> {
const versions = Updates.list();
const latest = Updates.latest();
const lines = versions.map((v) => {
const entry = Updates.get({ version: v });
const tag = v === latest ? " ← latest" : "";
return `⬜ \`${v}\` — ${entry?.title ?? ""}${tag}`;
});
await Discord.Interaction.reply(interaction, {
content: lines.length > 0 ? lines.join("\n") : "No versions found.",
ephemeral: true,
});
}
export async function autocompleteVersion(interaction: any): Promise<void> {
const focused = interaction.options.getFocused().toLowerCase();
const versions = Updates.list();
const choices = versions
.filter((v) => v.toLowerCase().includes(focused))
.map((v) => {
const entry = Updates.get({ version: v });
return { name: `${v} — ${entry?.title ?? ""}`, value: v };
})
.slice(0, 25);
await interaction.respond(choices);
}
export const UpdatesCommands = {
post: handleUpdatesPost,
preview: handleUpdatesPreview,
list: handleUpdatesList,
autocomplete: autocompleteVersion,
};
| 1 | import { ChatInputCommandInteraction } from "discord.js"; |
| 2 | import { Updates } from "@systems/updates"; |
| 3 | import { Discord } from "@discord"; |
| 4 | |
| 5 | export async function handleUpdatesPost(interaction: ChatInputCommandInteraction): Promise<void> { |
| 6 | await Discord.Interaction.deferReply(interaction, { ephemeral: true }); |
| 7 | |
| 8 | const opts = Discord.Interaction.options(interaction); |
| 9 | const version = opts.string({ key: "version" }) ?? Updates.latest(); |
| 10 | |
| 11 | if (!version) { |
| 12 | await Discord.Interaction.editReply(interaction, "❌ No versions found."); |
| 13 | return; |
| 14 | } |
| 15 | |
| 16 | try { |
| 17 | await Updates.post({ version, client: interaction.client }); |
| 18 | await Discord.Interaction.editReply(interaction, `✅ Update \`${version}\` posted.`); |
| 19 | } catch (err: any) { |
| 20 | await Discord.Interaction.editReply(interaction, `❌ Failed: ${err.message}`); |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | export async function handleUpdatesPreview(interaction: ChatInputCommandInteraction): Promise<void> { |
| 25 | await Discord.Interaction.deferReply(interaction, { ephemeral: true }); |
| 26 | |
| 27 | const opts = Discord.Interaction.options(interaction); |
| 28 | const version = opts.string({ key: "version" }) ?? Updates.latest(); |
| 29 | |
| 30 | if (!version) { |
| 31 | await Discord.Interaction.editReply(interaction, "❌ No versions found."); |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | await Updates.preview({ version, interaction }); |
| 36 | } |
| 37 | |
| 38 | export async function handleUpdatesList(interaction: ChatInputCommandInteraction): Promise<void> { |
| 39 | const versions = Updates.list(); |
| 40 | const latest = Updates.latest(); |
| 41 | const lines = versions.map((v) => { |
| 42 | const entry = Updates.get({ version: v }); |
| 43 | const tag = v === latest ? " ← latest" : ""; |
| 44 | return `⬜ \`${v}\` — ${entry?.title ?? ""}${tag}`; |
| 45 | }); |
| 46 | |
| 47 | await Discord.Interaction.reply(interaction, { |
| 48 | content: lines.length > 0 ? lines.join("\n") : "No versions found.", |
| 49 | ephemeral: true, |
| 50 | }); |
| 51 | } |
| 52 | |
| 53 | export async function autocompleteVersion(interaction: any): Promise<void> { |
| 54 | const focused = interaction.options.getFocused().toLowerCase(); |
| 55 | const versions = Updates.list(); |
| 56 | const choices = versions |
| 57 | .filter((v) => v.toLowerCase().includes(focused)) |
| 58 | .map((v) => { |
| 59 | const entry = Updates.get({ version: v }); |
| 60 | return { name: `${v} — ${entry?.title ?? ""}`, value: v }; |
| 61 | }) |
| 62 | .slice(0, 25); |
| 63 | await interaction.respond(choices); |
| 64 | } |
| 65 | |
| 66 | export const UpdatesCommands = { |
| 67 | post: handleUpdatesPost, |
| 68 | preview: handleUpdatesPreview, |
| 69 | list: handleUpdatesList, |
| 70 | autocomplete: autocompleteVersion, |
| 71 | }; |