import { ApplicationCommandTypes, Client, ComponentTypes, InteractionTypes } from "oceanic.js"; import { readdir } from "node:fs/promises"; import { KVDB } from "./utils/kvdb"; const db = new KVDB({ user: process.env.POSTGRESQL_USERNAME, password: process.env.POSTGRESQL_PASSWORD, host: process.env.POSTGRESQL_HOST, port: process.env.POSTGRESQL_PORT, database: process.env.POSTGRESQL_DATABASE }); await db.init(); const client = new Client({ token: process.env.TOKEN }); const commandFiles = await readdir("./commands", { recursive: true }); const commandsData = [] const commands = new Map() for (const commandFile of commandFiles) { const command = (await import(`./commands/${commandFile}`)).default commandsData.push(command.data) commands.set(command.data.name, command) } const buttonFiles = await readdir("./buttons", { recursive: true }); const buttonsData = [] const buttons = new Map() for (const buttonFile of buttonFiles) { const button = (await import(`./buttons/${buttonFile}`)).default buttonsData.push(button.data) buttons.set(button.data.name, button) } const modalFiles = await readdir("./modals", { recursive: true }); const modalsData = [] const modals = new Map() for (const modalFile of modalFiles) { const modal = (await import(`./modals/${modalFile}`)).default modalsData.push(modal.data) modals.set(modal.data.name, modal) } client.on("ready", async() => { await client.application.bulkEditGlobalCommands(commandsData); console.log("Ready as", client.user.tag); }); // if you do not add a listener for the error event, any errors will cause an UncaughtError to be thrown, // and your process may be killed as a result. client.on("error", (err) => { console.error("bro vazne..", err); }); client.on("interactionCreate", async(interaction) => { switch(interaction.type) { // https://docs.oceanic.ws/latest/classes/CommandInteraction.html case InteractionTypes.APPLICATION_COMMAND: // data = https://docs.oceanic.ws/latest/interfaces/Types_Interactions.ApplicationCommandInteractionData.html switch(interaction.data.type) { // Chat Input commands are what you use in the chat, i.e. slash commands case ApplicationCommandTypes.CHAT_INPUT: const command = commands.get(interaction.data.name) if (!command) return await interaction.reply("invalid command") await command.handle(interaction, db) break } break case InteractionTypes.MESSAGE_COMPONENT: switch(interaction.data.componentType) { case ComponentTypes.BUTTON: const button = buttons.get(interaction.data.customID.split(".")[0]) if (!button) return await interaction.reply("something went wrong") await button.handle(interaction, db) break } break case InteractionTypes.MODAL_SUBMIT: const modal = modals.get(interaction.data.customID.split(".")[0]) if (!modal) return await interaction.reply("something went wrong") await modal.handle(interaction, db) break } }); await client.connect();