Skip to content

Commit 2d01d66

Browse files
committed
initial user option impl
1 parent 1474f71 commit 2d01d66

File tree

3 files changed

+58
-7
lines changed

3 files changed

+58
-7
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by Elizabeth (lizclipse) on 09/08/2023.
6+
//
7+
8+
import Foundation
9+
import DiscordKitCore
10+
11+
public struct UserOption: CommandOption {
12+
public init(_ name: String, description: String, `required`: Bool? = nil, choices: [AppCommandOptionChoice]? = nil, minLength: Int? = nil, maxLength: Int? = nil, autocomplete: Bool? = nil) {
13+
type = .user
14+
15+
self.required = `required`
16+
self.choices = choices
17+
self.name = name
18+
self.description = description
19+
self.autocomplete = autocomplete
20+
}
21+
22+
public var type: CommandOptionType
23+
24+
public var required: Bool?
25+
26+
/// Choices for the user to pick from
27+
///
28+
/// > Important: There can be a max of 25 choices.
29+
public let choices: [AppCommandOptionChoice]?
30+
31+
public let name: String
32+
public let description: String
33+
34+
/// If autocomplete interactions are enabled for this option
35+
public let autocomplete: Bool?
36+
}

Sources/DiscordKitBot/BotMessage.swift

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,26 @@ import DiscordKitCore
1414
/// > Internally, `Message`s are converted to and from this type
1515
/// > for easier use
1616
public struct BotMessage {
17-
public let content: String
18-
public let channelID: Snowflake // This will be changed very soon
19-
public let id: Snowflake // This too
17+
// public let content: String
18+
// public let channelID: Snowflake // This will be changed very soon
19+
// public let id: Snowflake // This too
20+
// public let author: User
21+
22+
public var content: String { get { return inner.content } }
23+
public var channelID: Snowflake { get { return inner.channel_id } }
24+
public var id: Snowflake { get { return inner.id } }
25+
26+
public let inner: Message
2027

2128
// The REST handler associated with this message, used for message actions
2229
fileprivate weak var rest: DiscordREST?
2330

2431
internal init(from message: Message, rest: DiscordREST) {
25-
content = message.content
26-
channelID = message.channel_id
27-
id = message.id
32+
self.inner = message
33+
// content = message.content
34+
// channelID = message.channel_id
35+
// id = message.id
36+
// author = message.author
2837

2938
self.rest = rest
3039
}

Sources/DiscordKitCore/Objects/Data/Interaction.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public struct Interaction: Decodable {
7272
case integer(Int)
7373
case double(Double)
7474
case boolean(Bool) // Discord docs are disappointing
75+
case user(Snowflake)
7576

7677
public func encode(to encoder: Encoder) throws {
7778
var container = encoder.singleValueContainer()
@@ -80,14 +81,18 @@ public struct Interaction: Decodable {
8081
case .integer(let val): try container.encode(val)
8182
case .double(let val): try container.encode(val)
8283
case .boolean(let val): try container.encode(val)
84+
case .user(let val): try container.encode(val)
8385
}
8486
}
8587

8688
/// Get the wrapped `String` value
8789
///
8890
/// - Returns: The string value of a certain option if it is present and is of type `String`, otherwise `nil`
8991
public func value() -> String? {
90-
guard case let .string(val) = self else { return nil }
92+
guard case let .string(val) = self else {
93+
guard case let .user(val) = self else { return nil }
94+
return val
95+
}
9196
return val
9297
}
9398
/// Get the wrapped `Int` value
@@ -145,6 +150,7 @@ public struct Interaction: Decodable {
145150
case .number: value = .double(try container.decode(Double.self, forKey: .value))
146151
case .boolean: value = .boolean(try container.decode(Bool.self, forKey: .value))
147152
case .string: value = .string(try container.decode(String.self, forKey: .value))
153+
case .user: value = .user(try container.decode(Snowflake.self, forKey: .value))
148154
default: value = nil
149155
}
150156
}

0 commit comments

Comments
 (0)