0fficerSally
0fficerSally
DIAdiscord.js - Imagine an app
Created by 0fficerSally on 10/12/2024 in #djs-questions
Command Handling With TypeScript
Hello, I'm having trouble setting up command handling with TypeScript. I'm trying to follow the official guide that is directed towards JavaScript. What I Have So Far /src/index.ts
// Dependencies
import { Client, Events, GatewayIntentBits } from 'discord.js';

// Create Client Instance
const client = new Client({
intents: [
GatewayIntentBits.Guilds
]
});

// Log Ready Event
client.once(Events.ClientReady, (readyClient: Client<true>) => {
console.group("[🟢] Client Ready");

console.info(`Username: "${readyClient.user.username}"`);
console.info(`Guilds: ${readyClient.guilds.cache.size}`);
console.info(`Users: ${readyClient.users.cache.size}`);

console.groupEnd();
});

// Connect Client
client.login(process.env.DISCORD_BOT_TOKEN);
// Dependencies
import { Client, Events, GatewayIntentBits } from 'discord.js';

// Create Client Instance
const client = new Client({
intents: [
GatewayIntentBits.Guilds
]
});

// Log Ready Event
client.once(Events.ClientReady, (readyClient: Client<true>) => {
console.group("[🟢] Client Ready");

console.info(`Username: "${readyClient.user.username}"`);
console.info(`Guilds: ${readyClient.guilds.cache.size}`);
console.info(`Users: ${readyClient.users.cache.size}`);

console.groupEnd();
});

// Connect Client
client.login(process.env.DISCORD_BOT_TOKEN);
/src/commands/utility/ping.ts
// Dependencies
import { CommandInteraction, SlashCommandBuilder } from 'discord.js';

// Export Command
module.exports = {
data: new SlashCommandBuilder()
.setName("ping")
.setDescription("Ping the app."),
async execute(interaction: CommandInteraction) {
await interaction.reply("Pong!");
},
};
// Dependencies
import { CommandInteraction, SlashCommandBuilder } from 'discord.js';

// Export Command
module.exports = {
data: new SlashCommandBuilder()
.setName("ping")
.setDescription("Ping the app."),
async execute(interaction: CommandInteraction) {
await interaction.reply("Pong!");
},
};
Where I Am Stuck
We recommend attaching a .commands property to your client instance so that you can access your commands in other files. The rest of the examples in this guide will follow this convention. For TypeScript users, we recommend extending the base Client class to add this property, casting, or augmenting the module type.
I'd like to ask if what I currently have is good so far, and where I go from here. I already tried a couple of different things over the last few days, but I'm starting over and immediately asking here to be sure I understand what I'm doing.
9 replies