Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ let package = Package(
.target(
name: "DiscordKitCore",
dependencies: [
.product(name: "Reachability", package: "Reachability.swift", condition: .when(platforms: [.macOS])),
.product(name: "Reachability", package: "Reachability.swift", condition: .when(platforms: [.macOS, .iOS])),
.product(name: "SwiftProtobuf", package: "swift-protobuf"),
.product(name: "Logging", package: "swift-log"),
.product(name: "OpenCombine", package: "OpenCombine", condition: .when(platforms: [.linux])),
Expand Down
17 changes: 11 additions & 6 deletions Sources/DiscordKit/Gateway/GatewayCachedState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,17 @@ public class CachedState: ObservableObject {
/// Populates the cache using the provided event.
/// - Parameter event: An incoming Gateway "ready" event.
func configure(using event: ReadyEvt) {
event.guilds.forEach(appendOrReplace(_:))
dms = event.private_channels
user = event.user
event.users.forEach(appendOrReplace(_:))
event.merged_members.enumerated().forEach { (idx, guildMembers) in
members[event.guilds[idx].id] = guildMembers.first(where: { $0.user_id == event.user.id })
do {
let guilds = try event.guilds.map({ try $0.unwrap() })
guilds.forEach(appendOrReplace(_:))
dms = try event.private_channels.map({ try $0.unwrap() })
user = event.user
event.users.forEach(appendOrReplace(_:))
event.merged_members.enumerated().forEach { (idx, guildMembers) in
members[guilds[idx].id] = guildMembers.first(where: { $0.user_id == event.user.id })
}
} catch {
print("Error configuring cache: \(error.localizedDescription)")
}
print(members)
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/DiscordKitCore/REST/APIChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public extension DiscordREST {
/// > POST: `/channels/{channel.id}/messages`
func createChannelMsg(
message: NewMessage,
attachments: [URL] = [],
attachments: [some Attachable] = [URL](),
id: Snowflake
) async throws -> Message {
return try await postReq(path: "channels/\(id)/messages", body: message, attachments: attachments)
Expand All @@ -66,7 +66,7 @@ public extension DiscordREST {
id: Snowflake,
msgID: Snowflake
) async throws -> MessageReadAck {
return try await postReq(path: "channels/\(id)/messages/\(msgID)/ack", body: MessageReadAck(token: nil), attachments: [])
return try await postReq(path: "channels/\(id)/messages/\(msgID)/ack", body: MessageReadAck(token: nil), attachments: [URL]())
}

/// Typing Start (Undocumented endpoint!)
Expand Down
46 changes: 39 additions & 7 deletions Sources/DiscordKitCore/REST/APIMultipartFormBody.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,61 @@

import Foundation

#if canImport(AppIntents)
import AppIntents

@available(macOS 13.0, iOS 16.0, *)
extension IntentFile: Attachable {
public func asAttachable() throws -> AttachableData {
AttachableData(data: self.data, name: self.filename, mimeType: self.type?.preferredMIMEType ?? "application/octet-stream")
}
}
#endif

public protocol Attachable {
func asAttachable() throws -> AttachableData
}

public struct AttachableData {
public let data: Data
public let name: String
public let mimeType: String

public init(data: Data, name: String, mimeType: String) {
self.data = data
self.name = name
self.mimeType = mimeType
}
}

extension URL: Attachable {
public func asAttachable() throws -> AttachableData {
let name = try self.resourceValues(forKeys: [URLResourceKey.nameKey]).name ?? UUID().uuidString
return AttachableData(data: try Data(contentsOf: self), name: name, mimeType: self.mimeType)
}
}

public extension DiscordREST {
static func createMultipartBody(
with payloadJson: Data?,
boundary: String,
attachments: [URL]
attachments: [some Attachable]
) -> Data {
var body = Data()

for (num, attachment) in attachments.enumerated() {
guard let name = try? attachment.resourceValues(forKeys: [URLResourceKey.nameKey]).name else {
continue
}
guard let attachmentData = try? Data(contentsOf: attachment) else {
guard let attachmentData = try? attachment.asAttachable() else {
DiscordREST.log.error("Could not get data of attachment #\(num)")
continue
}
let name = attachmentData.name

body.append("--\(boundary)\r\n".data(using: .utf8)!)
body.append(
"Content-Disposition: form-data; name=\"files[\(num)]\"; filename=\"\(name)\"\r\n".data(using: .utf8)!
)
body.append("Content-Type: \(attachment.mimeType)\r\n\r\n".data(using: .utf8)!)
body.append(attachmentData)
body.append("Content-Type: \(attachmentData.mimeType)\r\n\r\n".data(using: .utf8)!)
body.append(attachmentData.data)
body.append("\r\n".data(using: .utf8)!)
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/DiscordKitCore/REST/APIRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public extension DiscordREST {
func makeRequest(
path: String,
query: [URLQueryItem] = [],
attachments: [URL] = [],
attachments: [some Attachable] = [URL](),
body: Data? = nil,
method: RequestMethod = .get
) async throws -> Data {
Expand Down Expand Up @@ -143,7 +143,7 @@ public extension DiscordREST {
func postReq<D: Decodable, B: Encodable>(
path: String,
body: B? = nil,
attachments: [URL] = []
attachments: [some Attachable] = [URL]()
) async throws -> D {
let payload = body != nil ? try DiscordREST.encoder.encode(body) : nil
let respData = try await makeRequest(
Expand Down