Fixed most issues. Ping command still does not work. Using yarn instead of pnpm.

This commit is contained in:
Ahmad Khan 2023-09-16 22:10:14 -04:00
parent 41e6158fbc
commit fdf256346e
14 changed files with 674 additions and 1829 deletions

View file

@ -1,43 +1,56 @@
import { REST } from 'discord.js';
import fs from 'fs';
import * as fs from 'fs/promises';
import path from 'path';
import { REST } from '@discordjs/rest';
import { fileURLToPath } from 'url';
const commands: any = [];
const __dirname = path.dirname(new URL(import.meta.url).pathname);
const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.resolve();
// Define the path to the directory containing your command files
const foldersPath = path.join(__dirname, '\\target\\commands'); // Change 'commands' to your actual directory name
export async function registerCommands(clientId: any, guildId: any, token: any) {
for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.ts'));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = await import(filePath);
if ('data' in command && 'execute' in command) {
commands.push(command.data.toJSON());
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
const commands = [];
try {
const commandFolders = await fs.readdir(foldersPath);
for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
const commandFiles = (await fs.readdir(commandsPath)).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join('file://', commandsPath, file);
// Import the module and handle any potential errors
let commandModule;
try {
commandModule = await import(filePath);
} catch (error) {
console.error(`Error importing module from ${filePath}: ${error}`);
continue; // Continue to the next module on error
}
// Check if the imported module has 'data' and 'execute' properties
if ('data' in commandModule && 'execute' in commandModule) {
commands.push(commandModule.data.toJSON());
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
}
}
const rest = new REST().setToken(token);
(async () => {
try {
console.log(`Started refreshing ${commands.length} application (/) commands.`);
const data: any = await rest.put(
`/applications/${clientId}/guilds/${guildId}/commands`,
{ body: commands }
);
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
console.error(error);
}
})();
}
export default registerCommands;
const rest = new REST({ version: '10' }).setToken(token);
console.log(`Started refreshing ${commands.length} application (/) commands.`);
const data: any = await rest.put(
`/applications/${clientId}/guilds/${guildId}/commands`,
{ body: commands }
);
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
console.error(error);
}
}

View file

@ -12,17 +12,20 @@ class CustomClient extends Client {
const client = new CustomClient({ intents: [GatewayIntentBits.Guilds] });
async function initializeCommands() {
const foldersPathBefore = path.join(__dirname, 'commands');
const foldersPath = foldersPathBefore.slice(1);
function initializeCommands() {
const __dirname = path.resolve();
const foldersPath = path.join(__dirname, '\\target\\commands');
console.log(foldersPath)
console.log(`Folder Path Received: ${foldersPath}`);
try {
const commandFiles = await fs.promises.readdir(foldersPath);
const commandFiles = fs.readdirSync(foldersPath);
for (const file of commandFiles) {
const command = require(path.join(foldersPath, file));
client.commands.set(command.name, command);
if (file.endsWith('.js')) {
const command = require(path.join(foldersPath, file));
console.log(command);
client.commands.set(command.name, command);
}
}
console.log('Commands initialized successfully!');
} catch (error) {
@ -33,7 +36,7 @@ async function initializeCommands() {
initializeCommands();
try {
registerCommands(clientId, guildId, token);
await registerCommands(clientId, guildId, token);
} catch (error) {
console.error("Error registering slash commands:", error);
}