Skip to content

Commit 60ab313

Browse files
committed
add changelogs + update types
1 parent 312503e commit 60ab313

19 files changed

+201
-47
lines changed

CHANGELOG.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
## [0.1.9] - 2023-12-12
6+
7+
### Changes (breaking)
8+
9+
- Update `ValidationFunctionProps` type name to `ValidationProps`.
10+
- Update `autocompleteRun` command function name to `autocomplete`.
11+
- Update `AutocompleteCommandProps` type name to `AutocompleteProps`.
12+
13+
### Deprecated
14+
15+
- `guildOnly` in command options. CommandKit no longer handles the `guildOnly` condition. Use `dm_permission` in your command `data` object instead.
16+
17+
### Fixed
18+
19+
- Broken docs links.

apps/docs/pages/docs/enums/_meta.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
{
2-
"CommandType": "CommandType",
32
"ReloadType": "ReloadType"
43
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# SlashCommandProps
2+
3+
### `client`
4+
5+
- Type: [`Client`](https://old.discordjs.dev/#/docs/discord.js/main/class/Client)
6+
7+
### `handler`
8+
9+
- Type: [`CommandKit`](/docs/typedef/CommandKit)
10+
11+
### `interaction`
12+
13+
- Type [`AutocompleteInteraction`](https://old.discordjs.dev/#/docs/discord.js/main/class/AutocompleteInteraction)

apps/docs/pages/docs/typedef/CommandObject.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
### `data`
44

5-
- Type: [`CommandData`](/typedef/CommandData)
5+
- Type: [`CommandData`](/docs/typedef/CommandData)
66

77
### `options` (optional)
88

9-
- Type: [`CommandOptions`](/typedef/CommandOptions)
9+
- Type: [`CommandOptions`](/docs/typedef/CommandOptions)
1010

1111
### `filePath`
1212

apps/docs/pages/docs/typedef/CommandOptions.mdx

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { Callout } from 'nextra/components';
2-
31
# CommandOptions
42

53
### `devOnly` (optional)
@@ -8,16 +6,6 @@ import { Callout } from 'nextra/components';
86

97
This determines whether the command should only be registered in development guilds and be executed by the bot developers.
108

11-
### `guildOnly` (optional)
12-
13-
<Callout type="error">
14-
This will soon be deprecated. Use `dm_permission` in your command `data` instead.
15-
</Callout>
16-
17-
- Type: `boolean`
18-
19-
This determines whether the command should only be handled in guilds.
20-
219
### `userPermissions` (optional)
2210

2311
- Type: [`PermissionsString[]`](https://discord.js.org/docs/packages/discord.js/main/PermissionsString:TypeAlias)

apps/docs/pages/docs/typedef/ContextMenuCommandProps.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
### `handler`
88

9-
- Type: [`CommandKit`](/typedef/CommandKit)
9+
- Type: [`CommandKit`](/docs/typedef/CommandKit)
1010

1111
### `interaction`
1212

apps/docs/pages/docs/typedef/_meta.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"AutocompleteProps": "AutocompleteProps",
23
"CommandData": "CommandData",
34
"CommandKit": "CommandKit",
45
"CommandObject": "CommandObject",

apps/docs/pages/guide/command-file-setup.mdx

Lines changed: 143 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Here's an example `/ping` slash command which replies with "Pong!"
1010

1111
<Tabs items={['CommonJS', 'ESM', 'TypeScript']}>
1212
<Tabs.Tab>
13-
```js filename="commands/misc/ping.js" copy
13+
```js filename="commands/Misc/ping.js" copy
1414
module.exports = {
1515
data: {
1616
name: 'ping',
@@ -32,7 +32,7 @@ Here's an example `/ping` slash command which replies with "Pong!"
3232
```
3333
</Tabs.Tab>
3434
<Tabs.Tab>
35-
```js filename="commands/misc/ping.js" copy
35+
```js filename="commands/Misc/ping.js" copy
3636
export const data = {
3737
name: 'ping',
3838
description: 'Pong!',
@@ -52,7 +52,7 @@ Here's an example `/ping` slash command which replies with "Pong!"
5252
```
5353
</Tabs.Tab>
5454
<Tabs.Tab>
55-
```ts filename="commands/misc/ping.ts" copy
55+
```ts filename="commands/Misc/ping.ts" copy
5656
import type { CommandData, SlashCommandProps, CommandOptions } from 'commandkit';
5757

5858
export const data: CommandData = {
@@ -82,7 +82,7 @@ Here's an example `content` command which replies with the content of the target
8282

8383
<Tabs items={['CommonJS', 'ESM', 'TypeScript']}>
8484
<Tabs.Tab>
85-
```js filename="commands/misc/content.js" copy
85+
```js filename="commands/Misc/content.js" copy
8686
const { ApplicationCommandType } = require("discord.js");
8787

8888
module.exports = {
@@ -106,7 +106,7 @@ Here's an example `content` command which replies with the content of the target
106106
```
107107
</Tabs.Tab>
108108
<Tabs.Tab>
109-
```js filename="commands/misc/content.js" copy
109+
```js filename="commands/Misc/content.js" copy
110110
import { ApplicationCommandType } from "discord.js";
111111

112112
export const data = {
@@ -128,7 +128,7 @@ Here's an example `content` command which replies with the content of the target
128128
```
129129
</Tabs.Tab>
130130
<Tabs.Tab>
131-
```ts filename="commands/misc/content.ts" copy
131+
```ts filename="commands/Misc/content.ts" copy
132132
import { type CommandData, type ContextMenuCommandProps, type CommandOptions } from "commandkit";
133133
import { ApplicationCommandType } from "discord.js";
134134

@@ -153,24 +153,158 @@ Here's an example `content` command which replies with the content of the target
153153

154154
</Tabs>
155155

156+
## Autocomplete function
157+
158+
In addition to the `run` function, you can also export an `autocomplete` function from your command file so that you don't have to create a new `"interactionCreate"` event listener.
159+
160+
Here's an example of how to use the `autocomplete` function:
161+
162+
<Tabs items={['CommonJS', 'ESM', 'TypeScript']}>
163+
<Tabs.Tab>
164+
```js filename="commands/Fun/pet.js" copy
165+
const pets = require('../data/pets.json');
166+
167+
module.exports = {
168+
data: new SlashCommandBuilder()
169+
.setName('pet')
170+
.setDescription('Find a pet from a list of pets.')
171+
.addStringOption((option) =>
172+
option
173+
.setName('pet')
174+
.setDescription('The pet to check.')
175+
.setRequired(true)
176+
.setAutocomplete(true)
177+
),
178+
179+
run: ({ interaction, client, handler }) => {
180+
const targetPetId = interaction.options.getString('pet');
181+
const targetPetObj = pets.find((pet) => pet.id === targetPetId);
182+
183+
interaction.reply(`Your pet name is ${targetPetObj.name}.`);
184+
},
185+
186+
autocomplete: ({ interaction, client, handler }) => {
187+
const focusedPetOption = interaction.options.getFocused(true);
188+
189+
const filteredChoices = pets.filter((pet) => pet.name.startsWith(focusedPetOption));
190+
191+
const results = filteredChoices.map((pet) => {
192+
return {
193+
name: `${pet.name} | ${pet.type}`,
194+
value: pet.id,
195+
};
196+
});
197+
198+
interaction.respond(results.slice(0, 25));
199+
}
200+
}
201+
```
202+
</Tabs.Tab>
203+
<Tabs.Tab>
204+
```js filename="commands/Fun/pet.js" copy
205+
import pets from '../data/pets.json';
206+
207+
export const data = new SlashCommandBuilder()
208+
.setName('pet')
209+
.setDescription('Find a pet from a list of pets.')
210+
.addStringOption((option) =>
211+
option
212+
.setName('pet')
213+
.setDescription('The pet to check.')
214+
.setRequired(true)
215+
.setAutocomplete(true)
216+
);
217+
218+
export function run({ interaction, client, handler }) {
219+
const targetPetId = interaction.options.getString('pet');
220+
const targetPetObj = pets.find((pet) => pet.id === targetPetId);
221+
222+
interaction.reply(`Your pet name is ${targetPetObj.name}.`);
223+
}
224+
225+
export function autocomplete({ interaction, client, handler }) {
226+
const focusedPetOption = interaction.options.getFocused(true);
227+
228+
const filteredChoices = pets.filter((pet) => pet.name.startsWith(focusedPetOption));
229+
230+
const results = filteredChoices.map((pet) => {
231+
return {
232+
name: `${pet.name} | ${pet.type}`,
233+
value: pet.id,
234+
};
235+
});
236+
237+
interaction.respond(results.slice(0, 25));
238+
}
239+
```
240+
</Tabs.Tab>
241+
<Tabs.Tab>
242+
```ts filename="commands/Fun/pet.ts" copy
243+
import type { CommandData, AutocompleteProps, CommandOptions } from 'commandkit';
244+
import pets from '../data/pets.json';
245+
246+
export const data = new SlashCommandBuilder()
247+
.setName('pet')
248+
.setDescription('Find a pet from a list of pets.')
249+
.addStringOption((option) =>
250+
option
251+
.setName('pet')
252+
.setDescription('The pet to check.')
253+
.setRequired(true)
254+
.setAutocomplete(true)
255+
);
256+
257+
export function run({ interaction, client, handler }: SlashCommandProps) {
258+
const targetPetId = interaction.options.getString('pet');
259+
const targetPetObj = pets.find((pet) => pet.id === targetPetId);
260+
261+
interaction.reply(`Your pet name is ${targetPetObj.name}.`);
262+
}
263+
264+
export function autocomplete({ interaction, client, handler }: AutocompleteProps) {
265+
const focusedPetOption = interaction.options.getFocused(true);
266+
267+
const filteredChoices = pets.filter((pet) => pet.name.startsWith(focusedPetOption));
268+
269+
const results = filteredChoices.map((pet) => {
270+
return {
271+
name: `${pet.name} | ${pet.type}`,
272+
value: pet.id,
273+
};
274+
});
275+
276+
interaction.respond(results.slice(0, 25));
277+
}
278+
```
279+
</Tabs.Tab>
280+
281+
</Tabs>
282+
156283
## Properties and Methods
157284

158285
### `data`
159286

160-
- Type: [`CommandData`](/typedef/CommandData) | [`SlashCommandBuilder`](https://discord.js.org/docs/packages/builders/main/SlashCommandBuilder:Class) | [`ContextMenuCommandBuilder`](https://discord.js.org/docs/packages/builders/main/ContextMenuCommandBuilder:Class)
287+
- Type: [`CommandData`](/docs/typedef/CommandData) | [`SlashCommandBuilder`](https://discord.js.org/docs/packages/builders/main/SlashCommandBuilder:Class) | [`ContextMenuCommandBuilder`](https://discord.js.org/docs/packages/builders/main/ContextMenuCommandBuilder:Class)
161288

162289
This property contains the command's structural information, and is required to register and handle commands.
163290

164291
### `run`
165292

166293
- Type: `void`
167294

168-
- Props Type: [`SlashCommandProps`](/typedef/SlashCommandProps) | [`ContextMenuCommandProps`](/typedef/ContextMenuCommandProps)
295+
- Props Type: [`SlashCommandProps`](/docs/typedef/SlashCommandProps) | [`ContextMenuCommandProps`](/docs/typedef/ContextMenuCommandProps)
169296

170297
This function will be called when the command is executed.
171298

299+
### `autocomplete`
300+
301+
- Type: `void`
302+
- Props Type: [`AutocompleteProps`](/docs/typedef/AutocompleteProps)
303+
304+
This function will be called when an autocomplete interaction event is triggered for any option set as autocomplete in this command's `data` object.
305+
172306
### `options` (optional)
173307

174-
- Type: [`CommandOptions`](/typedef/CommandOptions)
308+
- Type: [`CommandOptions`](/docs/typedef/CommandOptions)
175309

176310
This property contains the command's registration/handling behaviour.

apps/docs/pages/guide/commandkit-setup.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,4 @@ This is used to disable CommandKit's built-in validation functions. Setting this
165165
- Type: `boolean`
166166
- Default: `false`
167167
168-
This is used to change the behaviour of how CommandKit loads application commands. By default it's one-by-one while comparing changes. Setting this option to `true` will load application commands all at once on every restart, and when [reloadCommands()](/typedef/CommandKit#reloadcommands) is called.
168+
This is used to change the behaviour of how CommandKit loads application commands. By default it's one-by-one while comparing changes. Setting this option to `true` will load application commands all at once on every restart, and when [`reloadCommands()`](/docs/typedef/CommandKit#reloadcommands) is called.

apps/docs/pages/guide/installation.mdx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ import { Callout } from 'nextra/components';
33
# Installation
44

55
<div align="center" className="my-8">
6-
<img
7-
src="https://raw.githubusercontent.com/underctrl-io/commandkit/master/apps/docs/public/ckit_logo.svg"
8-
width="60%"
9-
/>
6+
<img src="/ckit_logo.svg" width="60%" />
107
<br />
118
<div className="flex items-center justify-center gap-2">
129
<a href="https://ctrl.lol/discord">

apps/docs/pages/guide/validation-file-setup.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ Validation functions are called on every command trigger, so it's important to k
4848

4949
<Tabs.Tab label="TypeScript">
5050
```ts filename="validations/cooldowns.ts" copy
51-
import type { ValidationFunctionProps } from 'commandkit';
51+
import type { ValidationProps } from 'commandkit';
5252
import cooldowns from '../cooldowns-cache';
5353

54-
export default function ({ interaction, commandObj, handler }: ValidationFunctionProps) {
54+
export default function ({ interaction, commandObj, handler }: ValidationProps) {
5555
if (cooldowns.has(`${interaction.user.id}-${commandObj.data.name}`)) {
5656
interaction.reply({
5757
content: "You're on cooldown, please wait some time before running this command again.",

apps/docs/pages/index.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export default function Home() {
4949

5050
<div className="flex items-center justify-center gap-3 mt-10 md:justify-start text-neutral-100">
5151
<Link href='/guide/installation' className="font-semibold bg-[#b079fc] py-2 px-4 rounded-full">Guide</Link>
52-
<Link href='/docs/typedef/CommandData' className="font-semibold bg-blue-500 py-2 px-4 rounded-full">Docs</Link>
52+
<Link href='/docs/typedef/AutocompleteProps' className="font-semibold bg-blue-500 py-2 px-4 rounded-full">Docs</Link>
5353
<Link href='https://github.com/underctrl-io/commandkit' className="font-semibold bg-[#fc7993] py-2 px-4 rounded-full" target="_blank" rel="noopener noreferrer">GitHub</Link>
5454
</div>
5555
</div>

packages/commandkit/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "commandkit",
33
"description": "Beginner friendly command & event handler for Discord.js",
4-
"version": "0.1.8",
4+
"version": "0.1.9",
55
"license": "MIT",
66
"main": "./dist/index.js",
77
"module": "./dist/index.mjs",

packages/commandkit/src/CommandKit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export class CommandKit {
111111
}
112112

113113
const commands = this.#data.commandHandler.commands.map((cmd) => {
114-
const { run, ...command } = cmd;
114+
const { run, autocomplete, ...command } = cmd;
115115
return command;
116116
});
117117

packages/commandkit/src/handlers/command-handler/CommandHandler.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,10 @@ export class CommandHandler {
181181

182182
if (!targetCommand) return;
183183

184-
const { data, options, run, autocompleteRun, ...rest } = targetCommand;
184+
const { data, options, run, autocomplete, ...rest } = targetCommand;
185185

186186
// Skip if autocomplete handler is not defined
187-
if (isAutocomplete && !autocompleteRun) return;
187+
if (isAutocomplete && !autocomplete) return;
188188

189189
const commandObj = {
190190
data: targetCommand.data,
@@ -238,7 +238,7 @@ export class CommandHandler {
238238
handler: this.#data.commandkitInstance,
239239
};
240240

241-
await targetCommand[isAutocomplete ? 'autocompleteRun' : 'run']!(context);
241+
await targetCommand[isAutocomplete ? 'autocomplete' : 'run']!(context);
242242
});
243243
}
244244

0 commit comments

Comments
 (0)