refactor: remove registerEvents function (#10877)

* refactor: remove `registerEvents` function

* refactor: use try-catch

* fix: missing `await`
This commit is contained in:
Danial Raza
2025-05-11 14:21:13 +02:00
committed by GitHub
parent 4f6fedfb1f
commit 2c21de68f3
10 changed files with 92 additions and 92 deletions

View File

@@ -0,0 +1,20 @@
import { Events } from 'npm:discord.js@^14.19.3';
import type { Event } from './index.ts';
import { loadCommands } from '../util/loaders.ts';
const commands = await loadCommands(new URL('../commands/', import.meta.url));
export default {
name: Events.InteractionCreate,
async execute(interaction) {
if (interaction.isCommand()) {
const command = commands.get(interaction.commandName);
if (!command) {
throw new Error(`Command '${interaction.commandName}' not found.`);
}
await command.execute(interaction);
}
},
} satisfies Event<Events.InteractionCreate>;

View File

@@ -1,18 +1,24 @@
import 'https://deno.land/std@0.223.0/dotenv/load.ts'; import 'https://deno.land/std@0.223.0/dotenv/load.ts';
import { URL } from 'node:url'; import { URL } from 'node:url';
import { Client, GatewayIntentBits } from 'npm:discord.js@^14.19.3'; import { Client, GatewayIntentBits } from 'npm:discord.js@^14.19.3';
import { loadCommands, loadEvents } from './util/loaders.ts'; import { loadEvents } from './util/loaders.ts';
import { registerEvents } from './util/registerEvents.ts';
// Initialize the client // Initialize the client
const client = new Client({ intents: [GatewayIntentBits.Guilds] }); const client = new Client({ intents: [GatewayIntentBits.Guilds] });
// Load the events and commands // Load the events and commands
const events = await loadEvents(new URL('events/', import.meta.url)); const events = await loadEvents(new URL('events/', import.meta.url));
const commands = await loadCommands(new URL('commands/', import.meta.url));
// Register the event handlers // Register the event handlers
registerEvents(commands, events, client); for (const event of events) {
client[event.once ? 'once' : 'on'](event.name, async (...args) => {
try {
await event.execute(...args);
} catch (error) {
console.error(`Error executing event ${String(event.name)}:`, error);
}
});
}
// Login to the client // Login to the client
void client.login(Deno.env.get('DISCORD_TOKEN')); void client.login(Deno.env.get('DISCORD_TOKEN'));

View File

@@ -1,25 +0,0 @@
import { Events, type Client } from 'npm:discord.js@^14.19.3';
import type { Command } from '../commands/index.ts';
import type { Event } from '../events/index.ts';
export function registerEvents(commands: Map<string, Command>, events: Event[], client: Client): void {
// Create an event to handle command interactions
const interactionCreateEvent: Event<Events.InteractionCreate> = {
name: Events.InteractionCreate,
async execute(interaction) {
if (interaction.isCommand()) {
const command = commands.get(interaction.commandName);
if (!command) {
throw new Error(`Command '${interaction.commandName}' not found.`);
}
await command.execute(interaction);
}
},
};
for (const event of [...events, interactionCreateEvent]) {
client[event.once ? 'once' : 'on'](event.name, async (...args) => event.execute(...args));
}
}

View File

@@ -23,7 +23,7 @@ export const schema = z.object({
/** /**
* Defines the predicate to check if an object is a valid Event type. * Defines the predicate to check if an object is a valid Event type.
* *
* @type {import('../util/loaders').StructurePredicate<Event>} * @type {import('../util/loaders.js').StructurePredicate<Event>}
* @returns {structure is Event} * @returns {structure is Event}
*/ */
export const predicate = (structure) => schema.safeParse(structure).success; export const predicate = (structure) => schema.safeParse(structure).success;

View File

@@ -0,0 +1,20 @@
import { Events } from 'discord.js';
import { loadCommands } from '../util/loaders.js';
const commands = await loadCommands(new URL('../commands/', import.meta.url));
/** @type {import('../events/index.js').Event<Events.InteractionCreate>} */
export default {
name: Events.InteractionCreate,
async execute(interaction) {
if (interaction.isCommand()) {
const command = commands.get(interaction.commandName);
if (!command) {
throw new Error(`Command '${interaction.commandName}' not found.`);
}
await command.execute(interaction);
}
},
};

View File

@@ -1,18 +1,24 @@
import process from 'node:process'; import process from 'node:process';
import { URL } from 'node:url'; import { URL } from 'node:url';
import { Client, GatewayIntentBits } from 'discord.js'; import { Client, GatewayIntentBits } from 'discord.js';
import { loadCommands, loadEvents } from './util/loaders.js'; import { loadEvents } from './util/loaders.js';
import { registerEvents } from './util/registerEvents.js';
// Initialize the client // Initialize the client
const client = new Client({ intents: [GatewayIntentBits.Guilds] }); const client = new Client({ intents: [GatewayIntentBits.Guilds] });
// Load the events and commands // Load the events and commands
const events = await loadEvents(new URL('events/', import.meta.url)); const events = await loadEvents(new URL('events/', import.meta.url));
const commands = await loadCommands(new URL('commands/', import.meta.url));
// Register the event handlers // Register the event handlers
registerEvents(commands, events, client); for (const event of events) {
client[event.once ? 'once' : 'on'](event.name, async (...args) => {
try {
await event.execute(...args);
} catch (error) {
console.error(`Error executing event ${String(event.name)}:`, error);
}
});
}
// Login to the client // Login to the client
void client.login(process.env.DISCORD_TOKEN); void client.login(process.env.DISCORD_TOKEN);

View File

@@ -1,29 +0,0 @@
import { Events } from 'discord.js';
/**
* @param {Map<string, import('../commands/index.js').Command>} commands
* @param {import('../events/index.js').Event[]} events
* @param {import('discord.js').Client} client
*/
export function registerEvents(commands, events, client) {
// Create an event to handle command interactions
/** @type {import('../events/index.js').Event<Events.InteractionCreate>} */
const interactionCreateEvent = {
name: Events.InteractionCreate,
async execute(interaction) {
if (interaction.isCommand()) {
const command = commands.get(interaction.commandName);
if (!command) {
throw new Error(`Command '${interaction.commandName}' not found.`);
}
await command.execute(interaction);
}
},
};
for (const event of [...events, interactionCreateEvent]) {
client[event.once ? 'once' : 'on'](event.name, async (...args) => event.execute(...args));
}
}

View File

@@ -0,0 +1,21 @@
import { URL } from 'node:url';
import { Events } from 'discord.js';
import { loadCommands } from '../util/loaders.[REPLACE_IMPORT_EXT]';
import type { Event } from './index.[REPLACE_IMPORT_EXT]';
const commands = await loadCommands(new URL('../commands/', import.meta.url));
export default {
name: Events.InteractionCreate,
async execute(interaction) {
if (interaction.isCommand()) {
const command = commands.get(interaction.commandName);
if (!command) {
throw new Error(`Command '${interaction.commandName}' not found.`);
}
await command.execute(interaction);
}
},
} satisfies Event<Events.InteractionCreate>;

View File

@@ -1,18 +1,24 @@
import process from 'node:process'; import process from 'node:process';
import { URL } from 'node:url'; import { URL } from 'node:url';
import { Client, GatewayIntentBits } from 'discord.js'; import { Client, GatewayIntentBits } from 'discord.js';
import { loadCommands, loadEvents } from './util/loaders.[REPLACE_IMPORT_EXT]'; import { loadEvents } from './util/loaders.[REPLACE_IMPORT_EXT]';
import { registerEvents } from './util/registerEvents.[REPLACE_IMPORT_EXT]';
// Initialize the client // Initialize the client
const client = new Client({ intents: [GatewayIntentBits.Guilds] }); const client = new Client({ intents: [GatewayIntentBits.Guilds] });
// Load the events and commands // Load the events and commands
const events = await loadEvents(new URL('events/', import.meta.url)); const events = await loadEvents(new URL('events/', import.meta.url));
const commands = await loadCommands(new URL('commands/', import.meta.url));
// Register the event handlers // Register the event handlers
registerEvents(commands, events, client); for (const event of events) {
client[event.once ? 'once' : 'on'](event.name, async (...args) => {
try {
await event.execute(...args);
} catch (error) {
console.error(`Error executing event ${String(event.name)}:`, error);
}
});
}
// Login to the client // Login to the client
void client.login(process.env.DISCORD_TOKEN); void client.login(process.env.DISCORD_TOKEN);

View File

@@ -1,25 +0,0 @@
import { Events, type Client } from 'discord.js';
import type { Command } from '../commands/index.[REPLACE_IMPORT_EXT]';
import type { Event } from '../events/index.[REPLACE_IMPORT_EXT]';
export function registerEvents(commands: Map<string, Command>, events: Event[], client: Client): void {
// Create an event to handle command interactions
const interactionCreateEvent: Event<Events.InteractionCreate> = {
name: Events.InteractionCreate,
async execute(interaction) {
if (interaction.isCommand()) {
const command = commands.get(interaction.commandName);
if (!command) {
throw new Error(`Command '${interaction.commandName}' not found.`);
}
await command.execute(interaction);
}
},
};
for (const event of [...events, interactionCreateEvent]) {
client[event.once ? 'once' : 'on'](event.name, async (...args) => event.execute(...args));
}
}