Skip to content

refactor: remove registerEvents function #10877

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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>;
14 changes: 10 additions & 4 deletions packages/create-discord-bot/template/Deno/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import 'https://deno.land/std@0.223.0/dotenv/load.ts';
import { URL } from 'node:url';
import { Client, GatewayIntentBits } from 'npm:discord.js@^14.19.3';
import { loadCommands, loadEvents } from './util/loaders.ts';
import { registerEvents } from './util/registerEvents.ts';
import { loadEvents } from './util/loaders.ts';

// Initialize the client
const client = new Client({ intents: [GatewayIntentBits.Guilds] });

// Load the events and commands
const events = await loadEvents(new URL('events/', import.meta.url));
const commands = await loadCommands(new URL('commands/', import.meta.url));

// 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
void client.login(Deno.env.get('DISCORD_TOKEN'));

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const schema = z.object({
/**
* 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}
*/
export const predicate = (structure) => schema.safeParse(structure).success;
Original file line number Diff line number Diff line change
@@ -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);
}
},
};
14 changes: 10 additions & 4 deletions packages/create-discord-bot/template/JavaScript/src/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import process from 'node:process';
import { URL } from 'node:url';
import { Client, GatewayIntentBits } from 'discord.js';
import { loadCommands, loadEvents } from './util/loaders.js';
import { registerEvents } from './util/registerEvents.js';
import { loadEvents } from './util/loaders.js';

// Initialize the client
const client = new Client({ intents: [GatewayIntentBits.Guilds] });

// Load the events and commands
const events = await loadEvents(new URL('events/', import.meta.url));
const commands = await loadCommands(new URL('commands/', import.meta.url));

// 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
void client.login(process.env.DISCORD_TOKEN);

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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>;
14 changes: 10 additions & 4 deletions packages/create-discord-bot/template/TypeScript/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import process from 'node:process';
import { URL } from 'node:url';
import { Client, GatewayIntentBits } from 'discord.js';
import { loadCommands, loadEvents } from './util/loaders.[REPLACE_IMPORT_EXT]';
import { registerEvents } from './util/registerEvents.[REPLACE_IMPORT_EXT]';
import { loadEvents } from './util/loaders.[REPLACE_IMPORT_EXT]';

// Initialize the client
const client = new Client({ intents: [GatewayIntentBits.Guilds] });

// Load the events and commands
const events = await loadEvents(new URL('events/', import.meta.url));
const commands = await loadCommands(new URL('commands/', import.meta.url));

// 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
void client.login(process.env.DISCORD_TOKEN);

This file was deleted.