DiscordKit feat: impl getGatewayURL
This commit is contained in:
24
.vscode/launch.json
vendored
Normal file
24
.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
{
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"type": "swift",
|
||||||
|
"request": "launch",
|
||||||
|
"args": [],
|
||||||
|
"cwd": "${workspaceFolder:zunda-bot}",
|
||||||
|
"name": "Debug zundamon",
|
||||||
|
"target": "zundamon",
|
||||||
|
"configuration": "debug",
|
||||||
|
"preLaunchTask": "swift: Build Debug zundamon"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "swift",
|
||||||
|
"request": "launch",
|
||||||
|
"args": [],
|
||||||
|
"cwd": "${workspaceFolder:zunda-bot}",
|
||||||
|
"name": "Release zundamon",
|
||||||
|
"target": "zundamon",
|
||||||
|
"configuration": "release",
|
||||||
|
"preLaunchTask": "swift: Build Release zundamon"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"originHash" : "b3f560c4f196109910c6c326162b62c9d4c171d5ba1f22725daa5f5a9ffd2c75",
|
"originHash" : "8fae71593cfeba2ef52421241b82a32253b5dfcaf81f7533c3f5b0f981822d3a",
|
||||||
"pins" : [
|
"pins" : [
|
||||||
{
|
{
|
||||||
"identity" : "async-http-client",
|
"identity" : "async-http-client",
|
||||||
@@ -24,8 +24,8 @@
|
|||||||
"kind" : "remoteSourceControl",
|
"kind" : "remoteSourceControl",
|
||||||
"location" : "https://github.com/DiscordBM/DiscordBM.git",
|
"location" : "https://github.com/DiscordBM/DiscordBM.git",
|
||||||
"state" : {
|
"state" : {
|
||||||
"revision" : "90d98d45ae3ee8ed8a2d3d6d86be4aa8ab56515c",
|
"branch" : "main",
|
||||||
"version" : "1.16.0"
|
"revision" : "90d98d45ae3ee8ed8a2d3d6d86be4aa8ab56515c"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -60,8 +60,8 @@
|
|||||||
"kind" : "remoteSourceControl",
|
"kind" : "remoteSourceControl",
|
||||||
"location" : "https://github.com/apple/swift-async-algorithms.git",
|
"location" : "https://github.com/apple/swift-async-algorithms.git",
|
||||||
"state" : {
|
"state" : {
|
||||||
"revision" : "2971dd5d9f6e0515664b01044826bcea16e59fac",
|
"revision" : "9d349bcc328ac3c31ce40e746b5882742a0d1272",
|
||||||
"version" : "1.1.2"
|
"version" : "1.1.3"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -87,8 +87,8 @@
|
|||||||
"kind" : "remoteSourceControl",
|
"kind" : "remoteSourceControl",
|
||||||
"location" : "https://github.com/apple/swift-collections.git",
|
"location" : "https://github.com/apple/swift-collections.git",
|
||||||
"state" : {
|
"state" : {
|
||||||
"revision" : "7b847a3b7008b2dc2f47ca3110d8c782fb2e5c7e",
|
"revision" : "8d9834a6189db730f6264db7556a7ffb751e99ee",
|
||||||
"version" : "1.3.0"
|
"version" : "1.4.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -96,8 +96,8 @@
|
|||||||
"kind" : "remoteSourceControl",
|
"kind" : "remoteSourceControl",
|
||||||
"location" : "https://github.com/apple/swift-configuration.git",
|
"location" : "https://github.com/apple/swift-configuration.git",
|
||||||
"state" : {
|
"state" : {
|
||||||
"revision" : "1bb939fe7bbb00b8f8bab664cc90020c035c08d9",
|
"revision" : "be76c4ad929eb6c4bcaf3351799f2adf9e6848a9",
|
||||||
"version" : "1.1.0"
|
"version" : "1.2.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -114,8 +114,8 @@
|
|||||||
"kind" : "remoteSourceControl",
|
"kind" : "remoteSourceControl",
|
||||||
"location" : "https://github.com/apple/swift-distributed-tracing.git",
|
"location" : "https://github.com/apple/swift-distributed-tracing.git",
|
||||||
"state" : {
|
"state" : {
|
||||||
"revision" : "e109d8b5308d0e05201d9a1dd1c475446a946a11",
|
"revision" : "dc4030184203ffafbb2ec614352487235d747fe0",
|
||||||
"version" : "1.4.0"
|
"version" : "1.4.1"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -5,8 +5,9 @@ import PackageDescription
|
|||||||
|
|
||||||
let package = Package(
|
let package = Package(
|
||||||
name: "zundamon",
|
name: "zundamon",
|
||||||
|
platforms: [.macOS(.v13)],
|
||||||
dependencies: [
|
dependencies: [
|
||||||
.package(url: "https://github.com/DiscordBM/DiscordBM.git", from: "1.16.0"),
|
.package(url: "https://github.com/DiscordBM/DiscordBM.git", branch: "main"),
|
||||||
.package(url: "https://github.com/thebarndog/swift-dotenv.git", from: "2.1.0"),
|
.package(url: "https://github.com/thebarndog/swift-dotenv.git", from: "2.1.0"),
|
||||||
.package(url: "https://github.com/CoreOffice/XMLCoder.git", from: "0.18.0")
|
.package(url: "https://github.com/CoreOffice/XMLCoder.git", from: "0.18.0")
|
||||||
],
|
],
|
||||||
@@ -18,8 +19,12 @@ let package = Package(
|
|||||||
dependencies: [
|
dependencies: [
|
||||||
.product(name: "DiscordBM", package: "discordbm"),
|
.product(name: "DiscordBM", package: "discordbm"),
|
||||||
.product(name: "SwiftDotenv", package: "swift-dotenv"),
|
.product(name: "SwiftDotenv", package: "swift-dotenv"),
|
||||||
.product(name: "XMLCoder", package: "xmlcoder")
|
.product(name: "XMLCoder", package: "xmlcoder"),
|
||||||
|
.target(name: "DiscordKit")
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
.target(
|
||||||
|
name: "DiscordKit",
|
||||||
|
),
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|||||||
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
|
import Foundation
|
||||||
|
#if canImport(FoundationNetworking)
|
||||||
import FoundationNetworking
|
import FoundationNetworking
|
||||||
|
#endif
|
||||||
import DiscordBM
|
import DiscordBM
|
||||||
|
|
||||||
struct MessageHandler {
|
struct MessageHandler {
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
#if canImport(FoundationNetworking)
|
||||||
import FoundationNetworking
|
import FoundationNetworking
|
||||||
|
#endif
|
||||||
import XMLCoder
|
import XMLCoder
|
||||||
import DiscordBM
|
import DiscordBM
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
import DiscordBM
|
import DiscordBM
|
||||||
|
import DiscordKit
|
||||||
import SwiftDotenv
|
import SwiftDotenv
|
||||||
|
|
||||||
@main
|
@main
|
||||||
@@ -10,28 +11,33 @@ struct Zundamon {
|
|||||||
let tmp = Result { try Dotenv.configure() }
|
let tmp = Result { try Dotenv.configure() }
|
||||||
switch (tmp) {
|
switch (tmp) {
|
||||||
case .success:
|
case .success:
|
||||||
print("Loaded .env file")
|
break
|
||||||
case .failure:
|
case .failure:
|
||||||
|
print("Failed to load .env file")
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
let token = ProcessInfo.processInfo.environment["DISCORD_TOKEN"]!
|
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
|
let bot = try await DiscordKit.Bot(token: token)
|
||||||
taskGroup.addTask {
|
|
||||||
await bot.connect()
|
|
||||||
let tmp = try! await bot.client.getOwnUser()
|
|
||||||
ownID = try! tmp.decode().id
|
|
||||||
}
|
|
||||||
|
|
||||||
for await event in await bot.events {
|
// let bot = await BotGatewayManager(token: token, intents: [.guildMessages, .messageContent])
|
||||||
taskGroup.addTask {
|
|
||||||
await EventHandler(event: event, client: bot.client).handleAsync()
|
//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