Initial commit

This commit is contained in:
CL TheDreWen
2025-10-28 11:40:35 +01:00
committed by GitHub
commit c41ef18355
10 changed files with 800 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
import { CommandInteraction, SlashCommandBuilder } from "discord.js";
export default {
data: new SlashCommandBuilder()
.setName('ping')
.setDescription('Pong again!'),
async execute(interaction : CommandInteraction) {
await interaction.reply('Pong !');
}
}

72
src/deploy-commands.ts Normal file
View File

@@ -0,0 +1,72 @@
import path from "path";
import fs from "fs";
import { configDotenv } from "dotenv";
import { REST, Routes } from "discord.js";
configDotenv();
const commands: any[] = [];
const foldersPath = path.join(__dirname, 'commands');
if (!fs.existsSync(foldersPath)) {
console.log('[ERROR] Commands directory not found at:', foldersPath);
process.exit(1);
}
const commandFolders = fs.readdirSync(foldersPath);
for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
if (!fs.statSync(commandsPath).isDirectory()) continue;
const commandFiles = fs.readdirSync(commandsPath).filter(file =>
file.endsWith('.js')
);
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
try {
const command = require(filePath);
const commandModule = command.default || command;
if ('data' in commandModule && 'execute' in commandModule) {
commands.push(commandModule.data.toJSON());
console.log(`[SUCCESS] Loaded command: ${commandModule.data.name}`);
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
} catch (error) {
console.log(`[ERROR] Failed to load command at ${filePath}:`, error);
}
}
}
if (!process.env.TOKEN) {
console.error('[ERROR] TOKEN is not defined in environment variables');
process.exit(1);
}
if (!process.env.CLIENT_ID) {
console.error('[ERROR] CLIENT_ID is not defined in environment variables');
process.exit(1);
}
const rest = new REST().setToken(process.env.TOKEN);
(async () => {
try {
console.log(`Started refreshing ${commands.length} application (/) commands.`);
const data = await rest.put(
Routes.applicationCommands(process.env.CLIENT_ID as string),
{ body: commands },
) as any[];
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
console.error('[ERROR] Failed to deploy commands:', error);
process.exit(1);
}
})();

66
src/index.ts Normal file
View File

@@ -0,0 +1,66 @@
import { Client, Collection, Events, GatewayIntentBits, MessageFlags } from "discord.js";
import { configDotenv } from "dotenv";
import path from "path";
import fs from "fs";
configDotenv();
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
]
});
// @ts-expect-error
client.commands = new Collection();
const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);
for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
const commandModule = command.default || command;
if ('data' in commandModule && 'execute' in commandModule) {
// @ts-expect-error
client.commands.set(commandModule.data.name, commandModule);
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
}
client.on(Events.InteractionCreate, async interaction => {
if (!interaction.isChatInputCommand()) return;
// @ts-expect-error
const command = interaction.client.commands.get(interaction.commandName);
if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({ content: 'There was an error while executing this command!', flags: MessageFlags.Ephemeral });
} else {
await interaction.reply({ content: 'There was an error while executing this command!', flags: MessageFlags.Ephemeral });
}
}
});
client.once(Events.ClientReady, readyClient => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
});
client.login(process.env.TOKEN)