Skip to content

Commit

Permalink
Merge pull request #11 from Mimickal/use-ts
Browse files Browse the repository at this point in the history
  • Loading branch information
Mimickal authored May 22, 2023
2 parents 625a1e0 + a134549 commit 20fc7f8
Show file tree
Hide file tree
Showing 19 changed files with 2,086 additions and 1,192 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# Node things
node_modules
.nyc_output
lib

# My dev things
todo
4 changes: 4 additions & 0 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"loader": "ts-node/esm",
"experimental-specifier-resolution": "node"
}
45 changes: 39 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
src="https://www.gnu.org/graphics/lgplv3-with-text-154x68.png">
</a>

_Now with 100% more TypeScript!_

_**NOTE: version 2.x of this library supports discord.js v14. If you still need
v13 support, use an older 1.x version of this library.**_

Expand Down Expand Up @@ -40,7 +42,7 @@ It also supports some additional "option" types

This library adds a new builder `SlashCommandRegistry` that serves as the
entry point for defining all of your commands. Existing builders from
[@discordjs/builders](https://www.npmjs.com/package/@discordjs/builders)
[`@discordjs/builders`](https://www.npmjs.com/package/@discordjs/builders)
still work as you expect, but there's a new function added to all of them:
`.setHandler(handler)`. The handler is a callback function that expects a
Discord.js `Interaction` instance. The `SlashCommandRegistry` will figure out
Expand Down Expand Up @@ -120,6 +122,32 @@ npm exec register src/commands.js -- --app 1234 --token path/to/token_file
npm exec register src/commands.js -- --app 1234 --token "my token text"
```

### Using the script with TypeScript

If you're using TypeScript and aren't transpiling to JavaScript (e.g. running
your bot with `ts-node`), you can still use this script. This library ships
with a separate, stand-alone TypeScript version of this register script.

Then you can run the register script from `node_modules` like this:
```sh
npx ts-node --esm --skipIgnore --logError --compilerOptions '{"esModuleInterop":true}' node_modules/discord-command-registry/src/register.ts
```

If you'd rather remove all the `ts-node` flags, you can also specify these
options in your `tsconfig.json`, like this:
```json
{
"ts-node": {
"esm": true,
"skipIgnore": true,
"logError": true,
"compilerOptions": {
"esModuleInterop": true,
}
}
}
```

### Using `SlashCommandRegistry.registerCommands()`

If you want more control over registration, the `SlashCommandRegistry` can
Expand Down Expand Up @@ -210,14 +238,19 @@ some flexibility if you have many commands that all use similar code.
## Additional option types

Discord (and Discord.js) does not currently support command options for things
like Applications. This library provides functions to approximate these
additional option types:
like Applications. This library provides builders and functions to approximate
these additional option types:

(Note: these are registered as string options under the hood)

- `SlashCommandCustomOption`
- `addApplicationOption(custom_option)`
- `addEmojiOption(custom_option)`
- `getApplication(interaction, option_name, required=false)`
- `getEmoji(interaction, option_name, required=false)`

To use these, register the option as a string option, then use the
`Options.getX(...)` helpers to retrieve the value.
To use these, register the option using the appropriate custom builder, then
use the `Options.getX(...)` helpers to retrieve the value.

For example, this is a functional example of an Application option:

Expand All @@ -232,7 +265,7 @@ const commands = new SlashCommandRegistry()
.addName('mycmd')
.addDescription('Example command that has an application option')
// Add your application option as a string option
.addStringOption(option => option
.addApplicationOption(option => option
.setName('app')
.setDescription('An application ID')
)
Expand Down
Loading

0 comments on commit 20fc7f8

Please sign in to comment.