From ac0672f13a87efcdf8193286c6a5bcd3d3743de1 Mon Sep 17 00:00:00 2001 From: Lukian LEIZOUR Date: Fri, 15 Mar 2024 21:48:47 +0100 Subject: [PATCH] bug resolutions --- package.json | 1 + src/commands/default/ping.ts | 2 +- src/events/interactionCreate.ts | 2 +- src/events/messageCreate.ts | 3 +- src/events/ready.ts | 2 +- src/index.ts | 79 ++++++++++++++++++--------------- tsconfig.json | 6 ++- 7 files changed, 53 insertions(+), 42 deletions(-) diff --git a/package.json b/package.json index 9822c7b..775856f 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "1.0.0", "description": "", "main": "index.js", + "type": "module", "scripts": { "dev": "tsx watch src", "build": "tsc", diff --git a/src/commands/default/ping.ts b/src/commands/default/ping.ts index 4bc8ec3..b40c2d0 100644 --- a/src/commands/default/ping.ts +++ b/src/commands/default/ping.ts @@ -1,6 +1,6 @@ import { SlashCommandBuilder, ChatInputCommandInteraction } from "discord.js"; -module.exports = { +export default { data: new SlashCommandBuilder() .setName("ping") .setDescription("Replies with Pong!"), diff --git a/src/events/interactionCreate.ts b/src/events/interactionCreate.ts index 80dc5bc..03c4322 100644 --- a/src/events/interactionCreate.ts +++ b/src/events/interactionCreate.ts @@ -1,6 +1,6 @@ import { Events, Interaction } from "discord.js"; -module.exports = { +export default { name: Events.InteractionCreate, async execute(interaction: Interaction) { if (interaction.isChatInputCommand()) { diff --git a/src/events/messageCreate.ts b/src/events/messageCreate.ts index 67fe0d7..794121c 100644 --- a/src/events/messageCreate.ts +++ b/src/events/messageCreate.ts @@ -1,5 +1,6 @@ import { Events, Message } from "discord.js"; -module.exports = { + +export default { name: Events.MessageCreate, async execute(message: Message) { //console.log(message.content); diff --git a/src/events/ready.ts b/src/events/ready.ts index e6f4053..bc9a17e 100644 --- a/src/events/ready.ts +++ b/src/events/ready.ts @@ -1,6 +1,6 @@ import { Events, Client } from "discord.js"; -module.exports = { +export default { name: Events.ClientReady, once: true, execute(client: Client) { diff --git a/src/index.ts b/src/index.ts index f4e87b8..4ebde80 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,32 +9,56 @@ const client: Client = new Client({ }) client.commands = new Collection(); -const commands = []; -const foldersPath = path.join(__dirname, "commands"); -const commandFolders = fs.readdirSync(foldersPath); +const commands: any[] = []; -for (const folder of commandFolders) { - const commandsPath = path.join(foldersPath, folder); - const commandFiles = fs - .readdirSync(commandsPath) - .filter((file) => file.endsWith(".ts") || file.endsWith(".js")); +async function loadCommands() { + const foldersPath = './src/commands'; + const commandFolders = fs.readdirSync(foldersPath); - for (const file of commandFiles) { - const filePath = path.join(commandsPath, file); - const command = require(filePath); - if ("data" in command && "execute" in command) { - client.commands.set(command.data.name, command); - commands.push(command.data.toJSON()); - } - else { - console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`); - } - } + for (const folder of commandFolders) { + const commandsPath = `./src/commands/${folder}`; + const commandFiles = fs + .readdirSync(commandsPath) + .filter((file) => file.endsWith(".ts") || file.endsWith(".js")); + + for (const file of commandFiles) { + const filePath = `./commands/${folder}/${file}`; + const command = await import(filePath.replace(".ts", ".js")); + if ("data" in command.default && "execute" in command.default) { + client.commands.set(command.default.data.name, command.default); + commands.push(command.default.data.toJSON()); + } + else { + console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`); + } + } + } +} + +async function loadEvents() { + const eventsPath = "./src/events"; + const eventFiles = fs + .readdirSync(eventsPath) + .filter((file) => file.endsWith(".ts") || file.endsWith(".js")); + + for (const file of eventFiles) { + const filePath = `./events/${file}`; + const event = await import(filePath.replace(".ts", ".js")); + + if (event.default.once) { + client.once(event.default.name, (...args) => event.default.execute(...args)); + } + else { + client.on(event.default.name, (...args) => event.default.execute(...args)); + } + } } const rest = new REST().setToken(process.env.DISCORD_TOKEN ? process.env.DISCORD_TOKEN : ""); (async () => { + await loadCommands(); + await loadEvents(); try { console.log(`Started refreshing ${commands.length} application (/) commands.`); @@ -49,21 +73,4 @@ const rest = new REST().setToken(process.env.DISCORD_TOKEN ? process.env.DISCORD } })(); -const eventsPath = path.join(__dirname, "events"); -const eventFiles = fs - .readdirSync(eventsPath) - .filter((file) => file.endsWith(".ts") || file.endsWith(".js")); - -for (const file of eventFiles) { - const filePath = path.join(eventsPath, file); - const event = require(filePath); - - if (event.once) { - client.once(event.name, (...args) => event.execute(...args)); - } - else { - client.on(event.name, (...args) => event.execute(...args)); - } -} - client.login(process.env.DISCORD_TOKEN); diff --git a/tsconfig.json b/tsconfig.json index 145223f..bd66ca5 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,9 @@ { "compilerOptions": { - "target": "es5", - "module": "commonjs", + "target": "ES5", + "module": "NodeNext", + "moduleResolution": "NodeNext", + "resolveJsonModule": true, "strict": true, "outDir": "./dist", "skipLibCheck": true,