Skip to content

Commit

Permalink
Episode 3 - Event Handler
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerPottsDev committed Oct 21, 2021
1 parent 6856e0e commit f1396f3
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 47 deletions.
59 changes: 12 additions & 47 deletions bot.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
require("dotenv").config();
const fs = require("fs");
const { REST } = require("@discordjs/rest");
const { Routes } = require("discord-api-types/v9");
const { Client, Intents, Collection } = require("discord.js");

const client = new Client({
Expand All @@ -23,51 +21,18 @@ for (const file of commandFiles) {
client.commands.set(command.data.name, command);
}

client.once("ready", () => {
console.log("Ghost is online.");

const CLIENT_ID = client.user.id;

const rest = new REST({
version: "9"
}).setToken(process.env.TOKEN);

(async () => {
try {
if (process.env.ENV === "production") {
await rest.put(Routes.applicationCommands(CLIENT_ID), {
body: commands
});
console.log("Successfully registered commands globally.");
} else {
await rest.put(Routes.applicationGuildCommands(CLIENT_ID, process.env.GUILD_ID), {
body: commands
});
console.log("Successfully registered commands locally.");
}
} catch (err) {
if (err) console.error(err);
}
})();
});

client.on("interactionCreate", async interaction => {
if (!interaction.isCommand()) return;

const command = client.commands.get(interaction.commandName);

if (!command) return;

try {
await command.execute(interaction);
} catch(err) {
if (err) console.error(err);

await interaction.reply({
content: "An error occurred while executing that command.",
ephemeral: true,
});
const eventFiles = fs
.readdirSync("./events")
.filter(file => file.endsWith(".js"));

for (const file of eventFiles) {
const event = require(`./events/${file}`);

if (event.once) {
client.once(event.name, (...args) => event.execute(...args, commands));
} else {
client.on(event.name, (...args) => event.execute(...args, commands));
}
});
}

client.login(process.env.TOKEN);
22 changes: 22 additions & 0 deletions events/interactionCreate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module.exports = {
name: "interactionCreate",
once: true,
async execute(interaction) {
if (!interaction.isCommand()) return;

const command = interaction.client.commands.get(interaction.commandName);

if (!command) return;

try {
await command.execute(interaction);
} catch (err) {
if (err) console.error(err);

await interaction.reply({
content: "An error occurred while executing that command.",
ephemeral: true,
});
}
}
}
38 changes: 38 additions & 0 deletions events/ready.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const { REST } = require("@discordjs/rest");
const { Routes } = require("discord-api-types/v9");
require("dotenv").config();

module.exports = {
name: "ready",
once: true,
execute(client, commands) {
console.log("Ghost is online.");

const CLIENT_ID = client.user.id;

const rest = new REST({
version: "9",
}).setToken(process.env.TOKEN);

(async () => {
try {
if (process.env.ENV === "production") {
await rest.put(Routes.applicationCommands(CLIENT_ID), {
body: commands,
});
console.log("Successfully registered commands globally.");
} else {
await rest.put(
Routes.applicationGuildCommands(CLIENT_ID, process.env.GUILD_ID),
{
body: commands,
}
);
console.log("Successfully registered commands locally.");
}
} catch (err) {
if (err) console.error(err);
}
})();
},
};

0 comments on commit f1396f3

Please sign in to comment.