Move eventhandlers to separate files
This commit is contained in:
24
events/interactionCreate.js
Normal file
24
events/interactionCreate.js
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
require("dotenv").config();
|
||||||
|
const { REST } = require("@discordjs/rest");
|
||||||
|
const { Routes } = require("discord-api-types/v9")
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
name: "interactionCreate",
|
||||||
|
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.log(err);
|
||||||
|
await interaction.reply({
|
||||||
|
content: "An error occured processing the command",
|
||||||
|
ephemeral: true
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
31
events/ready.js
Normal file
31
events/ready.js
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
require("dotenv").config();
|
||||||
|
const { REST } = require("@discordjs/rest");
|
||||||
|
const { Routes } = require("discord-api-types/v9")
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
name: "ready",
|
||||||
|
once: true,
|
||||||
|
execute (client, commands) {
|
||||||
|
console.log("Bot started.");
|
||||||
|
|
||||||
|
const CLIENT_ID = client.user.id;
|
||||||
|
const rest = new REST({
|
||||||
|
version: 9
|
||||||
|
}).setToken(process.env.TOKEN);
|
||||||
|
|
||||||
|
(async () => {
|
||||||
|
try {
|
||||||
|
console.log("Registering commands...");
|
||||||
|
if(process.env.ENV === "production") {
|
||||||
|
await rest.put(Routes.applicationCommands(CLIENT_ID), {body: commands });
|
||||||
|
console.log("Global commands registered");
|
||||||
|
} else {
|
||||||
|
await rest.put(Routes.applicationGuildCommands(CLIENT_ID, process.env.GUILD_ID), {body: commands });
|
||||||
|
console.log("Local commands registered");
|
||||||
|
}
|
||||||
|
} catch (err){
|
||||||
|
if (err) console.log(err);
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
}
|
||||||
|
}
|
||||||
51
index.js
51
index.js
@@ -1,8 +1,6 @@
|
|||||||
require("dotenv").config();
|
require("dotenv").config();
|
||||||
|
|
||||||
const fs = require("fs");
|
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, Intents, Collection} = require("discord.js");
|
||||||
|
|
||||||
const client = new Client({intents: [
|
const client = new Client({intents: [
|
||||||
@@ -13,9 +11,7 @@ const client = new Client({intents: [
|
|||||||
]});
|
]});
|
||||||
|
|
||||||
const commandFiles = fs.readdirSync("./commands").filter(file => file.endsWith(".js"));
|
const commandFiles = fs.readdirSync("./commands").filter(file => file.endsWith(".js"));
|
||||||
|
|
||||||
const commands = [];
|
const commands = [];
|
||||||
|
|
||||||
client.commands = new Collection();
|
client.commands = new Collection();
|
||||||
|
|
||||||
for (const file of commandFiles) {
|
for (const file of commandFiles) {
|
||||||
@@ -24,47 +20,16 @@ for (const file of commandFiles) {
|
|||||||
client.commands.set(command.data.name, command);
|
client.commands.set(command.data.name, command);
|
||||||
}
|
}
|
||||||
|
|
||||||
client.once("ready", () => {
|
const eventFiles = fs.readdirSync("./events").filter(file => file.endsWith(".js"));
|
||||||
console.log("Bot started.");
|
for (const file of eventFiles) {
|
||||||
|
const event = require(`./events/${file}`);
|
||||||
|
|
||||||
const CLIENT_ID = client.user.id;
|
if (event.once) {
|
||||||
const rest = new REST({
|
client.once(event.name, (...args) => event.execute(...args, commands));
|
||||||
version: 9
|
} else {
|
||||||
}).setToken(process.env.TOKEN);
|
client.on(event.name, (...args) => event.execute(...args, commands));
|
||||||
|
|
||||||
(async () => {
|
|
||||||
try {
|
|
||||||
console.log("Registering commands...");
|
|
||||||
if(process.env.ENV === "production") {
|
|
||||||
await rest.put(Routes.applicationCommands(CLIENT_ID), {body: commands });
|
|
||||||
console.log("Global commands registered");
|
|
||||||
} else {
|
|
||||||
await rest.put(Routes.applicationGuildCommands(CLIENT_ID, process.env.GUILD_ID), {body: commands });
|
|
||||||
console.log("Local commands registered");
|
|
||||||
}
|
|
||||||
} catch (err){
|
|
||||||
if (err) console.log(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.log(err);
|
|
||||||
await interaction.reply({
|
|
||||||
content: "An error occured processing the command",
|
|
||||||
ephemeral: true
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
|
||||||
client.login(process.env.TOKEN);
|
client.login(process.env.TOKEN);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user