Add class for interactions

This commit is contained in:
Marius Begby committed 2023-08-31 17:36:16 +02:00
1 parent d0f1e940a5
commit b96a73df6f
37 files changed
+1038 -1012

No files matched your search

+4 -5
View File
@@ -1,7 +1,7 @@
import 'dotenv/config';
import config from 'config';
import { REST, RouteLike, Routes, SlashCommandBuilder } from 'discord.js';
import { REST, RESTPostAPIChatInputApplicationCommandsJSONBody, RouteLike, Routes } from 'discord.js';
import fs from 'node:fs';
import path from 'node:path';
import { v4 as uuidv4 } from 'uuid';
@@ -23,8 +23,8 @@ const logger: Logger = loggerModule.child({
//TODO: Fix deploying not working with require()
const slashCommands: SlashCommandBuilder[] = [];
const systemCommands: SlashCommandBuilder[] = [];
const slashCommands: RESTPostAPIChatInputApplicationCommandsJSONBody[] = [];
const systemCommands: RESTPostAPIChatInputApplicationCommandsJSONBody[] = [];
const commandFolders: string[] = fs.readdirSync(path.resolve('./dist/interactions/commands'));
if (!process.env.DISCORD_BOT_TOKEN) {
@@ -52,8 +52,7 @@ const rest: REST = new REST({ version: '10' }).setToken(process.env.DISCORD_BOT_
for (const file of commandFiles) {
// TODO: create commandModule type
const commandModule = await import(`../interactions/commands/${folder}/${file}`);
const command = commandModule.default;
const { default: command } = await import(`../interactions/commands/${folder}/${file}`);
command.isSystemCommand
? systemCommands.push(command.data.toJSON())
: slashCommands.push(command.data.toJSON());
-50
View File
@@ -1,50 +0,0 @@
import { Collection } from 'discord.js';
import fs from 'node:fs';
import path from 'node:path';
import loggerModule from '../services/logger';
import { RegisterClientCommandsParams } from '../types/utilTypes';
import { Logger } from 'pino';
export const registerClientCommands = async ({ client, executionId }: RegisterClientCommandsParams) => {
const logger: Logger = loggerModule.child({
source: 'registerClientCommands.js',
module: 'register',
name: 'registerClientCommands',
executionId: executionId,
shardId: client.shard?.ids[0]
});
logger.debug('Registering client commands...');
client.commands = new Collection();
const commandFolders: string[] = fs.readdirSync(path.resolve('./dist/interactions/commands'));
for (const folder of commandFolders) {
logger.trace(`Registering client commands for folder '${folder}'...`);
const commandFiles: string[] = fs
.readdirSync(path.resolve(`./dist/interactions/commands/${folder}`))
.filter((file) => file.endsWith('.js'));
for (const file of commandFiles) {
try {
// delete command from require cache
delete require.cache[require.resolve(`../interactions/commands/${folder}/${file}`)];
// register command
//const command = require(`../interactions/commands/${folder}/${file}`);
const commandModule = await import(`../interactions/commands/${folder}/${file}`);
client.commands.delete(commandModule.default.data.name);
client.commands.set(commandModule.default.data.name, commandModule.default);
} catch (error) {
if (error instanceof Error) {
logger.error(`Error registering command ${folder}/${file}: ${error.message}`);
} else {
throw error;
}
}
}
}
logger.trace('Registering client commands complete.');
};
+124
View File
@@ -0,0 +1,124 @@
import { Collection } from 'discord.js';
import fs from 'node:fs';
import path from 'node:path';
import loggerModule from '../services/logger';
import { RegisterClientInteractionsParams } from '../types/utilTypes';
import { Logger } from 'pino';
import { ExtendedClient } from '../types/clientTypes';
export const registerClientInteractions = async ({ client, executionId }: RegisterClientInteractionsParams) => {
const logger: Logger = loggerModule.child({
source: 'registerClientInteractions.js',
module: 'register',
name: 'registerClientInteractions',
executionId: executionId,
shardId: client.shard?.ids[0]
});
await registerSlashCommandInteractions(logger, client);
await registerAutocompleteInteractions(logger, client);
await registerComponentInteractions(logger, client);
logger.debug('Registering all client interactions complete.');
};
async function registerSlashCommandInteractions(logger: Logger, client: ExtendedClient) {
logger.debug('Registering client slash command interactions...');
client.slashCommandInteractions = new Collection();
const baseFolderPath = './dist/interactions/commands';
const slashCommandInteractionFolders: string[] = fs.readdirSync(path.resolve(baseFolderPath));
for (const categoryFolder of slashCommandInteractionFolders) {
logger.trace(`Registering client commands for folder '${categoryFolder}'...`);
const files: string[] = fs
.readdirSync(path.resolve(`${baseFolderPath}/${categoryFolder}`))
.filter((file) => file.endsWith('.js'));
for (const file of files) {
try {
const filePath = path.resolve(`${baseFolderPath}/${categoryFolder}/${file}`);
// delete require cache for file
delete require.cache[filePath];
// import and register to client collection
const { default: slashCommandInteraction } = await import(filePath);
client.slashCommandInteractions.delete(slashCommandInteraction.name);
client.slashCommandInteractions.set(slashCommandInteraction.name, slashCommandInteraction);
} catch (error) {
if (error instanceof Error) {
logger.error(
`Error registering slash command interaction '${categoryFolder}/${file}': ${error.message}`
);
} else {
throw error;
}
}
}
}
logger.trace('Registering client slash command interactions complete.');
}
async function registerAutocompleteInteractions(logger: Logger, client: ExtendedClient) {
logger.debug('Registering client autocomplete interactions...');
client.autocompleteInteractions = new Collection();
const baseFolderPath = './dist/interactions/autocomplete';
const files: string[] = fs.readdirSync(path.resolve(`${baseFolderPath}`)).filter((file) => file.endsWith('.js'));
for (const file of files) {
try {
const filePath = path.resolve(`${baseFolderPath}/${file}`);
// delete require cache for file
delete require.cache[filePath];
// import and register to client collection
const { default: autocompleteInteraction } = await import(filePath);
client.autocompleteInteractions.delete(autocompleteInteraction.name);
client.autocompleteInteractions.set(autocompleteInteraction.name, autocompleteInteraction);
} catch (error) {
if (error instanceof Error) {
logger.error(`Error registering autocomplete interaction '${file}': ${error.message}`);
} else {
throw error;
}
}
}
logger.trace('Registering client autocomplete interactions complete.');
}
async function registerComponentInteractions(logger: Logger, client: ExtendedClient) {
logger.debug('Registering client component interactions...');
client.componentInteractions = new Collection();
const baseFolderPath = './dist/interactions/components';
const files: string[] = fs.readdirSync(path.resolve(`${baseFolderPath}`)).filter((file) => file.endsWith('.js'));
for (const file of files) {
try {
const filePath = path.resolve(`${baseFolderPath}/${file}`);
// delete require cache for file
delete require.cache[filePath];
// import and register to client collection
const { default: componentInteraction } = await import(filePath);
client.componentInteractions.delete(componentInteraction.name);
client.componentInteractions.set(componentInteraction.name, componentInteraction);
} catch (error) {
if (error instanceof Error) {
logger.error(`Error registering component interaction '${file}': ${error.message}`);
} else {
throw error;
}
}
}
logger.trace('Registering client component interactions complete.');
}