Skip to content

Commit 3b9d590

Browse files
committed
Upgrade event handler to TypeScript
1 parent 5dcda5b commit 3b9d590

File tree

5 files changed

+31
-10
lines changed

5 files changed

+31
-10
lines changed

src/events/interactionCreate.js renamed to src/events/interactionCreate.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1+
import { Client, Interaction, MessageComponentInteraction, CommandInteraction } from 'discord.js';
2+
3+
interface CustomInteraction {
4+
id: string;
5+
type: string;
6+
disabled: boolean;
7+
execute(client: Client, interaction: MessageComponentInteraction | Interaction | CommandInteraction | any): Promise<void>;
8+
}
9+
110
export default {
211
event: 'interactionCreate',
3-
async execute(client, interaction) {
12+
async execute(client: Client, interaction: MessageComponentInteraction | Interaction | CommandInteraction | any) {
413
try {
514

615
const interactionType = (() => {
@@ -17,7 +26,7 @@ export default {
1726
if (interaction.isUserSelectMenu()) return 'userSelectMenu';
1827
})();
1928

20-
let action = client.interaction.get(`${interactionType}-${interaction.customId || interaction.commandName}`);
29+
let action = client.interaction.get(`${interactionType}-${interaction.customId || interaction.commandName}`) as CustomInteraction;
2130
if (!action) return;
2231
try {
2332
action.execute(client, interaction);

src/events/messageCreate.js renamed to src/events/messageCreate.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
import config from '../data/config.js';
2+
import { Client, Message } from 'discord.js';
3+
4+
interface CustomCommand {
5+
id: string;
6+
type: string;
7+
disabled: boolean;
8+
aliases: string[];
9+
execute(client: Client, message: Message, args: string[]): Promise<void>;
10+
}
211

312
export default {
413
event: 'messageCreate',
5-
async execute(client, message) {
14+
async execute(client: Client, message: Message) {
615
try {
716
if (message.author.bot) return;
817

918
if (message.content.startsWith(config.bot.prefix)) {
1019
const args = message.content.slice(config.bot.prefix.length).trim().split(/ +/);
11-
const command = client.interaction.get(`messageCommand-${args[0]}`);
20+
const command = client.interaction.get(`messageCommand-${args[0]}`) as CustomCommand;
1221
if (!command) return;
1322
try {
1423
command.execute(client, message, args);

src/events/ready.js renamed to src/events/ready.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
import { Client } from 'discord.js';
12
import chalk from 'chalk';
23
import { readFileSync } from 'fs';
34
let version = JSON.parse(readFileSync('./package.json', 'utf8')).version;
45

56
export default {
67
event: 'ready',
7-
async execute(client) {
8+
async execute(client: Client) {
89
try {
9-
console.log(chalk.yellow(`[LOGIN] logged in as ${client.user.tag} -> Version ${version}`));
10+
console.log(chalk.yellow(`[LOGIN] logged in as ${client.user!.tag} -> Version ${version}`));
1011
} catch (error) {
1112
return console.log(error);
1213
}

src/handlers/events.js renamed to src/handlers/events.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { readdirSync } from 'fs';
22
import chalk from 'chalk';
3+
import { Client } from 'discord.js';
34

4-
async function loadEvents(client) {
5-
const eventFiles = readdirSync('./src/events').filter(file => file.endsWith('.js'));
5+
async function loadEvents(client: Client) {
6+
const eventFiles = readdirSync('./dist/events').filter(file => file.endsWith('.js'));
67
if (!eventFiles) return;
78
for (let i = 0; i < eventFiles.length; i++) {
9+
eventFiles[i] = eventFiles[i];
810
const event = await import(`../events/${eventFiles[i]}`);
911
client.on(event.default.event, event.default.execute.bind(null, client));
1012
console.log(chalk.greenBright(`[EVENT] Loaded ${(chalk.yellow(eventFiles[i]))} with event ${(chalk.yellow(event.default.event))}`));

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Discord from 'discord.js';
22
import config from './data/config.js';
33

4-
// import loadEvents from './handlers/events.js';
4+
import loadEvents from './handlers/events.js';
55
// import loadInteractions from './handlers/handler.js';
66
// import registerApplicationCommands from './handlers/application.js';
77
import { loadCronJobs } from './handlers/cronjobs.js';
@@ -26,7 +26,7 @@ await client.login(config.bot.token);
2626

2727
client.interaction = new Discord.Collection();
2828

29-
// await loadEvents(client);
29+
await loadEvents(client);
3030
// await loadInteractions('./src/interactions', client);
3131
// await registerApplicationCommands(client);
3232
await loadCronJobs('./src/cron', client);

0 commit comments

Comments
 (0)