|
1 | 1 | [VERSION]
|
2 | 2 |
|
3 |
| -> **Warning** |
4 |
| -> This update contains breaking changes! |
| 3 | +## Sub Command Support [#14] |
5 | 4 |
|
6 |
| -In order to make the code of ken more type-safe, I am now using [safepool](https://github.com/zekrotja/safepool) |
7 |
| -instead of `sync.Pool` which uses generic type parameters for ensuring type safety. Therefore, the minimum |
8 |
| -required module version has been bumped to `go1.18`. You might need to upgrade your project accordingly to |
9 |
| -be able to use ken. |
| 5 | +Finally, you can now register and use sub command groups! |
10 | 6 |
|
11 |
| -## Autocomplete Support [#18] |
12 |
| - |
13 |
| -[Command option autocomplete](https://discord.com/developers/docs/interactions/application-commands#autocomplete) |
14 |
| -support has now been added to ken. |
15 |
| - |
16 |
| -Simply enable autocomplete on your command option by setting the `Autocomplete` property to `true`. |
| 7 | +Simply add a sub command option to your command in the `Options` implementation of your command. |
17 | 8 |
|
18 | 9 | *Example:*
|
19 | 10 | ```go
|
20 |
| -func (c *TestCommand) Options() []*discordgo.ApplicationCommandOption { |
| 11 | +func (c *SubsCommand) Options() []*discordgo.ApplicationCommandOption { |
21 | 12 | return []*discordgo.ApplicationCommandOption{
|
22 | 13 | {
|
23 |
| - Type: discordgo.ApplicationCommandOptionString, |
24 |
| - Name: "language", |
25 |
| - Required: true, |
26 |
| - Description: "Choose a programming language.", |
27 |
| - Autocomplete: true, |
| 14 | + Type: discordgo.ApplicationCommandOptionSubCommandGroup, |
| 15 | + Name: "group", |
| 16 | + Description: "Some sub command gorup", |
| 17 | + Options: []*discordgo.ApplicationCommandOption{ |
| 18 | + // ... |
| 19 | + }, |
28 | 20 | },
|
29 | 21 | }
|
30 | 22 | }
|
31 | 23 | ```
|
32 | 24 |
|
33 |
| -Now, you simply need to implement the [`AutocompleteCommand`](https://pkg.go.dev/github.com/zekrotja/ken#Command) |
34 |
| -on your command. |
| 25 | +Now, you can add a [`SubCommandGroup`](https://pkg.go.dev/github.com/zekrotja/ken#SubCommandGroup) handler to your `HandleSubCommands` method to register sub commands in the group. |
35 | 26 |
|
36 | 27 | *Example:*
|
37 | 28 | ```go
|
38 |
| -func (c *TestCommand) Autocomplete(ctx *ken.AutocompleteContext) ([]*discordgo.ApplicationCommandOptionChoice, error) { |
39 |
| - input, ok := ctx.GetInput("language") |
40 |
| - |
41 |
| - if !ok { |
42 |
| - return nil, nil |
43 |
| - } |
44 |
| - |
45 |
| - choises := make([]*discordgo.ApplicationCommandOptionChoice, 0, len(programmingLanguages)) |
46 |
| - input = strings.ToLower(input) |
47 |
| - |
48 |
| - for _, lang := range programmingLanguages { |
49 |
| - if strings.HasPrefix(lang[1], input) { |
50 |
| - choises = append(choises, &discordgo.ApplicationCommandOptionChoice{ |
51 |
| - Name: lang[0], |
52 |
| - Value: lang[0], |
53 |
| - }) |
54 |
| - } |
55 |
| - } |
56 |
| - |
57 |
| - return choises, nil |
| 29 | +func (c *SubsCommand) Run(ctx ken.Context) (err error) { |
| 30 | + err = ctx.HandleSubCommands( |
| 31 | + ken.SubCommandGroup{"group", []ken.CommandHandler{ |
| 32 | + ken.SubCommandHandler{"one", c.one}, |
| 33 | + ken.SubCommandHandler{"two", c.two}, |
| 34 | + }}, |
| 35 | + ) |
| 36 | + return |
58 | 37 | }
|
59 | 38 | ```
|
60 | 39 |
|
61 |
| -> The full example can be found in [examples/autocomplete](https://github.com/zekroTJA/ken/tree/master/examples/autocomplete). |
| 40 | +> The full example can be found in [examples/subcommandgroups](https://github.com/zekroTJA/ken/tree/master/examples/subcommandgroups). |
| 41 | +
|
| 42 | +## Minor Changes |
62 | 43 |
|
63 |
| -To properly handle errors occuring during autocomplete handling, a new command handler hook `OnEventError` has been added to the [`Options`](https://pkg.go.dev/github.com/zekrotja/ken#Options). It will be called every time a non-command related user event error occurs. |
| 44 | +- The [`AutocompleteContext`](https://pkg.go.dev/github.com/zekrotja/ken#AutocompleteContext) now implements [`ObjectMap`](https://pkg.go.dev/github.com/zekrotja/ken#ObjectMap) so you can pass dependencies down the line just like with command contexts. |
64 | 45 |
|
65 |
| -## `RespondMessage` |
| 46 | +- [`AutocompleteContext`](https://pkg.go.dev/github.com/zekrotja/ken#AutocompleteContext) now has a new helper function [`GetInputAny`](https://pkg.go.dev/github.com/zekrotja/ken#AutocompleteContext.GetInputAny) which takes multiple option names and returns the first match found in the event data. |
66 | 47 |
|
67 |
| -A new respond method has been added to the `ContentResponder` called [`RespondMessage`](https://pkg.go.dev/github.com/zekrotja/ken#Ctx.RespondMessage). It simply takes a message as parameter and responds with a simple message content containing the passed message. |
| 48 | +- [`FoolowUpMessage`](https://pkg.go.dev/github.com/zekrotja/ken#FollowUpMessage) has now been added accordingly to [`RespondMessage`](https://pkg.go.dev/github.com/zekrotja/ken@master#Ctx.RespondMessage), which was added in `v0.19.0`. |
68 | 49 |
|
69 | 50 | ## Update
|
70 | 51 |
|
|
0 commit comments