0% found this document useful (0 votes)
9 views2 pages

Deploy Commands

This document is a TypeScript script for deploying Discord bot commands using the Discord.js library. It loads environment variables, validates them, reads command files from a specified directory, and registers the commands either globally or for a specific guild based on the presence of a GUILD_ID. The script also handles the deletion of existing commands before registering the new ones and logs the process to the console.

Uploaded by

adansaprudin971
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Deploy Commands

This document is a TypeScript script for deploying Discord bot commands using the Discord.js library. It loads environment variables, validates them, reads command files from a specified directory, and registers the commands either globally or for a specific guild based on the presence of a GUILD_ID. The script also handles the deletion of existing commands before registering the new ones and logs the process to the console.

Uploaded by

adansaprudin971
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import { REST, Routes } from 'discord.

js';
import { config } from 'dotenv';
import * as fs from 'fs';
import * as path from 'path';

// Load environment variables


config();

// Validate required environment variables


if (!process.env.DISCORD_TOKEN || !process.env.CLIENT_ID) {
console.error('Missing required environment variables. Please check your .env
file.');
process.exit(1);
}

// Check if GUILD_ID is set for guild-specific deployment


if (!process.env.GUILD_ID) {
console.warn('GUILD_ID environment variable is not set. Commands will be
registered globally.');
console.warn('To register commands to a specific server, set the GUILD_ID in your
.env file.');
}

// Create an array to hold command data


const commands: any[] = [];

// Get the path to the commands directory


const commandsPath = path.join(__dirname, 'commands');

// Read all command files


const commandFiles = fs.readdirSync(commandsPath).filter(file =>
file.endsWith('.js') || file.endsWith('.ts')
);

// Load each command file and add its data to the commands array
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);

if ('data' in command && 'execute' in command) {


commands.push(command.data.toJSON());
console.log(`Added command: ${command.data.name}`);
} else {
console.warn(`The command at ${filePath} is missing required "data" or
"execute" property.`);
}
}

// Function to deploy commands


async function deployCommands() {
try {
// Create a new REST instance and set the token
const rest = new REST({ version: '10' }).setToken(process.env.DISCORD_TOKEN as
string);

// First, delete all existing commands


try {
console.log('Deleting all existing application commands...');
if (process.env.GUILD_ID) {
// Delete guild commands if GUILD_ID is provided
await rest.put(
Routes.applicationGuildCommands(process.env.CLIENT_ID as string,
process.env.GUILD_ID as string),
{ body: [] }
);
console.log(`Successfully deleted all guild commands for guild $
{process.env.GUILD_ID}.`);
} else {
// Delete global commands if no GUILD_ID is provided
await rest.put(
Routes.applicationCommands(process.env.CLIENT_ID as string),
{ body: [] }
);
console.log('Successfully deleted all global application commands.');
}
} catch (error) {
console.error('Error deleting existing commands:', error);
}

// Now register new commands


console.log(`Started registering ${commands.length} application (/)
commands.`);

let data: any[];

if (process.env.GUILD_ID) {
// Register commands to a specific guild
data = await rest.put(
Routes.applicationGuildCommands(
process.env.CLIENT_ID as string,
process.env.GUILD_ID as string
),
{ body: commands }
) as any[];

console.log(`Successfully registered ${data.length} guild commands for guild


${process.env.GUILD_ID}.`);
} else {
// Register commands globally if no GUILD_ID is provided
data = await rest.put(
Routes.applicationCommands(process.env.CLIENT_ID as string),
{ body: commands }
) as any[];

console.log(`Successfully registered ${data.length} global application


commands.`);
}

} catch (error) {
console.error('Error deploying commands:', error);
}
}

// Execute the deployment


deployCommands();

You might also like