Removed Custom Builder. Cleaned-Up package.json. Make the bot partially work. Command register was added but, has to be fixed.

This commit is contained in:
Ahmad Khan 2023-09-21 20:15:38 -04:00
parent 169ef72bfd
commit 4e818fb965
14 changed files with 152 additions and 360 deletions

15
source/commands/ping.ts Normal file
View file

@ -0,0 +1,15 @@
import { SlashCommandBuilder } from 'discord.js';
interface Command {
data: SlashCommandBuilder;
execute: (interaction: any) => Promise<void>;
}
export const command: Command = {
data: new SlashCommandBuilder().setName('ping').setDescription('Replies with Pong!'),
execute: async (interaction) => {
await interaction.reply('Pong!');
},
};
export default command;

13
source/commands/server.ts Normal file
View file

@ -0,0 +1,13 @@
import { SlashCommandBuilder } from 'discord.js';
interface Command {
data: SlashCommandBuilder;
execute: (interaction: any) => Promise<void>;
}
export const command: Command = {
data: new SlashCommandBuilder().setName('server').setDescription('Provides information about the server.'),
execute: async (interaction) => {
await interaction.reply(`This server is ${interaction.guild.name} and has ${interaction.guild.memberCount} members.`);
},
};

13
source/commands/user.ts Normal file
View file

@ -0,0 +1,13 @@
import { SlashCommandBuilder } from 'discord.js';
interface Command {
data: SlashCommandBuilder;
execute: (interaction: any) => Promise<void>;
}
export const command: Command = {
data: new SlashCommandBuilder().setName('user').setDescription('Provides information about the user.'),
execute: async (interaction) => {
await interaction.reply(`This command was run by ${interaction.user.username}, who joined on ${interaction.member.joinedAt}.`);
},
};

View file

@ -0,0 +1,5 @@
{
"token": "DISCORD_BOT_API_KEY",
"application_client_id": "DISCORD_BOT_ID",
"guild_id": "DISCORD_SERVER_ID"
}

View file

@ -1,17 +1,70 @@
// Require the necessary discord.js classes
import { Client, Events, GatewayIntentBits } from 'discord.js';
import config from '../config.json' assert { type: 'json' };
import fs from'node:fs';
import path from 'node:path';
import { Client, Collection, Events, GatewayIntentBits } from'discord.js';
import config from './config.json' assert { type: 'json' };
import { deployCommands } from './util/deployCommand.js';
const { token } = config;
const { token, application_client_id, guild_id } = config;
// Create a new client instance
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
const client: any = new Client({ intents: [GatewayIntentBits.Guilds] });
client.commands = new Collection();
try {
const __dirname = path.resolve();
const commandsPath = path.join(__dirname, '/target/commands/');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join('file://', commandsPath, file);
const commandModule = await import(filePath);
const command = commandModule.default;
if (command instanceof Object && 'data' in command && 'execute' in command) {
client.commands.set(command.data.name, command);
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
} catch (error: any) {
console.log(`Error while getting commands up: ${error}`)
}
try {
await deployCommands({token, guild_id, application_client_id});
} catch (error: any) {
console.log(`Error while registering commands: ${error}`)
}
// When the client is ready, run this code (only once)
// We use 'c' for the event parameter to keep it separate from the already defined 'client'
client.once(Events.ClientReady, c => {
client.once(Events.ClientReady, (c: any) => {
console.log(`Ready! Logged in as ${c.user.tag}`);
});
client.on(Events.InteractionCreate, async (interaction: any) => {
if (!interaction.isChatInputCommand()) return;
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!', ephemeral: true });
} else {
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
}
});
// Log in to Discord with your client's token
client.login(token);

View file

@ -0,0 +1,44 @@
import { REST, Routes } from 'discord.js';
import fs from 'node:fs';
import path from 'node:path';
export async function deployCommands({token, guildId, clientId}: any) {
const commands = [];
const __dirname = path.resolve();
const commandsPath = path.join(__dirname, '/target/commands/');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
// Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment
for (const file of commandFiles) {
const filePath = path.join('file://', commandsPath, file);
const command = await import(filePath);
if (command instanceof Object && '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.`);
}
}
// Construct and prepare an instance of the REST module
const rest = new REST().setToken(token);
// and deploy your commands!
(async () => {
try {
console.log(`Started refreshing ${commands.length} application (/) commands.`);
// The put method is used to fully refresh all commands in the guild with the current set
const data: any = await rest.put(
Routes.applicationGuildCommands(clientId, guildId),
{ body: commands },
);
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
// And of course, make sure you catch and log any errors!
console.error(error);
}
})();
}