Skip to content

Commit d76feb7

Browse files
fix: interaction#options should never be null (#108)
* feat: always have options handler * add changeset
1 parent ec43a73 commit d76feb7

File tree

7 files changed

+31
-58
lines changed

7 files changed

+31
-58
lines changed

.changeset/dry-dragons-check.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@buape/carbon": patch
3+
---
4+
5+
fix: interaction#options should never be null

apps/cloudo/src/commands/testing/options.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default class Options extends Command {
2121

2222
async run(interaction: CommandInteraction) {
2323
interaction.reply({
24-
content: `${interaction.options?.getString("str")}`
24+
content: `${interaction.options.getString("str")}`
2525
})
2626
}
2727
}

apps/rocko/src/commands/testing/options.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,19 @@ export default class Options extends Command {
7777

7878
async run(interaction: CommandInteraction) {
7979
await interaction.reply({
80-
content: `Errors: ${interaction.options?.errors}\nStr: ${interaction.options?.getString("str")}\nInt: ${interaction.options?.getInteger("int")}\nNum: ${interaction.options?.getNumber("num")}\nBool: ${interaction.options?.getBoolean("bool")}\nUser: ${interaction.options?.getUser("user")}\nChannel: ${interaction.options?.getChannel("channel")}\nRole: ${interaction.options?.getRole("role")}\nMentionable: ${interaction.options?.getMentionable("mentionable")}\nAutocomplete: ${interaction.options?.getString("autocomplete")}`
80+
content: `Str: ${interaction.options.getString("str")}\nInt: ${interaction.options.getInteger("int")}\nNum: ${interaction.options.getNumber("num")}\nBool: ${interaction.options.getBoolean("bool")}\nUser: ${interaction.options.getUser("user")}\nChannel: ${interaction.options.getChannel("channel")}\nRole: ${interaction.options.getRole("role")}\nMentionable: ${interaction.options.getMentionable("mentionable")}\nAutocomplete: ${interaction.options.getString("autocomplete")}`
8181
})
8282
}
8383

8484
async autocomplete(interaction: AutocompleteInteraction) {
8585
await interaction.respond([
8686
{
8787
name: "That thing you said",
88-
value: `${interaction.options?.getFocused() || "NONE"}`
88+
value: `${interaction.options.getFocused() || "NONE"}`
8989
},
9090
{
9191
name: "That thing you said but with a 4",
92-
value: `4: ${interaction.options?.getFocused() || "NONE"}`
92+
value: `4: ${interaction.options.getFocused() || "NONE"}`
9393
}
9494
])
9595
}

packages/carbon/src/internals/AutocompleteInteraction.ts

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,22 @@ import {
77
InteractionType,
88
Routes
99
} from "discord-api-types/v10"
10-
import type { BaseCommand } from "../abstracts/BaseCommand.js"
1110
import {
1211
BaseInteraction,
1312
type InteractionDefaults
1413
} from "../abstracts/BaseInteraction.js"
1514
import type { Client } from "../classes/Client.js"
16-
import { Command } from "../classes/Command.js"
1715
import { OptionsHandler } from "./OptionsHandler.js"
1816

1917
export class AutocompleteInteraction extends BaseInteraction<APIApplicationCommandAutocompleteInteraction> {
2018
/**
2119
* This is the options of the commands, parsed from the interaction data.
2220
*/
23-
options?: AutocompleteOptionsHandler
21+
options: AutocompleteOptionsHandler
2422
constructor(
2523
client: Client,
2624
data: APIApplicationCommandAutocompleteInteraction,
27-
defaults: InteractionDefaults,
28-
command?: BaseCommand
25+
defaults: InteractionDefaults
2926
) {
3027
super(client, data, defaults)
3128
if (data.type !== InteractionType.ApplicationCommandAutocomplete) {
@@ -34,20 +31,12 @@ export class AutocompleteInteraction extends BaseInteraction<APIApplicationComma
3431
if (data.data.type !== ApplicationCommandType.ChatInput) {
3532
throw new Error("Invalid command type was used to create this class")
3633
}
37-
if (
38-
command instanceof Command &&
39-
!data.data.options?.find(
40-
(x) =>
41-
x.type === ApplicationCommandOptionType.Subcommand ||
42-
x.type === ApplicationCommandOptionType.SubcommandGroup
43-
)
44-
) {
45-
this.options = new AutocompleteOptionsHandler(
46-
client,
47-
(data.data.options ??
48-
[]) as APIApplicationCommandInteractionDataBasicOption[]
49-
)
50-
}
34+
35+
this.options = new AutocompleteOptionsHandler(
36+
client,
37+
(data.data.options ??
38+
[]) as APIApplicationCommandInteractionDataBasicOption[]
39+
)
5140
}
5241

5342
override async defer() {

packages/carbon/src/internals/CommandHandler.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,9 @@ export class CommandHandler extends Base {
8787
const command = this.getCommand(rawInteraction)
8888
if (!command) return false
8989

90-
const interaction = new CommandInteraction(
91-
this.client,
92-
rawInteraction,
93-
{ ephemeral: command.ephemeral },
94-
command
95-
)
90+
const interaction = new CommandInteraction(this.client, rawInteraction, {
91+
ephemeral: command.ephemeral
92+
})
9693

9794
try {
9895
const command = this.getCommand(rawInteraction)
@@ -115,8 +112,7 @@ export class CommandHandler extends Base {
115112
const interaction = new AutocompleteInteraction(
116113
this.client,
117114
rawInteraction,
118-
{ ephemeral: command.ephemeral },
119-
command
115+
{ ephemeral: command.ephemeral }
120116
)
121117

122118
try {
Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,40 @@
11
import {
22
type APIApplicationCommandInteraction,
33
type APIApplicationCommandInteractionDataBasicOption,
4-
ApplicationCommandOptionType,
54
ApplicationCommandType,
65
InteractionType
76
} from "discord-api-types/v10"
8-
import type { BaseCommand } from "../abstracts/BaseCommand.js"
97
import {
108
BaseInteraction,
119
type InteractionDefaults
1210
} from "../abstracts/BaseInteraction.js"
1311
import type { Client } from "../classes/Client.js"
14-
import { Command } from "../classes/Command.js"
1512
import { OptionsHandler } from "./OptionsHandler.js"
16-
// import type { RawOptions } from "./OptionsHandler.js"
1713

1814
/**
1915
* Represents a command interaction
2016
*/
2117
export class CommandInteraction extends BaseInteraction<APIApplicationCommandInteraction> {
2218
/**
2319
* This is the options of the commands, parsed from the interaction data.
24-
* It is only available if the command is a {@link Command} class, and the command is a ChatInput command.
20+
* It will not have any options in it if the command is not a ChatInput command.
2521
*/
26-
options?: OptionsHandler
22+
options: OptionsHandler
2723
constructor(
2824
client: Client,
2925
data: APIApplicationCommandInteraction,
30-
defaults: InteractionDefaults,
31-
command?: BaseCommand
26+
defaults: InteractionDefaults
3227
) {
3328
super(client, data, defaults)
3429
if (data.type !== InteractionType.ApplicationCommand) {
3530
throw new Error("Invalid interaction type was used to create this class")
3631
}
37-
if (
38-
command instanceof Command &&
39-
data.data.type === ApplicationCommandType.ChatInput &&
40-
!data.data.options?.find(
41-
(x) =>
42-
x.type === ApplicationCommandOptionType.Subcommand ||
43-
x.type === ApplicationCommandOptionType.SubcommandGroup
44-
)
45-
) {
46-
this.options = new OptionsHandler(
47-
client,
48-
(data.data.options ??
49-
[]) as APIApplicationCommandInteractionDataBasicOption[]
50-
)
51-
}
32+
this.options = new OptionsHandler(
33+
client,
34+
data.data.type === ApplicationCommandType.ChatInput
35+
? ((data.data.options ??
36+
[]) as APIApplicationCommandInteractionDataBasicOption[])
37+
: []
38+
)
5239
}
5340
}

packages/carbon/src/internals/OptionsHandler.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ export class OptionsHandler extends Base {
2222
* The raw options that were in the interaction data, before they were parsed.
2323
*/
2424
readonly raw: APIApplicationCommandInteractionDataBasicOption[]
25-
/**
26-
* The errors that were encountered while parsing the options.
27-
*/
28-
readonly errors: Array<string> = []
2925

3026
constructor(
3127
client: Client,

0 commit comments

Comments
 (0)