Skip to content

Commit 4b9aaa5

Browse files
committed
Upgrade Interaction Handler and its Interactions to TypeScript
1 parent 031da14 commit 4b9aaa5

26 files changed

+145
-112
lines changed

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
import config from '../data/config.js';
22
import chalk from 'chalk';
3-
import { REST, Routes } from 'discord.js';
3+
import { Client, REST, Routes } from 'discord.js';
4+
45
const rest = new REST({ version: '10' }).setToken(config.bot.token);
56

6-
async function registerApplicationCommands(client) {
7+
async function registerApplicationCommands(client: Client) {
78
try {
8-
let commands = new Map();
9+
if (!client.user) throw new Error('User not available in the client.');
10+
11+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
12+
const commands = new Map<string, any>();
913
for (const [key, value] of client.interaction) {
10-
if (key.startsWith('slashCommand-') || key.startsWith('contextMenu-')) commands.set(key, value);
14+
if (typeof key === 'string' && (key.startsWith('slashCommand-') || key.startsWith('contextMenu-'))) {
15+
commands.set(key, value);
16+
}
1117
}
12-
18+
1319
await rest.put(
1420
Routes.applicationCommands(client.user.id),
1521
{ body: [...commands.values()].map(command => command.data.toJSON()) },
1622
);
23+
1724
console.log(chalk.greenBright('[APPLICATION] Successfully registered application commands.'));
1825
} catch (err) {
1926
console.error(err);

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
import fs from 'fs';
22
import chalk from 'chalk';
3+
import { Client } from 'discord.js';
34

4-
async function loadInteractions(folderPath, client) {
5+
async function loadInteractions(folderPath: string, client: Client) {
56
try {
6-
let files = fs.readdirSync(folderPath);
7+
const files = fs.readdirSync(folderPath);
78

89
for (let i = 0; i < files.length; i++) {
9-
let file = files[i];
10-
let filePath = folderPath + '/' + file;
11-
let stats = fs.statSync(filePath);
10+
const file = files[i];
11+
const filePath = folderPath + '/' + file;
12+
const stats = fs.statSync(filePath);
1213

1314
if (stats.isDirectory()) {
1415
await loadInteractions(filePath, client);
1516
} else {
16-
let interactionPath = '.' + filePath;
17-
let interaction = await import(`../${interactionPath}?${Date.now()}`);
17+
const interactionPath = '.' + filePath;
18+
const interaction = await import(`../${interactionPath}?${Date.now()}`);
1819

1920
if (interaction.default.disabled === true) return;
2021
if (interaction.default.type === 'messageCommand') {

src/index.ts

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

44
import loadEvents from './handlers/events.js';
5-
// import loadInteractions from './handlers/handler.js';
6-
// import registerApplicationCommands from './handlers/application.js';
5+
import loadInteractions from './handlers/handler.js';
6+
import registerApplicationCommands from './handlers/application.js';
77
import { loadCronJobs } from './handlers/cronjobs.js';
88

99
const client = new Discord.Client({
@@ -27,9 +27,9 @@ await client.login(config.bot.token);
2727
client.interaction = new Discord.Collection();
2828

2929
await loadEvents(client);
30-
// await loadInteractions('./src/interactions', client);
31-
// await registerApplicationCommands(client);
32-
await loadCronJobs('./src/cron', client);
30+
await loadInteractions('./dist/interactions', client);
31+
await registerApplicationCommands(client);
32+
await loadCronJobs('./dist/cron', client);
3333

3434
process.on('uncaughtException', function (err) {
3535
console.error(err);

src/interactions/button.js

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

src/interactions/button.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Client, ButtonInteraction } from 'discord.js';
2+
3+
export default {
4+
id: 'button',
5+
type: 'button',
6+
disabled: false,
7+
async execute(client: Client, interaction: ButtonInteraction) {
8+
interaction.reply('Hello World!');
9+
}
10+
};

src/interactions/channelSelectMenu.js

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Client, ChannelSelectMenuInteraction } from 'discord.js';
2+
3+
export default {
4+
id: 'channelSelectMenu',
5+
type: 'channelSelectMenu',
6+
disabled: false,
7+
async execute(client: Client, interaction: ChannelSelectMenuInteraction) {
8+
interaction.reply('Hello World!');
9+
}
10+
};

src/interactions/mentionableSelectMenu.js

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Client, MentionableSelectMenuInteraction } from 'discord.js';
2+
3+
export default {
4+
id: 'mentionableSelectMenu',
5+
type: 'mentionableSelectMenu',
6+
disabled: false,
7+
async execute(client: Client, interaction: MentionableSelectMenuInteraction) {
8+
interaction.reply('Hello World!');
9+
}
10+
};

src/interactions/messageCommand.js

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

0 commit comments

Comments
 (0)