Skip to content

Commit

Permalink
Implement Channel
Browse files Browse the repository at this point in the history
Resolves #1
  • Loading branch information
sinoru committed Jul 21, 2022
1 parent 65bd2b7 commit 6415892
Show file tree
Hide file tree
Showing 5 changed files with 674 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ A Swift library for Discord API.
- [/users/{user.id}](Sources/DiscordV10/User+API.swift#L28-L35)
- [/users/@me/guilds](Sources/DiscordV10/Guild+API.swift#L15-L24)
- [/guilds/{guild.id}](Sources/DiscordV10/Guild+API.swift#L28-L35)
- [/channels/{channel.id}](Sources/DiscordV10/Channel+API.swift#L15-L22)
- [/guilds/{guild.id}/channels](Sources/DiscordV10/Channel+API.swift#L26-L35)

## Using **swift-discord** in your project

Expand Down
36 changes: 36 additions & 0 deletions Sources/DiscordV10/Channel+API.swift
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
}
}
171 changes: 171 additions & 0 deletions Sources/DiscordV10/Channel.swift
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
}
}
38 changes: 38 additions & 0 deletions Tests/DiscordV10Tests/ChannelAPITests.swift
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)
}
}
Loading

0 comments on commit 6415892

Please sign in to comment.