@kevlid/discordmenus
v0.1.8
Published
some menu stuff for discord stuff
Readme
@kevlid/discordmenus
⚠️ Usage is NOT recommended
const fs = require("fs");
const path = require("path");
const { Client, GatewayIntentBits, REST, Routes, SlashCommandBuilder } = require("discord.js");
const { MenuManager, MenuBuilder, CustomIdPrefix, fromDiscordJS } = require("@kevlid/discordmenus");
const DATA = path.join(__dirname, "menu-data.json");
const store = new Map(
Object.entries(fs.existsSync(DATA) ? JSON.parse(fs.readFileSync(DATA, "utf8")) : { prefix: "!" }),
);
function persist() {
fs.writeFileSync(DATA, JSON.stringify(Object.fromEntries(store), null, 2));
}
const builder = new MenuBuilder()
.setKey("settings")
.setTitle("Settings")
.createCategory(c => c.setKey("main"))
.addString(o => o.setKey("prefix").setTitle("Prefix").setMinLength(1).setMaxLength(5).setPreview(true));
const manager = new MenuManager();
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.once("ready", async () => {
await manager.registerMenu(builder, {
get: (key) => store.get(key),
save: (key, value) => {
store.set(key, value);
persist();
},
});
await new REST().setToken(process.env.BOT_TOKEN).put(
Routes.applicationGuildCommands(client.user.id, process.env.GUILD_ID),
{ body: [new SlashCommandBuilder().setName("menu").setDescription("Open").toJSON()] },
);
});
client.on("interactionCreate", async (interaction) => {
if (interaction.isChatInputCommand() && interaction.commandName === "menu") {
await interaction.deferReply();
await interaction.editReply(
await manager.renderMenu("settings", { userId: interaction.user.id }),
);
return;
}
if (interaction.customId?.startsWith(CustomIdPrefix)) {
const body = await manager.handleInteraction(fromDiscordJS(interaction));
await interaction.client.rest.post(
Routes.interactionCallback(interaction.id, interaction.token),
{ body },
);
}
});
client.login(process.env.BOT_TOKEN);Role / user / channel selects
- Persist IDs as strings (Discord snowflakes are too large for JS numbers; Mongo/JSON numbers will corrupt them and
default_valueswon’t show on rerender). - Select
custom_ids include category + page so paging doesn’t reuse the same component identity in the same slot across pages.
