diff --git a/bin/cli.js b/bin/cli.js index ff50e4b..cad8bbe 100644 --- a/bin/cli.js +++ b/bin/cli.js @@ -45,9 +45,12 @@ program { type: "input", name: "directory", - message: "Project directory (leave empty for project name):", - default: answers => answers.name, - filter: input => input.trim() + message: "Project directory (leave empty for current):", + default: "", + filter: input => input.trim(), + transformer: (input) => { + return input.trim() === "" ? "(current)" : input; + } }, { type: "list", @@ -95,7 +98,7 @@ program // Create project structure const dirs = [ - ".discraft", + "discraft", "commands", "events", "config", @@ -113,8 +116,8 @@ program // Copy template files const templateFiles = { "config/bot.config.js": path.join(__dirname, "..", "src", "config", "bot.config.js"), - ".discraft/commands/handler.js": path.join(__dirname, "..", "src", ".discraft", "commands", "handler.js"), - ".discraft/events/handler.js": path.join(__dirname, "..", "src", ".discraft", "events", "handler.js"), + "discraft/commands/handler.js": path.join(__dirname, "..", "src", "discraft", "commands", "handler.js"), + "discraft/events/handler.js": path.join(__dirname, "..", "src", "discraft", "events", "handler.js"), "services/discord.js": path.join(__dirname, "..", "src", "services", "discord.js"), "utils/logger.js": path.join(__dirname, "..", "src", "utils", "logger.js"), "events/ready.js": path.join(__dirname, "..", "src", "events", "ready.js"), diff --git a/package.json b/package.json index 96ca46e..1701e42 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "discraft", - "version": "1.4.10", + "version": "1.4.11", "description": "Best framework for discord bots", "main": "index.js", "type": "module", diff --git a/scripts/compile/genCommands.js b/scripts/compile/genCommands.js index eb63fe7..87b157c 100644 --- a/scripts/compile/genCommands.js +++ b/scripts/compile/genCommands.js @@ -4,12 +4,12 @@ import { info, debug, error } from "../../common/utils/logger.js"; export default function generateCommands(srcDir) { try { - debug("Generating .discraft/commands/index.js..."); + debug("Generating discraft/commands/index.js..."); const COMMANDS_DIR = path.join(srcDir, "commands"); - const OUTPUT_DIR = path.join(srcDir, ".discraft", "commands"); + const OUTPUT_DIR = path.join(srcDir, "discraft", "commands"); const OUTPUT_FILE = path.join(OUTPUT_DIR, "index.js"); - // Create .discraft directory if it doesn't exist + // Create discraft directory if it doesn't exist if (!fs.existsSync(OUTPUT_DIR)) { fs.mkdirSync(OUTPUT_DIR, { recursive: true }); } @@ -38,10 +38,10 @@ export default function generateCommands(srcDir) { content += "};"; - // Write the file to .discraft directory + // Write the file to discraft directory fs.writeFileSync(OUTPUT_FILE, content); - info("Generated .discraft/commands/index.js"); + info("Generated discraft/commands/index.js"); } catch (err) { - error("Error generating .discraft/commands/index.js:", err); + error("Error generating discraft/commands/index.js:", err); } } diff --git a/scripts/compile/genEvents.js b/scripts/compile/genEvents.js index 18ad604..efba57e 100644 --- a/scripts/compile/genEvents.js +++ b/scripts/compile/genEvents.js @@ -4,12 +4,12 @@ import { info, debug, error } from "../../common/utils/logger.js"; export default function generateEvents(srcDir) { try { - debug("Generating .discraft/events/index.js..."); + debug("Generating discraft/events/index.js..."); const EVENTS_DIR = path.join(srcDir, "events"); - const OUTPUT_DIR = path.join(srcDir, ".discraft", "events"); + const OUTPUT_DIR = path.join(srcDir, "discraft", "events"); const OUTPUT_FILE = path.join(OUTPUT_DIR, "index.js"); - // Create .discraft directory if it doesn't exist + // Create discraft directory if it doesn't exist if (!fs.existsSync(OUTPUT_DIR)) { fs.mkdirSync(OUTPUT_DIR, { recursive: true }); } @@ -38,10 +38,10 @@ export default function generateEvents(srcDir) { content += "};"; - // Write the file to .discraft directory + // Write the file to discraft directory fs.writeFileSync(OUTPUT_FILE, content); - info("Generated .discraft/events/index.js"); + info("Generated discraft/events/index.js"); } catch (err) { - error("Error generating .discraft/events/index.js:", err); + error("Error generating discraft/events/index.js:", err); } } diff --git a/src/handlers/CommandHandler.js b/src/handlers/CommandHandler.js deleted file mode 100644 index 93a4666..0000000 --- a/src/handlers/CommandHandler.js +++ /dev/null @@ -1,92 +0,0 @@ -import { Collection, REST, Routes } from "discord.js"; -import { error, info, debug, success } from "../utils/logger.js"; -import { token, clientId } from "../config/bot.config.js"; -import { commands } from "../.discraft/commands/index.js"; - -export class CommandHandler { - constructor(client, startTime) { - this.client = client; - this.commands = new Collection(); - this.commandsData = []; - this.setupEventListeners(); - this.serverStartTime = startTime; - } - - setupEventListeners() { - // Handle command interactions - this.client.on("interactionCreate", async (interaction) => { - if (!interaction.isCommand()) return; - - const command = this.commands.get(interaction.commandName); - if (!command) return; - - try { - await command.execute(interaction); - } catch (err) { - error("Error executing command:", err); - const content = { content: "There was an error executing this command!", ephemeral: true }; - try { - if (interaction.replied || interaction.deferred) { - await interaction.followUp(content); - } else { - await interaction.reply(content); - } - } catch (err) { - error("Error replying:", err); - } - } - }); - - // Handle guild joins - this.client.on("guildCreate", async (guild) => { - debug(`Bot joined new guild: ${guild.name} (${guild.id})`); - await this.registerCommands(); - }); - - // Register commands when bot is ready - this.client.once("ready", async () => { - debug("Registering commands..."); - await this.registerCommands(); - info(`Bot is ready as ${this.client.user.tag}`); - debug(`Time to register commands: ${Date.now() - this.client.readyTimestamp}ms`); - success(`Time to online: ${Date.now() - this.serverStartTime}ms`); - }); - } - - async loadCommands() { - // Clear existing commands - this.commands.clear(); - this.commandsData = []; - - // Load commands from static imports - for (const [name, command] of Object.entries(commands)) { - if ("data" in command && "execute" in command) { - this.commands.set(command.data.name, command); - this.commandsData.push(command.data.toJSON()); - debug(`Loaded command: ${command.data.name}`); - } else { - error(`The command ${name} is missing required "data" or "execute" property.`); - } - } - } - - async registerCommands() { - // Reload commands to ensure we have the latest versions - await this.loadCommands(); - - const rest = new REST().setToken(token); - try { - debug(`Started refreshing ${this.commandsData.length} application (/) commands.`); - - // Register commands globally - const data = await rest.put( - Routes.applicationCommands(clientId), - { body: this.commandsData }, - ); - - info(`Successfully reloaded ${data.length} application (/) commands.`); - } catch (err) { - error("Error registering commands:", err); - } - } -} diff --git a/src/index.js b/src/index.js index e78db03..2675395 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,7 @@ import { debug, error, info, success } from "./utils/logger.js"; import client from "./services/discord.js"; -import { CommandHandler } from "./.discraft/commands/handler.js"; -import { eventHandler } from "./.discraft/events/handler.js"; +import { CommandHandler } from "./discraft/commands/handler.js"; +import { eventHandler } from "./discraft/events/handler.js"; const startTime = Date.now();