Skip to content

Commit

Permalink
WIP: Add TypeScript examples
Browse files Browse the repository at this point in the history
  • Loading branch information
omar-sobhy committed May 22, 2024
1 parent 412297f commit bb8fb06
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 13 deletions.
47 changes: 47 additions & 0 deletions guide/additional-features/cooldowns.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, SlashCommand> = new Collection(),
public cooldowns: Collection<string, Collection<string, number>> = 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<void>;
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:
Expand Down Expand Up @@ -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.
Expand Down
24 changes: 11 additions & 13 deletions guide/creating-your-bot/event-handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,54 +159,53 @@ 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<void>;
}
```
:::
::: 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) {
console.log(`Ready! Logged in as ${client.user.tag}`);
},
};

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;
Expand All @@ -231,7 +230,7 @@ const event: Event = {
},
}

export default event;
export default eventHandler;
```
:::
::::
Expand Down Expand Up @@ -302,7 +301,6 @@ const config = JSON.parse(configRaw);

assertObjectIsConfig(config);


const { token } = config;

// ExtendedClient's second `commands` parameter defaults to an empty Collection
Expand Down

0 comments on commit bb8fb06

Please sign in to comment.