Skip to content

Commit

Permalink
initial user option impl
Browse files Browse the repository at this point in the history
  • Loading branch information
lizclipse committed Aug 9, 2023
1 parent 1474f71 commit 2d01d66
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 7 deletions.
36 changes: 36 additions & 0 deletions Sources/DiscordKitBot/ApplicationCommand/Option/UserOption.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// File.swift
//
//
// Created by Elizabeth (lizclipse) on 09/08/2023.
//

import Foundation
import DiscordKitCore

public struct UserOption: CommandOption {
public init(_ name: String, description: String, `required`: Bool? = nil, choices: [AppCommandOptionChoice]? = nil, minLength: Int? = nil, maxLength: Int? = nil, autocomplete: Bool? = nil) {
type = .user

self.required = `required`
self.choices = choices
self.name = name
self.description = description
self.autocomplete = autocomplete
}

public var type: CommandOptionType

public var required: Bool?

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

public let name: String
public let description: String

/// If autocomplete interactions are enabled for this option
public let autocomplete: Bool?
}
21 changes: 15 additions & 6 deletions Sources/DiscordKitBot/BotMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,26 @@ import DiscordKitCore
/// > Internally, `Message`s are converted to and from this type
/// > for easier use
public struct BotMessage {
public let content: String
public let channelID: Snowflake // This will be changed very soon
public let id: Snowflake // This too
// public let content: String
// public let channelID: Snowflake // This will be changed very soon
// public let id: Snowflake // This too
// public let author: User

public var content: String { get { return inner.content } }
public var channelID: Snowflake { get { return inner.channel_id } }
public var id: Snowflake { get { return inner.id } }

public let inner: Message

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

internal init(from message: Message, rest: DiscordREST) {
content = message.content
channelID = message.channel_id
id = message.id
self.inner = message
// content = message.content
// channelID = message.channel_id
// id = message.id
// author = message.author

self.rest = rest
}
Expand Down
8 changes: 7 additions & 1 deletion Sources/DiscordKitCore/Objects/Data/Interaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public struct Interaction: Decodable {
case integer(Int)
case double(Double)
case boolean(Bool) // Discord docs are disappointing
case user(Snowflake)

public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
Expand All @@ -80,14 +81,18 @@ public struct Interaction: Decodable {
case .integer(let val): try container.encode(val)
case .double(let val): try container.encode(val)
case .boolean(let val): try container.encode(val)
case .user(let val): try container.encode(val)
}
}

/// Get the wrapped `String` value
///
/// - Returns: The string value of a certain option if it is present and is of type `String`, otherwise `nil`
public func value() -> String? {
guard case let .string(val) = self else { return nil }
guard case let .string(val) = self else {
guard case let .user(val) = self else { return nil }
return val
}
return val
}
/// Get the wrapped `Int` value
Expand Down Expand Up @@ -145,6 +150,7 @@ public struct Interaction: Decodable {
case .number: value = .double(try container.decode(Double.self, forKey: .value))
case .boolean: value = .boolean(try container.decode(Bool.self, forKey: .value))
case .string: value = .string(try container.decode(String.self, forKey: .value))
case .user: value = .user(try container.decode(Snowflake.self, forKey: .value))
default: value = nil
}
}
Expand Down

0 comments on commit 2d01d66

Please sign in to comment.