From bb8fb06bcdafb0b45f0592bd069ee7d6ab3bf1b2 Mon Sep 17 00:00:00 2001 From: omar-sobhy Date: Wed, 22 May 2024 20:20:31 +0400 Subject: [PATCH] WIP: Add TypeScript examples --- guide/additional-features/cooldowns.md | 47 +++++++++++++++++++++++ guide/creating-your-bot/event-handling.md | 24 ++++++------ 2 files changed, 58 insertions(+), 13 deletions(-) diff --git a/guide/additional-features/cooldowns.md b/guide/additional-features/cooldowns.md index 6c20a1719..91ce75345 100644 --- a/guide/additional-features/cooldowns.md +++ b/guide/additional-features/cooldowns.md @@ -28,6 +28,39 @@ In your main file, initialize a [Collection](/additional-info/collections.md) to client.cooldowns = new Collection(); ``` +::::: ts-tip +You'll also need to edit the definitions for `ExtendedClient` and `SlashCommand`: +:::: code-group +::: code-group-item src/types/ExtendedClient.ts +```ts +import { Client, ClientOptions, Collection } from 'discord.js'; +import { SlashCommand } from '../types/SlashCommand'; + +export class ExtendedClient extends Client { + constructor( + options: ClientOptions, + public commands: Collection = new Collection(), + public cooldowns: Collection> = new Collection(), + ) { + super(options); + } +} +``` +::: +::: code-group-item src/types/SlashCommand.ts +```ts{6} +import { ChatInputCommandInteraction, SlashCommandBuilder } from 'discord.js'; + +export interface SlashCommand { + data: SlashCommandBuilder; + execute: (interaction: ChatInputCommandInteraction) => Promise; + cooldown?: number; +} +``` +::: +:::: +::::: + The key will be the command names, and the values will be Collections associating the user's id (key) to the last time (value) this user used this command. Overall the logical path to get a user's last usage of a command will be `cooldowns > command > user > timestamp`. In your `InteractionCreate` event, add the following code: @@ -55,6 +88,20 @@ try { } ``` +::: ts-tip +You'll need to use a type assertion on `interaction.client` to get the correct type: +```ts +const { cooldowns } = interaction.client as ExtendedClient; +``` +::: + +::: ts-tip +You may need to add non-null assertions around the code (notice the `!` at the end of the line): +```ts +const timestamps = cooldowns.get(command.data.name)!; +``` +::: + You check if the `cooldowns` Collection already has an entry for the command being used. If this is not the case, you can add a new entry, where the value is initialized as an empty Collection. Next, create the following variables: 1. `now`: The current timestamp. diff --git a/guide/creating-your-bot/event-handling.md b/guide/creating-your-bot/event-handling.md index 019e0ba56..3f8f34699 100644 --- a/guide/creating-your-bot/event-handling.md +++ b/guide/creating-your-bot/event-handling.md @@ -159,12 +159,13 @@ for (const folder of commandFolders) { client.login(token); ``` ::: -::: code-group-item src/types/Event.ts +::: code-group-item src/types/EventHandler.ts ```ts import { BaseInteraction, Events } from 'discord.js'; -export default interface Event { +export interface EventHandler { name: Events; + once?: boolean; execute: (...args: unknown[]) => Promise; } ``` @@ -172,25 +173,23 @@ export default interface Event { ::: code-groupitem src/events/ready.ts ```ts import { Events } from 'discord.js'; -import { Event } from '../../types/Event'; +import { EventHandler } from '../types/EventHandler'; -const event: Event = { +const eventHandler: EventHandler = { name: Events.ClientReady, once: true, execute(client) { console.log(`Ready! Logged in as ${client.user.tag}`); }, }; - - ``` ::: ::: code-group-item events/ready.ts ```js import { Events } from 'discord.js'; -import { Event } from '../../types/Event'; +import { EventHandler } from '../../types/EventHandler'; -const event: Event = { +const eventHandler: EventHandler = { name: Events.ClientReady, once: true, execute(client) { @@ -198,15 +197,15 @@ const event: Event = { }, }; -export default event; +export default eventHandler; ``` ::: ::: code-group-item src/events/interactionCreate.ts ```ts import { Events } from 'discord.js'; -import { Event } from '../../types/Event'; +import { EventHandler } from '../types/EventHandler'; -const event: Event = { +const eventHandler: EventHandler = { name: Events.InteractionCreate, async execute(interaction) { if (!interaction.isChatInputCommand()) return; @@ -231,7 +230,7 @@ const event: Event = { }, } -export default event; +export default eventHandler; ``` ::: :::: @@ -302,7 +301,6 @@ const config = JSON.parse(configRaw); assertObjectIsConfig(config); - const { token } = config; // ExtendedClient's second `commands` parameter defaults to an empty Collection