Haha-Yes/scripts/deploy-commands.js
2024-07-07 00:55:15 +02:00

44 lines
1.8 KiB
JavaScript

import { REST } from '@discordjs/rest';
import { Routes } from 'discord-api-types/v9';
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import dotenv from 'dotenv';
dotenv.config();
const { clientId, guildId, token } = process.env;
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const commands = [];
const categoryPath = fs.readdirSync(`${__dirname}/../commands`);
for (let i = 0; i < categoryPath.length; i++) {
const commandsPath = path.join(`${__dirname}/../commands`, categoryPath[i]);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = await import(pathToFileURL(filePath));
if (command.default.integration_types) {
Object.assign(command.default.data, { integration_types: command.default.integration_types });
Object.assign(command.default.data, { contexts: [0, 1, 2] });
}
commands.push(command.default.data.toJSON());
}
}
const rest = new REST({ version: '9' }).setToken(token);
if (process.argv[2] === 'global') {
rest.put(Routes.applicationCommands(clientId), { body: commands })
.then(() => console.log('Successfully registered application commands globally.'))
.catch(console.error);
}
else if (process.argv[2] === 'delete') {
rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: [] })
.then(() => console.log('Successfully deleted all guild commands.'))
.catch(console.error);
}
rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: commands })
.then(() => console.log(`Successfully registered application commands for the guild ${guildId}.`))
.catch(console.error);