-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolves #1
- Loading branch information
Showing
5 changed files
with
674 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// | ||
// Channel+API.swift | ||
// | ||
// | ||
// Created by Jaehong Kang on 2022/07/21. | ||
// | ||
|
||
import Foundation | ||
#if canImport(FoundationNetworking) | ||
import FoundationNetworking | ||
#endif | ||
import Discord | ||
|
||
extension Channel { | ||
public init(channelID: Snowflake, session: Session) async throws { | ||
var urlRequest = URLRequest(url: URL(discordAPIPath: "v10/channels/\(channelID.rawValue)")!) | ||
urlRequest.httpMethod = "GET" | ||
|
||
let (data, _) = try await session.data(for: urlRequest, includesOAuth2Credential: true) | ||
|
||
self = try JSONDecoder.discord.decode(Channel.self, from: data) | ||
} | ||
} | ||
|
||
extension Channel { | ||
public static func channels(forGuildID guildID: Snowflake, session: Session) async throws -> [Channel] { | ||
var urlRequest = URLRequest(url: URL(discordAPIPath: "v10/guilds/\(guildID)/channels")!) | ||
urlRequest.httpMethod = "GET" | ||
|
||
let (data, _) = try await session.data(for: urlRequest, includesOAuth2Credential: true) | ||
|
||
let channels = try JSONDecoder.discord.decode([Channel].self, from: data) | ||
|
||
return channels | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
// | ||
// Channel.swift | ||
// | ||
// | ||
// Created by Jaehong Kang on 2022/07/21. | ||
// | ||
|
||
import Foundation | ||
import Discord | ||
|
||
public struct Channel: Equatable, Hashable, Identifiable, Sendable { | ||
public enum `Type`: Int, Equatable, Hashable, Sendable, Codable { | ||
case guildText = 0 | ||
case dm | ||
case guildVoice | ||
case groupDM | ||
case guildCategory | ||
case guildNews | ||
case guildNewsThread = 10 | ||
case guildPublicThread | ||
case guildPrivateThread | ||
case guildStageVoice | ||
case guildDirectory | ||
case guildForum | ||
} | ||
|
||
public struct Overwrite: Equatable, Hashable, Identifiable, Sendable, Codable { | ||
public enum `Type`: Int, Equatable, Hashable, Sendable, Codable { | ||
case role = 0 | ||
case member | ||
} | ||
|
||
public let id: Snowflake | ||
public let type: Unknown<`Type`> | ||
public let allow: String | ||
public let deny: String | ||
} | ||
|
||
public enum VideoQualityMode: Int, Equatable, Hashable, Sendable, Codable { | ||
case auto = 1 | ||
case full | ||
} | ||
|
||
public struct ThreadMetadata: Equatable, Hashable, Sendable, Codable { | ||
public let archived: Bool | ||
public let autoArchiveDuration: Int | ||
public let archiveTimestamp: Date | ||
public let locked: Bool | ||
public let invitable: Bool? | ||
public let createTimestamp: Date? | ||
} | ||
|
||
public struct ThreadMember: Equatable, Hashable, Sendable { | ||
public let id: Snowflake? | ||
public let userID: Snowflake? | ||
public let joinTimestamp: Date | ||
public let flags: Int | ||
} | ||
|
||
public struct Flags: OptionSet, Equatable, Hashable, Sendable, Codable { | ||
public static let pinned = Self.init(rawValue: 1 << 1) | ||
|
||
public let rawValue: Int | ||
|
||
public init(rawValue: Int) { | ||
self.rawValue = rawValue | ||
} | ||
} | ||
|
||
public let id: Snowflake | ||
|
||
public let type: Unknown<`Type`> | ||
|
||
public let guildID: Snowflake? | ||
|
||
public let position: Int? | ||
|
||
public let permissionOverwrites: [Overwrite]? | ||
|
||
public let name: String? | ||
public let topic: String? | ||
|
||
public let isNSFW: Bool? | ||
|
||
public let lastMessageID: Snowflake? | ||
|
||
public let bitrate: Int? | ||
public let userLimit: Int? | ||
public let rateLimitPerUser: Int? | ||
|
||
public let recipients: [User]? | ||
|
||
public let icon: String? | ||
|
||
public let ownerID: Snowflake? | ||
public let applicationID: Snowflake? | ||
public let parentID: Snowflake? | ||
|
||
public let lastPinTimestamp: Date? | ||
|
||
public let rtcRegion: String? | ||
public let videoQualityMode: Unknown<VideoQualityMode>? | ||
|
||
public let messageCount: Int? | ||
public let memberCount: Int? | ||
|
||
public let threadMetadata: ThreadMetadata? | ||
public let member: ThreadMember? | ||
|
||
public let defaultAutoArchiveDuration: Int? | ||
public let permissions: String? | ||
public let flags: Flags? | ||
} | ||
|
||
|
||
extension Channel.ThreadMember: Codable { | ||
enum CodingKeys: String, CodingKey { | ||
case id | ||
case userID = "userId" | ||
case joinTimestamp | ||
case flags | ||
} | ||
} | ||
|
||
extension Channel: Codable { | ||
enum CodingKeys: String, CodingKey { | ||
case id | ||
|
||
case type | ||
|
||
case guildID = "guildId" | ||
|
||
case position | ||
|
||
case permissionOverwrites | ||
|
||
case name | ||
case topic | ||
|
||
case isNSFW = "nsfw" | ||
|
||
case lastMessageID = "lastMessageId" | ||
|
||
case bitrate | ||
case userLimit | ||
case rateLimitPerUser | ||
|
||
case recipients | ||
|
||
case icon | ||
|
||
case ownerID = "ownerId" | ||
case applicationID = "applicationId" | ||
case parentID = "parentId" | ||
|
||
case lastPinTimestamp | ||
|
||
case rtcRegion | ||
case videoQualityMode | ||
|
||
case messageCount | ||
case memberCount | ||
|
||
case threadMetadata | ||
case member | ||
|
||
case defaultAutoArchiveDuration | ||
case permissions | ||
case flags | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// | ||
// ChannelAPITests.swift | ||
// | ||
// | ||
// Created by Jaehong Kang on 2022/07/21. | ||
// | ||
|
||
import _DiscordTestSupport | ||
@testable import Discord | ||
@testable import DiscordV10 | ||
|
||
final class ChannelAPITests: TestCase { | ||
var session: Session! | ||
|
||
override func setUp() async throws { | ||
// Put setup code here. This method is called before the invocation of each test method in the class. | ||
session = Self.session | ||
|
||
try XCTSkipIf(Self.oAuth2Credential == nil, "oAuth2Credential not available.") | ||
await session.updateOAuth2Credential(Self.oAuth2Credential) | ||
} | ||
|
||
override func tearDown() async throws { | ||
// Put teardown code here. This method is called after the invocation of each test method in the class. | ||
} | ||
|
||
func testInitByChannelID() async throws { | ||
let channel = try await Channel(channelID: 999525888003678308, session: session) | ||
|
||
XCTAssertEqual(channel.id, 999525888003678308) | ||
} | ||
|
||
func testGetChannelsFromGuild() async throws { | ||
let channels = try await Channel.channels(forGuildID: 999525887206756372, session: session) | ||
|
||
XCTAssertEqual(channels.count, 11) | ||
} | ||
} |
Oops, something went wrong.