Skip to content

Commit 2c21de6

Browse files
authored
refactor: remove registerEvents function (#10877)
* refactor: remove `registerEvents` function * refactor: use try-catch * fix: missing `await`
1 parent 4f6fedf commit 2c21de6

File tree

10 files changed

+92
-92
lines changed

10 files changed

+92
-92
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Events } from 'npm:discord.js@^14.19.3';
2+
import type { Event } from './index.ts';
3+
import { loadCommands } from '../util/loaders.ts';
4+
5+
const commands = await loadCommands(new URL('../commands/', import.meta.url));
6+
7+
export default {
8+
name: Events.InteractionCreate,
9+
async execute(interaction) {
10+
if (interaction.isCommand()) {
11+
const command = commands.get(interaction.commandName);
12+
13+
if (!command) {
14+
throw new Error(`Command '${interaction.commandName}' not found.`);
15+
}
16+
17+
await command.execute(interaction);
18+
}
19+
},
20+
} satisfies Event<Events.InteractionCreate>;
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
import 'https://deno.land/[email protected]/dotenv/load.ts';
22
import { URL } from 'node:url';
33
import { Client, GatewayIntentBits } from 'npm:discord.js@^14.19.3';
4-
import { loadCommands, loadEvents } from './util/loaders.ts';
5-
import { registerEvents } from './util/registerEvents.ts';
4+
import { loadEvents } from './util/loaders.ts';
65

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

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

1412
// Register the event handlers
15-
registerEvents(commands, events, client);
13+
for (const event of events) {
14+
client[event.once ? 'once' : 'on'](event.name, async (...args) => {
15+
try {
16+
await event.execute(...args);
17+
} catch (error) {
18+
console.error(`Error executing event ${String(event.name)}:`, error);
19+
}
20+
});
21+
}
1622

1723
// Login to the client
1824
void client.login(Deno.env.get('DISCORD_TOKEN'));

packages/create-discord-bot/template/Deno/src/util/registerEvents.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

packages/create-discord-bot/template/JavaScript/src/events/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const schema = z.object({
2323
/**
2424
* Defines the predicate to check if an object is a valid Event type.
2525
*
26-
* @type {import('../util/loaders').StructurePredicate<Event>}
26+
* @type {import('../util/loaders.js').StructurePredicate<Event>}
2727
* @returns {structure is Event}
2828
*/
2929
export const predicate = (structure) => schema.safeParse(structure).success;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Events } from 'discord.js';
2+
import { loadCommands } from '../util/loaders.js';
3+
4+
const commands = await loadCommands(new URL('../commands/', import.meta.url));
5+
6+
/** @type {import('../events/index.js').Event<Events.InteractionCreate>} */
7+
export default {
8+
name: Events.InteractionCreate,
9+
async execute(interaction) {
10+
if (interaction.isCommand()) {
11+
const command = commands.get(interaction.commandName);
12+
13+
if (!command) {
14+
throw new Error(`Command '${interaction.commandName}' not found.`);
15+
}
16+
17+
await command.execute(interaction);
18+
}
19+
},
20+
};
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
import process from 'node:process';
22
import { URL } from 'node:url';
33
import { Client, GatewayIntentBits } from 'discord.js';
4-
import { loadCommands, loadEvents } from './util/loaders.js';
5-
import { registerEvents } from './util/registerEvents.js';
4+
import { loadEvents } from './util/loaders.js';
65

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

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

1412
// Register the event handlers
15-
registerEvents(commands, events, client);
13+
for (const event of events) {
14+
client[event.once ? 'once' : 'on'](event.name, async (...args) => {
15+
try {
16+
await event.execute(...args);
17+
} catch (error) {
18+
console.error(`Error executing event ${String(event.name)}:`, error);
19+
}
20+
});
21+
}
1622

1723
// Login to the client
1824
void client.login(process.env.DISCORD_TOKEN);

packages/create-discord-bot/template/JavaScript/src/util/registerEvents.js

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { URL } from 'node:url';
2+
import { Events } from 'discord.js';
3+
import { loadCommands } from '../util/loaders.[REPLACE_IMPORT_EXT]';
4+
import type { Event } from './index.[REPLACE_IMPORT_EXT]';
5+
6+
const commands = await loadCommands(new URL('../commands/', import.meta.url));
7+
8+
export default {
9+
name: Events.InteractionCreate,
10+
async execute(interaction) {
11+
if (interaction.isCommand()) {
12+
const command = commands.get(interaction.commandName);
13+
14+
if (!command) {
15+
throw new Error(`Command '${interaction.commandName}' not found.`);
16+
}
17+
18+
await command.execute(interaction);
19+
}
20+
},
21+
} satisfies Event<Events.InteractionCreate>;
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
import process from 'node:process';
22
import { URL } from 'node:url';
33
import { Client, GatewayIntentBits } from 'discord.js';
4-
import { loadCommands, loadEvents } from './util/loaders.[REPLACE_IMPORT_EXT]';
5-
import { registerEvents } from './util/registerEvents.[REPLACE_IMPORT_EXT]';
4+
import { loadEvents } from './util/loaders.[REPLACE_IMPORT_EXT]';
65

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

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

1412
// Register the event handlers
15-
registerEvents(commands, events, client);
13+
for (const event of events) {
14+
client[event.once ? 'once' : 'on'](event.name, async (...args) => {
15+
try {
16+
await event.execute(...args);
17+
} catch (error) {
18+
console.error(`Error executing event ${String(event.name)}:`, error);
19+
}
20+
});
21+
}
1622

1723
// Login to the client
1824
void client.login(process.env.DISCORD_TOKEN);

packages/create-discord-bot/template/TypeScript/src/util/registerEvents.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)