DiscordKit feat: impl getGatewayURL
This commit is contained in:
36
Sources/DiscordKit/ApiClient.swift
Normal file
36
Sources/DiscordKit/ApiClient.swift
Normal file
@@ -0,0 +1,36 @@
|
||||
import Foundation
|
||||
#if canImport(FoundationNetowrking)
|
||||
import FoundationNetworking
|
||||
#endif
|
||||
|
||||
struct ApiClient {
|
||||
static let apiUrl: URL = URL(string: "https://discord.com/api/v10")!
|
||||
let token: String
|
||||
|
||||
private func authReq(_ request: consuming URLRequest) async throws -> (Data, URLResponse) {
|
||||
request.setValue("Bot \(token)", forHTTPHeaderField: "Authorization")
|
||||
request.setValue("DiscordKit (https:\\candy123.moe, v0.0.1)", forHTTPHeaderField: "User-Agent")
|
||||
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
||||
let (data, res) = try await URLSession.shared.data(for: request)
|
||||
guard let res = res as? HTTPURLResponse else { throw ApiError.invalidResponse }
|
||||
guard res.statusCode >= 200 && res.statusCode <= 299 else { throw ApiError.badStatus("Status code not 2xx: \(res.statusCode)") }
|
||||
|
||||
return (data, res)
|
||||
}
|
||||
|
||||
func getGatewayURL() async throws -> URL {
|
||||
var req = URLRequest(url: ApiClient.apiUrl.appending(path: "/gateway/bot"))
|
||||
req.httpMethod = "GET"
|
||||
let (data, _) = try await authReq(req)
|
||||
|
||||
let json = JSONDecoder()
|
||||
let decoded = try json.decode(GetGatewayResponse.self, from: data)
|
||||
return decoded.url
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
enum ApiError: Error {
|
||||
case invalidResponse
|
||||
case badStatus(_ message: String)
|
||||
}
|
||||
16
Sources/DiscordKit/Bot.swift
Normal file
16
Sources/DiscordKit/Bot.swift
Normal file
@@ -0,0 +1,16 @@
|
||||
import Foundation
|
||||
#if canImport(FoundationNetowrking)
|
||||
import FoundationNetworking
|
||||
#endif
|
||||
|
||||
public struct Bot {
|
||||
let client: ApiClient
|
||||
|
||||
public init(token: String) async throws {
|
||||
client = ApiClient(token: token)
|
||||
|
||||
let gateway = try await client.getGatewayURL()
|
||||
|
||||
print(gateway.absoluteURL)
|
||||
}
|
||||
}
|
||||
17
Sources/DiscordKit/Models.swift
Normal file
17
Sources/DiscordKit/Models.swift
Normal file
@@ -0,0 +1,17 @@
|
||||
import Foundation
|
||||
#if canImport(FoundationNetowrking)
|
||||
import FoundationNetworking
|
||||
#endif
|
||||
|
||||
public struct GetGatewayResponse: Codable {
|
||||
let url: URL
|
||||
let shards: Int
|
||||
let session_start_limit: SessionStartLimit
|
||||
}
|
||||
|
||||
public struct SessionStartLimit: Codable {
|
||||
let total: Int
|
||||
let remaining: Int
|
||||
let reset_after: Int
|
||||
let max_concurrency: Int
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
import Foundation
|
||||
#if canImport(FoundationNetworking)
|
||||
import FoundationNetworking
|
||||
#endif
|
||||
import DiscordBM
|
||||
|
||||
struct MessageHandler {
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import Foundation
|
||||
#if canImport(FoundationNetworking)
|
||||
import FoundationNetworking
|
||||
#endif
|
||||
import XMLCoder
|
||||
import DiscordBM
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import Foundation
|
||||
import DiscordBM
|
||||
import DiscordKit
|
||||
import SwiftDotenv
|
||||
|
||||
@main
|
||||
@@ -10,28 +11,33 @@ struct Zundamon {
|
||||
let tmp = Result { try Dotenv.configure() }
|
||||
switch (tmp) {
|
||||
case .success:
|
||||
print("Loaded .env file")
|
||||
break
|
||||
case .failure:
|
||||
print("Failed to load .env file")
|
||||
break
|
||||
}
|
||||
|
||||
let token = ProcessInfo.processInfo.environment["DISCORD_TOKEN"]!
|
||||
|
||||
let bot = await BotGatewayManager(token: token, intents: [.guildMessages, .messageContent])
|
||||
guard !token.isEmpty else { fatalError("Err: Empty DISCORD_TOKEN. Exiting...") }
|
||||
|
||||
await withTaskGroup(of: Void.self) { taskGroup in
|
||||
taskGroup.addTask {
|
||||
await bot.connect()
|
||||
let tmp = try! await bot.client.getOwnUser()
|
||||
ownID = try! tmp.decode().id
|
||||
}
|
||||
let bot = try await DiscordKit.Bot(token: token)
|
||||
|
||||
for await event in await bot.events {
|
||||
taskGroup.addTask {
|
||||
await EventHandler(event: event, client: bot.client).handleAsync()
|
||||
}
|
||||
}
|
||||
}
|
||||
// let bot = await BotGatewayManager(token: token, intents: [.guildMessages, .messageContent])
|
||||
|
||||
//await withTaskGroup(of: Void.self) { taskGroup in
|
||||
// taskGroup.addTask {
|
||||
// await bot.connect()
|
||||
// let tmp = try! await bot.client.getOwnUser()
|
||||
// ownID = try! tmp.decode().id
|
||||
// }
|
||||
|
||||
// for await event in await bot.events {
|
||||
// taskGroup.addTask {
|
||||
// await EventHandler(event: event, client: bot.client).handleAsync()
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user