Skip to content

Commit 98410cb

Browse files
committed
refactor: change the way that events are registered
1 parent 6cad400 commit 98410cb

File tree

7 files changed

+59
-42
lines changed

7 files changed

+59
-42
lines changed

src/commands/util/walkthrough.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { config } from "lib/config.js";
2-
import { messageData as issueCategorySelectorMessageData } from "components/issueCategorySelector.js";
2+
import { messageData as issueCategorySelectorMessageData } from "ui/components/issueCategorySelector.js";
33

44
import { ChannelType, type CommandInteraction, SlashCommandBuilder } from "discord.js";
55

src/events/commands.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import * as commands from "../commands/index.js";
2+
3+
import { type Client, Events } from "discord.js";
4+
5+
export default function registerEvents(client: Client) {
6+
return client.on(Events.InteractionCreate, async (interaction) => {
7+
if (interaction.isChatInputCommand()) {
8+
const command = commands[interaction.commandName];
9+
10+
if (!command) {
11+
console.error(`No command matching ${interaction.commandName} was found.`);
12+
return;
13+
}
14+
15+
try {
16+
await command.execute(interaction);
17+
} catch (error) {
18+
console.error(error);
19+
20+
if (interaction.replied || interaction.deferred) {
21+
await interaction.followUp({
22+
content: "There was an error while executing this command!",
23+
ephemeral: true,
24+
});
25+
} else {
26+
await interaction.reply({
27+
content: "There was an error while executing this command!",
28+
ephemeral: true,
29+
});
30+
}
31+
}
32+
}
33+
});
34+
}

src/events/walkthrough.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import issueCategorySelector from "../ui/components/issueCategorySelector.js";
2+
import productSelector, { messageData as productSelectorMessageData } from "../ui/components/productSelector.js";
3+
import operatingSystemFamilySelector, { messageData as operatingSystemFamilySelectorMessageData } from "../ui/components/operatingSystemFamilySelector.js";
4+
5+
import { type Client, Events } from "discord.js";
6+
7+
export default function registerEvents(client: Client) {
8+
return client.on(Events.InteractionCreate, async (interaction) => {
9+
if(interaction.isStringSelectMenu()) {
10+
if(interaction.customId === issueCategorySelector.data.custom_id) {
11+
await interaction.update(productSelectorMessageData);
12+
} else if(interaction.customId === productSelector.data.custom_id) {
13+
await interaction.update(operatingSystemFamilySelectorMessageData);
14+
} else if(interaction.customId === operatingSystemFamilySelector.data.custom_id) {
15+
await interaction.update("hi");
16+
}
17+
}
18+
});
19+
}

src/index.ts

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,17 @@
1-
import * as commands from "./commands/index.js";
21
import { config } from "./lib/config.js";
32

4-
import issueCategorySelector from "./components/issueCategorySelector.js";
5-
import productSelector, { messageData as productSelectorMessageData } from "./components/productSelector.js";
6-
import operatingSystemFamilySelector, { messageData as operatingSystemFamilySelectorMessageData } from "./components/operatingSystemFamilySelector.js";
3+
import registerCommandEvents from "./events/commands.js";
4+
import registerWalkthroughEvents from "./events/walkthrough.js";
75

86
import { Client, Events } from "discord.js";
97

108
const client = new Client({ intents: [] });
119

12-
client.on(Events.InteractionCreate, async (interaction) => {
13-
if (interaction.isChatInputCommand()) {
14-
const command = commands[interaction.commandName];
15-
16-
if (!command) {
17-
console.error(`No command matching ${interaction.commandName} was found.`);
18-
return;
19-
}
20-
21-
try {
22-
await command.execute(interaction);
23-
} catch (error) {
24-
console.error(error);
25-
26-
if (interaction.replied || interaction.deferred) {
27-
await interaction.followUp({
28-
content: "There was an error while executing this command!",
29-
ephemeral: true,
30-
});
31-
} else {
32-
await interaction.reply({
33-
content: "There was an error while executing this command!",
34-
ephemeral: true,
35-
});
36-
}
37-
}
38-
} else if(interaction.isStringSelectMenu()) {
39-
if(interaction.customId === issueCategorySelector.data.custom_id) {
40-
await interaction.update(productSelectorMessageData);
41-
} else if(interaction.customId === productSelector.data.custom_id) {
42-
await interaction.update(operatingSystemFamilySelectorMessageData);
43-
} else if(interaction.customId === operatingSystemFamilySelector.data.custom_id) {
44-
await interaction.update("hi");
45-
}
46-
}
47-
});
48-
4910
client.once(Events.ClientReady, () => {
5011
console.log(`Logged in as ${client.user?.tag}!`);
12+
13+
registerCommandEvents(client);
14+
registerWalkthroughEvents(client);
5115
});
5216

5317
client.login(config.token);
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)