mirror of
https://github.com/ahmadk953/poixpixel-discord-bot.git
synced 2025-06-06 23:19:30 +00:00
chore: add option to undeploy commands and not deploy on start
This commit is contained in:
parent
072c34d778
commit
14667ad69f
5 changed files with 113 additions and 12 deletions
|
@ -1,7 +1,7 @@
|
|||
import { Client, ClientOptions, Collection } from 'discord.js';
|
||||
import { Command } from '@/types/CommandTypes.js';
|
||||
import { Config } from '@/types/ConfigTypes.js';
|
||||
import { deployCommands } from '@/util/deployCommand.js';
|
||||
import { deployCommands, getFilesRecursively } from '@/util/deployCommand.js';
|
||||
import { registerEvents } from '@/util/eventLoader.js';
|
||||
|
||||
/**
|
||||
|
@ -29,20 +29,69 @@ export class ExtendedClient extends Client {
|
|||
|
||||
private async loadModules() {
|
||||
try {
|
||||
const commands = await deployCommands();
|
||||
if (!commands?.length) {
|
||||
throw new Error('No commands found');
|
||||
}
|
||||
if (process.env.SKIP_COMMAND_DEPLOY === 'true') {
|
||||
console.log('Skipping command deployment (SKIP_COMMAND_DEPLOY=true)');
|
||||
const commandFiles = await this.loadCommandsWithoutDeploying();
|
||||
|
||||
for (const command of commands) {
|
||||
this.commands.set(command.data.name, command);
|
||||
}
|
||||
await registerEvents(this);
|
||||
console.log(
|
||||
`Loaded ${commandFiles.length} commands and registered events (without deployment)`,
|
||||
);
|
||||
} else {
|
||||
const commands = await deployCommands();
|
||||
if (!commands?.length) {
|
||||
throw new Error('No commands found');
|
||||
}
|
||||
|
||||
await registerEvents(this);
|
||||
console.log(`Loaded ${commands.length} commands and registered events`);
|
||||
for (const command of commands) {
|
||||
this.commands.set(command.data.name, command);
|
||||
}
|
||||
|
||||
await registerEvents(this);
|
||||
console.log(`Loaded ${commands.length} commands and registered events`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error loading modules:', error);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads commands without deploying them to Discord
|
||||
* @returns Array of command objects
|
||||
*/
|
||||
private async loadCommandsWithoutDeploying(): Promise<Command[]> {
|
||||
try {
|
||||
const path = await import('path');
|
||||
|
||||
const __dirname = path.resolve();
|
||||
const commandsPath = path.join(__dirname, 'target', 'commands');
|
||||
|
||||
const commandFiles = getFilesRecursively(commandsPath);
|
||||
|
||||
const commands: Command[] = [];
|
||||
for (const file of commandFiles) {
|
||||
const commandModule = await import(`file://${file}`);
|
||||
const command = commandModule.default;
|
||||
|
||||
if (
|
||||
command instanceof Object &&
|
||||
'data' in command &&
|
||||
'execute' in command
|
||||
) {
|
||||
commands.push(command);
|
||||
this.commands.set(command.data.name, command);
|
||||
} else {
|
||||
console.warn(
|
||||
`[WARNING] The command at ${file} is missing a required "data" or "execute" property.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return commands;
|
||||
} catch (error) {
|
||||
console.error('Error loading commands:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue