DiscordJS-v14-Template/handler/index.js

55 lines
1.7 KiB
JavaScript
Raw Normal View History

2022-07-22 18:09:13 -04:00
require("dotenv").config();
const { glob } = require("glob");
const { promisify } = require("util");
const globPromise = promisify(glob);
2024-10-03 18:00:03 -04:00
const { REST } = require('@discordjs/rest');
const Discord = require('discord.js');
2022-07-22 18:09:13 -04:00
module.exports = async (client) => {
2024-10-03 18:00:03 -04:00
const rest = new REST({ version: '10' }).setToken(process.env.TOKEN);
2022-07-22 18:09:13 -04:00
// Slash Commands
const slashCommands = await globPromise(`${process.cwd()}/commands/*/*.js`);
const arrayOfSlashCommands = [];
2024-10-03 18:00:03 -04:00
2022-07-22 18:09:13 -04:00
slashCommands.map((value) => {
const file = require(value);
const splitted = value.split("/");
const directory = splitted[splitted.length - 2];
if (!file?.name) return;
const properties = { directory, ...file };
client.slashCommands.set(file.name, properties);
if (["MESSAGE", "USER"].includes(file.type)) delete file.description;
2024-10-03 18:00:03 -04:00
// Add integration types and contexts to support user apps
const JSONCommand = {
...file,
integration_types: [0, 1], // 0 for guild, 1 for user
contexts: [0, 1, 2], // 0 for guild, 1 for DMs, 2 for GDMs and other DMs
};
arrayOfSlashCommands.push(JSONCommand);
2022-07-22 18:09:13 -04:00
});
// Events
const eventFiles = await globPromise(`${process.cwd()}/events/*.js`);
eventFiles.map((value) => require(value));
// Slash Commands Register
client.on("ready", async () => {
2024-10-03 18:00:03 -04:00
try {
// Register for all the guilds the bot is in
await rest.put(
Discord.Routes.applicationCommands(client.user.id),
{ body: arrayOfSlashCommands }
);
console.log("Successfully registered application commands.");
} catch (error) {
console.error("Error while registering application commands:", error);
}
2022-07-22 18:09:13 -04:00
});
};