Skip to content
This repository has been archived by the owner on Apr 23, 2023. It is now read-only.

Commit

Permalink
Merge pull request #24 from DynamicaBot/sebasptsch/issue17
Browse files Browse the repository at this point in the history
Better command and event structure
  • Loading branch information
sebasptsch authored Dec 19, 2021
2 parents b0e56db + 842b838 commit 81cfb4a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 26 deletions.
6 changes: 3 additions & 3 deletions src/deploy-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ if (!TOKEN || !CLIENT_ID) {

(async () => {
try {
logger.info("Started refreshing application (/) commands.");
await logger.info("Started refreshing application (/) commands.");
if (GUILD_ID) {
await rest.put(Routes.applicationGuildCommands(CLIENT_ID, GUILD_ID), {
body: commandList,
Expand All @@ -29,9 +29,9 @@ if (!TOKEN || !CLIENT_ID) {
});
}

logger.info("Successfully reloaded application (/) commands.");
await logger.info("Successfully reloaded application (/) commands.");
} catch (error) {
logger.error(error);
await logger.error(error);
}
})();
}
24 changes: 24 additions & 0 deletions src/events/command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { CommandInteraction } from "discord.js";
import * as commands from "../commands";
import { ErrorEmbed } from "../lib/discordEmbeds";
import { logger } from "../lib/logger";
import { event } from "./event";

export const commandListener: event = {
name: "interactionCreate",
once: false,
async execute(interaction: CommandInteraction) {
if (!interaction.isCommand()) return;
try {
await commands[interaction.commandName].execute(interaction);
} catch (e) {
logger.error(e);
interaction.reply({
embeds: [
ErrorEmbed("There was an error while executing this command!"),
],
ephemeral: true,
});
}
},
};
25 changes: 2 additions & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Client, Intents } from "discord.js";
import dotenv from "dotenv";
import * as commands from "./commands";
import * as events from "./events";
import { ErrorEmbed } from "./lib/discordEmbeds";
import { logger } from "./lib/logger";
import { db } from "./lib/prisma";
import { scheduler } from "./lib/scheduler";
Expand All @@ -17,29 +15,9 @@ const client = new Client({
],
});

const commandList = Object.values(commands);
client.on("interactionCreate", async (interaction) => {
if (!interaction.isCommand()) return;

const command = commandList.find(
(command) => command.data.name === interaction.commandName
);

if (!command) return;

try {
await command.execute(interaction);
} catch (e) {
logger.error(e);
interaction.reply({
embeds: [ErrorEmbed("There was an error while executing this command!")],
ephemeral: true,
});
}
});

const eventList = Object.values(events);

// Register event handlers
for (const event of eventList) {
if (event.once) {
client.once(event.name, (...args: any) => event.execute(...args));
Expand All @@ -51,6 +29,7 @@ for (const event of eventList) {
// Login to Discord with your client's token
client.login(process.env.TOKEN);

// Handle stop signal
process.on("SIGINT", () => {
client.destroy();
scheduler.stop();
Expand Down

0 comments on commit 81cfb4a

Please sign in to comment.