Haha-Yes/scripts/deploy-commands.js
2022-10-13 16:32:58 +02:00

39 lines
1.5 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 } 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(filePath);
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);