How to register slash commands in Typescript

I do apologize for asking a simple and basic question here, but I've already searched for it here and google but couldn't find an appropriate solution to apply to my code... This is my code to register events to my bot and I didn't write this code by myself but it was from a tutorial video on Youtube. I don't demand to teach me the full process to register slash commands, but please just give me some tiny hints! + I want to make it global commands! client.ts
import { Client, GatewayIntentBits } from 'discord.js';
import { registerEvents } from './utils/index.js';
import Events from './events/index.js'
import Keys from './keys.js';

const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
]
});

registerEvents(client, Events);

client.login(Keys.clientToken).catch((err) => {
console.error('[Login Error]', err);
process.exit(1);
});
import { Client, GatewayIntentBits } from 'discord.js';
import { registerEvents } from './utils/index.js';
import Events from './events/index.js'
import Keys from './keys.js';

const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
]
});

registerEvents(client, Events);

client.login(Keys.clientToken).catch((err) => {
console.error('[Login Error]', err);
process.exit(1);
});
index.ts
import { Event } from '../utils/index.js';
import ready from './ready.js';
import message from './message.js';
import login from './login.js'

export default [
ready,
message,
login,
] as Event[];
import { Event } from '../utils/index.js';
import ready from './ready.js';
import message from './message.js';
import login from './login.js'

export default [
ready,
message,
login,
] as Event[];
message.ts
import { event, Events } from '../utils/index.js';

export default event(Events.InteractionCreate, async ({ client, log }, interaction) => {
if (!interaction.isCommand()) return;

if (interaction.commandName === 'hey') {
await interaction.reply('hi');
}
})
import { event, Events } from '../utils/index.js';

export default event(Events.InteractionCreate, async ({ client, log }, interaction) => {
if (!interaction.isCommand()) return;

if (interaction.commandName === 'hey') {
await interaction.reply('hi');
}
})
events.ts
import type { ClientEvents, Awaitable, Client } from 'discord.js';

// Export events enum through here to reduce the amounf of imports.
export { Events } from 'discord.js';

export type LogMethod = (...args: unknown[] ) => void;
export type EventKeys = keyof ClientEvents;

// Props that will be passed through the event callback.
export interface EventProps {
client: Client;
log: LogMethod;
}

export type EventCallback<T extends EventKeys> = (
props: EventProps,
...args: ClientEvents[T]
) => Awaitable<unknown>;

// Internal struct that represents an event.
export interface Event<T extends EventKeys = EventKeys> {
key: T;
callback: EventCallback<T>;
}

// Create an event struct.
export function event<T extends EventKeys>(key: T, callback: EventCallback<T>): Event<T> {
return {key, callback};
}

// Registers events to the client.
export function registerEvents(client: Client, events: Event[]): void {
for(const {key, callback} of events) {
client.on(key, (...args) => {
// Create a new log method for this event.
const log = console.log.bind(console, `[Event: ${key}]`)

// Try to catch uncaught errors.
try {
callback({ client, log }, ...args);
} catch(err) {
log('[Uncaught Error]', err);
}

})
}
}
import type { ClientEvents, Awaitable, Client } from 'discord.js';

// Export events enum through here to reduce the amounf of imports.
export { Events } from 'discord.js';

export type LogMethod = (...args: unknown[] ) => void;
export type EventKeys = keyof ClientEvents;

// Props that will be passed through the event callback.
export interface EventProps {
client: Client;
log: LogMethod;
}

export type EventCallback<T extends EventKeys> = (
props: EventProps,
...args: ClientEvents[T]
) => Awaitable<unknown>;

// Internal struct that represents an event.
export interface Event<T extends EventKeys = EventKeys> {
key: T;
callback: EventCallback<T>;
}

// Create an event struct.
export function event<T extends EventKeys>(key: T, callback: EventCallback<T>): Event<T> {
return {key, callback};
}

// Registers events to the client.
export function registerEvents(client: Client, events: Event[]): void {
for(const {key, callback} of events) {
client.on(key, (...args) => {
// Create a new log method for this event.
const log = console.log.bind(console, `[Event: ${key}]`)

// Try to catch uncaught errors.
try {
callback({ client, log }, ...args);
} catch(err) {
log('[Uncaught Error]', err);
}

})
}
}
4 Replies
d.js toolkit
d.js toolkit9mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
Astronaut
Astronaut9mo ago
It wasn't like this before I try to apply this(https://stackoverflow.com/questions/68431146/use-slash-commands-in-all-servers-where-have-a-bot-without-guildid-discord-js-v) solution to my code... thats my fault I put the wrong code on my question... Here is the previous one which replies to a certain message without slash command. message.ts
import { event, Events } from '../utils/index.js';

export default event(Events.MessageCreate, ({ log }, msg) => {
if(msg.content == 'ping') {
return msg.reply('pong');
}
})
import { event, Events } from '../utils/index.js';

export default event(Events.MessageCreate, ({ log }, msg) => {
if(msg.content == 'ping') {
return msg.reply('pong');
}
})
Stack Overflow
Use Slash Commands in all servers where have a bot without GuildID ...
I want people to be able to use Slash Commands against my bot on any server, as long as the bot is there. I have further granted the bot application.commands permission. I was referencing this answ...
d.js docs
d.js docs9mo ago
guide Creating Your Bot: Command handling read more
Astronaut
Astronaut9mo ago
I tried to follow this guide but it didn't seem like using Typescript... Maybe I can just make another project with Javascript so I can follow the guide but I'm using Typescript not just because of making a bot to use in discord but also wanted to improve my Typescript skill and comprehension of Javascript and Typescript...