Added main files

This commit is contained in:
Ahmad Khan 2023-09-15 21:47:33 -04:00
parent 7dfe149a2b
commit 0c96eed83a
8 changed files with 648 additions and 0 deletions

View file

@ -0,0 +1,9 @@
import { SlashCommandBuilder, CommandInteraction } from 'discord.js';
export const data = new SlashCommandBuilder()
.setName('ping')
.setDescription('Replies with Pong!');
export async function execute(interaction: CommandInteraction) {
return interaction.reply('Pong!');
}

43
src/deploy-command.ts Normal file
View file

@ -0,0 +1,43 @@
import { REST } from 'discord.js';
import fs from 'fs';
import path from 'path';
const commands: any = [];
const __dirname = path.dirname(new URL(import.meta.url).pathname);
const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);
export async function registerCommands(clientId: any, guildId: any, token: any) {
for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.ts'));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = await import(filePath);
if ('data' in command && 'execute' in command) {
commands.push(command.data.toJSON());
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
}
const rest = new REST().setToken(token);
(async () => {
try {
console.log(`Started refreshing ${commands.length} application (/) commands.`);
const data: any = await rest.put(
`/applications/${clientId}/guilds/${guildId}/commands`,
{ body: commands }
);
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
console.error(error);
}
})();
}
export default registerCommands;

64
src/index.ts Normal file
View file

@ -0,0 +1,64 @@
import fs from 'fs';
import path from 'path';
import { Client, Collection, GatewayIntentBits, Interaction } from 'discord.js';
import { registerCommands } from './deploy-command.js';
import config from './config.json' assert { type: 'json' };
const { clientId, guildId, token } = config;
class CustomClient extends Client {
commands: Collection<any, any> = new Collection();
}
const client = new CustomClient({ intents: [GatewayIntentBits.Guilds] });
async function initializeCommands() {
const foldersPathBefore = path.join(__dirname, 'commands');
const foldersPath = foldersPathBefore.slice(1);
console.log(foldersPath)
try {
const commandFiles = await fs.promises.readdir(foldersPath);
for (const file of commandFiles) {
const command = require(path.join(foldersPath, file));
client.commands.set(command.name, command);
}
console.log('Commands initialized successfully!');
} catch (error) {
console.error('Error initializing commands:', error);
}
}
initializeCommands();
try {
registerCommands(clientId, guildId, token);
} catch (error) {
console.error("Error registering slash commands:", error);
}
client.once('ready', () => {
console.log('Ready!');
});
client.on('interactionCreate', async (interaction: Interaction) => {
if (!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) 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!', ephemeral: true });
} else {
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
}
});
client.login(token);