Skip to content

Commit 54bea4a

Browse files
feat (command)!: update command builder with modifiers
BREAKING CHANGE: certain option parameters are no longer passed as arguments to init but as modifiers instead
1 parent 1e7b882 commit 54bea4a

File tree

5 files changed

+38
-19
lines changed

5 files changed

+38
-19
lines changed

Sources/DiscordKitBot/ApplicationCommand/Option/BooleanOption.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ import DiscordKitCore
1212
///
1313
/// To be used with the ``OptionBuilder`` from the ``NewAppCommand`` initialiser
1414
public struct BooleanOption: CommandOption {
15-
public init(_ name: String, description: String, `required`: Bool? = nil) {
15+
public init(_ name: String, description: String) {
1616
type = .boolean
1717

1818
self.name = name
1919
self.description = description
20-
self.required = `required`
2120
}
2221

2322
public let type: CommandOptionType
@@ -26,5 +25,5 @@ public struct BooleanOption: CommandOption {
2625

2726
public let description: String
2827

29-
public let required: Bool?
28+
public var required: Bool?
3029
}

Sources/DiscordKitBot/ApplicationCommand/Option/CommandOption.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,21 @@ public protocol CommandOption: Encodable {
2323
/// > Important: Must be 1-100 characters long
2424
var description: String { get }
2525
/// If this command is required
26-
var required: Bool? { get }
26+
var required: Bool? { get set }
2727

2828
/// Channel types to restrict visibility of command to
2929
// var channel_types: ChannelType? { get }
3030
}
3131

32+
// MARK: Modifiers
33+
public extension CommandOption {
34+
func required() -> Self {
35+
var opt = self
36+
opt.required = true
37+
return opt
38+
}
39+
}
40+
3241
public struct AppCommandOptionChoice: Encodable {
3342
public init(name: String, value: Interaction.Data.AppCommandData.OptionData.Value) {
3443
self.name = name

Sources/DiscordKitBot/ApplicationCommand/Option/IntegerOption.swift

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,12 @@ import DiscordKitCore
1212
///
1313
/// To be used with the ``OptionBuilder`` from the ``NewAppCommand`` initialiser
1414
public struct IntegerOption: CommandOption {
15-
public init(_ name: String, description: String, `required`: Bool? = nil, choices: [AppCommandOptionChoice]? = nil, min: Int? = nil, max: Int? = nil, autocomplete: Bool? = nil) {
15+
public init(_ name: String, description: String, choices: [AppCommandOptionChoice]? = nil, autocomplete: Bool? = nil) {
1616
type = .integer
1717

1818
self.name = name
1919
self.description = description
20-
self.required = `required`
2120
self.choices = choices
22-
self.min_value = min
23-
self.max_value = max
2421
self.autocomplete = autocomplete
2522
}
2623

@@ -30,18 +27,34 @@ public struct IntegerOption: CommandOption {
3027

3128
public let description: String
3229

33-
public let required: Bool?
30+
public var required: Bool?
3431

3532
/// Choices for the user to pick from
3633
///
3734
/// > Important: There can be a max of 25 choices.
3835
public let choices: [AppCommandOptionChoice]?
3936

4037
/// Minimium value permitted for this option
41-
public let min_value: Int?
38+
fileprivate(set) var min_value: Int?
4239
/// Maximum value permitted for this option
43-
public let max_value: Int?
40+
fileprivate(set) var max_value: Int?
4441

4542
/// If autocomplete interactions are enabled for this option
4643
public let autocomplete: Bool?
4744
}
45+
46+
extension IntegerOption {
47+
/// Require the value of this option to be greater than or equal to this value
48+
public func min(_ min: Int) -> Self {
49+
var opt = self
50+
opt.min_value = min
51+
return opt
52+
}
53+
54+
/// Require the value of this option to be smaller than or equal to this value
55+
public func max(_ max: Int) -> Self {
56+
var opt = self
57+
opt.max_value = max
58+
return opt
59+
}
60+
}

Sources/DiscordKitBot/ApplicationCommand/Option/NumberOption.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ import DiscordKitCore
1212
///
1313
/// To be used with the ``OptionBuilder`` from the ``NewAppCommand`` initialiser
1414
public struct NumberOption: CommandOption {
15-
public init(_ name: String, description: String, `required`: Bool? = nil, choices: [AppCommandOptionChoice]? = nil, min: Double? = nil, max: Double? = nil, autocomplete: Bool? = nil) {
15+
public init(_ name: String, description: String, choices: [AppCommandOptionChoice]? = nil, min: Double? = nil, max: Double? = nil, autocomplete: Bool? = nil) {
1616
type = .number
1717

1818
self.name = name
1919
self.description = description
20-
self.required = `required`
2120
self.choices = choices
2221
self.min_value = min
2322
self.max_value = max
@@ -30,7 +29,7 @@ public struct NumberOption: CommandOption {
3029

3130
public let description: String
3231

33-
public let required: Bool?
32+
public var required: Bool?
3433

3534
/// Choices for the user to pick from
3635
///

Sources/DiscordKitBot/ApplicationCommand/Option/SubCommand.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,17 @@ import DiscordKitCore
1010

1111
public struct SubCommand: CommandOption {
1212
/// Create a sub-command, optionally with an array of options
13-
public init(_ name: String, description: String, required: Bool? = nil, options: [CommandOption]? = nil) {
13+
public init(_ name: String, description: String, options: [CommandOption]? = nil) {
1414
type = .subCommand
1515

1616
self.name = name
1717
self.description = description
18-
self.required = required
1918
self.options = options
2019
}
2120

2221
/// Create a sub-command with options built by an ``OptionBuilder``
23-
public init(_ name: String, description: String, required: Bool? = nil, @OptionBuilder options: () -> [CommandOption]) {
24-
self.init(name, description: description, required: required, options: options())
22+
public init(_ name: String, description: String, @OptionBuilder options: () -> [CommandOption]) {
23+
self.init(name, description: description, options: options())
2524
}
2625

2726
public let type: CommandOptionType
@@ -30,7 +29,7 @@ public struct SubCommand: CommandOption {
3029

3130
public let description: String
3231

33-
public let required: Bool?
32+
public var required: Bool?
3433

3534
/// If this command is a subcommand or subcommand group type, these nested options will be its parameters
3635
public let options: [CommandOption]?

0 commit comments

Comments
 (0)