Skip to content

Commit

Permalink
Implement Guild
Browse files Browse the repository at this point in the history
Resolves #1
  • Loading branch information
sinoru committed Jul 21, 2022
1 parent 4ceedfb commit 64f77f3
Show file tree
Hide file tree
Showing 11 changed files with 859 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ A Swift library for Discord API.

- [/users/@me](Sources/DiscordV10/User+API.swift#L15-L24)
- [/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)

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

Expand Down
24 changes: 24 additions & 0 deletions Sources/Discord/Extensions/OptionSet.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// OptionSet.swift
//
//
// Created by Jaehong Kang on 2022/07/21.
//

import Foundation

extension OptionSet where RawValue: Decodable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()

self.init(rawValue: try container.decode(RawValue.self))
}
}

extension OptionSet where RawValue: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()

try container.encode(rawValue)
}
}
66 changes: 66 additions & 0 deletions Sources/Discord/Unknown.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//
// Unknown.swift
//
//
// Created by Jaehong Kang on 2022/07/21.
//

import Foundation

public enum Unknown<T> where T: RawRepresentable {
case value(T)
case rawValue(T.RawValue)
}

extension Unknown: RawRepresentable {
public var rawValue: T.RawValue {
switch self {
case .value(let value):
return value.rawValue
case .rawValue(let rawValue):
return rawValue
}
}

public init(rawValue: T.RawValue) {
if let value = T(rawValue: rawValue) {
self = .value(value)
} else {
self = .rawValue(rawValue)
}
}
}

extension Unknown: Equatable where T.RawValue: Equatable {
public static func == (lhs: Self, rhs: Self) -> Bool {
lhs.rawValue == rhs.rawValue
}
}

extension Unknown: Hashable where T.RawValue: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(rawValue)
}
}

extension Unknown: Comparable where T.RawValue: Comparable {
public static func < (lhs: Unknown<T>, rhs: Unknown<T>) -> Bool {
lhs.rawValue < rhs.rawValue
}
}

extension Unknown: Codable where T: Codable, T.RawValue: Codable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()

let rawValue = try container.decode(T.RawValue.self)

self.init(rawValue: rawValue)
}

public func encode(to encoder: Encoder) throws {
var encoder = encoder.singleValueContainer()

try encoder.encode(rawValue)
}
}
15 changes: 15 additions & 0 deletions Sources/DiscordV10/Emoji.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// Emoji.swift
//
//
// Created by Jaehong Kang on 2022/07/21.
//

import Foundation
import Discord

// TODO: Implement Properties
public struct Emoji: Equatable, Hashable, Sendable, Codable {
public let id: Snowflake?
public let name: String?
}
36 changes: 36 additions & 0 deletions Sources/DiscordV10/Guild+API.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// Guild+API.swift
//
//
// Created by Jaehong Kang on 2022/07/21.
//

import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
import Discord

extension Guild {
public static func myGuilds(session: Session) async throws -> [Guild] {
var urlRequest = URLRequest(url: URL(discordAPIPath: "v10/users/@me/guilds")!)
urlRequest.httpMethod = "GET"

let (data, _) = try await session.data(for: urlRequest, includesOAuth2Credential: true)

let guilds = try JSONDecoder.discord.decode([Guild].self, from: data)

return guilds
}
}

extension Guild {
public init(guildID: Snowflake, session: Session) async throws {
var urlRequest = URLRequest(url: URL(discordAPIPath: "v10/guilds/\(guildID.rawValue)")!)
urlRequest.httpMethod = "GET"

let (data, _) = try await session.data(for: urlRequest, includesOAuth2Credential: true)

self = try JSONDecoder.discord.decode(Guild.self, from: data)
}
}
187 changes: 187 additions & 0 deletions Sources/DiscordV10/Guild.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
//
// Guild.swift
//
//
// Created by Jaehong Kang on 2022/07/19.
//

import Foundation
import Discord

public struct Guild {
public enum VerificationLevel: Int, Sendable, Codable {
case none = 0
case low
case medium
case high
case veryHigh
}

public enum DefaultMessageNotificationLevel: Int, Sendable, Codable {
case allMessages = 0
case onlyMentions
}

public enum ExplicitContentFilterLevel: Int, Sendable, Codable {
case disabled = 0
case membersWithoutRoles
case allMembers
}

public enum MFALevel: Int, Sendable, Codable {
case none = 0
case elevated
}

public enum NSFWLevel: Int, Sendable, Codable {
case `default` = 0
case explicit
case safe
case ageRestricted
}

public enum PremiumTier: Int, Sendable, Codable {
case none = 0
case tier1
case tier2
case tier3
}

public struct SystemChannelFlags: OptionSet, Codable, Sendable {
public static let suppressJoinNotifications = Self.init(rawValue: 1 << 0)
public static let suppressPremiumSubscriptions = Self.init(rawValue: 1 << 1)
public static let suppressGuildReminderNotifications = Self.init(rawValue: 1 << 2)
public static let suppressJoinNotificationsReplies = Self.init(rawValue: 1 << 3)

public let rawValue: Int

public init(rawValue: Int) {
self.rawValue = rawValue
}
}

public let id: Snowflake

public let isUnavailable: Bool?

public let name: String?
public let icon: String?
public let splash: String?
public let discoverySplash: String?

public let isOwner: Bool?
public let ownerID: Snowflake?

public let permissions: String?

public let afkChannelID: Snowflake?
public let afkTimeout: Int?

public let isWidgetEnabled: Bool?
public let widgetChannelID: Snowflake?

public let verificationLevel: Unknown<VerificationLevel>?

public let defaultMessageNotifications: Unknown<DefaultMessageNotificationLevel>?

public let explicitContentFilter: Unknown<ExplicitContentFilterLevel>?

public let roles: [Role]?
public let emojis: [Emoji]?
public let features: [String]?

public let mfaLevel: Unknown<MFALevel>?

public let applicationID: Snowflake?

public let systemChannelID: Snowflake?
public let systemChannelFlags: SystemChannelFlags?

public let rulesChannelID: Snowflake?

public let maxPresences: Int?
public let maxMembers: Int?

public let vanityURLCode: String?
public let description: String?
public let banner: String?

public let premiumTier: Unknown<PremiumTier>?
public let premiumSubscriptionCount: Int?

public let preferredLocale: String?

public let publicUpdatesChannelID: Snowflake?

public let maxVideoChannelUsers: Int?

public let approximateMemberCount: Int?
public let approximatePresenceCount: Int?

public let welcomeScreen: WelcomeScreen?

public let nsfwLevel: Unknown<NSFWLevel>?

public let stickers: [Sticker]?

public let isPremiumProgressBarEnabled: Bool?
}

extension Guild: Codable {
enum CodingKeys: String, CodingKey {
case id

case isUnavailable = "unavailable"

case name
case icon
case splash
case discoverySplash

case isOwner = "owner"
case ownerID = "ownerId"

case permissions

case afkChannelID = "afkChannelId"
case afkTimeout

case isWidgetEnabled = "widgetEnabled"
case widgetChannelID = "widgetChannelId"

case verificationLevel

case defaultMessageNotifications

case explicitContentFilter

case roles
case emojis
case features

case mfaLevel

case applicationID = "applicationId"

case systemChannelID = "systemChannelId"
case systemChannelFlags
case rulesChannelID = "rulesChannelId"
case maxPresences
case maxMembers
case vanityURLCode = "vanityUrlCode"
case description
case banner
case premiumTier
case premiumSubscriptionCount
case preferredLocale
case publicUpdatesChannelID = "publicUpdatesChannelId"
case maxVideoChannelUsers
case approximateMemberCount
case approximatePresenceCount
case welcomeScreen
case nsfwLevel
case stickers
case isPremiumProgressBarEnabled = "premiumProgressBarEnabled"

}
}
14 changes: 14 additions & 0 deletions Sources/DiscordV10/Role.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// Role.swift
//
//
// Created by Jaehong Kang on 2022/07/21.
//

import Foundation
import Discord

// TODO: Implement Properties
public struct Role: Equatable, Hashable, Identifiable, Sendable, Codable {
public let id: Snowflake
}
14 changes: 14 additions & 0 deletions Sources/DiscordV10/Sticker.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// Sticker.swift
//
//
// Created by Jaehong Kang on 2022/07/21.
//

import Foundation
import Discord

// TODO: Implement Properties
public struct Sticker: Equatable, Hashable, Identifiable, Sendable, Codable {
public let id: Snowflake
}
14 changes: 14 additions & 0 deletions Sources/DiscordV10/WelcomeScreen.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// WelcomeScreen.swift
//
//
// Created by Jaehong Kang on 2022/07/21.
//

import Foundation
import Discord

// TODO: Implement Properties
public struct WelcomeScreen: Equatable, Hashable, Sendable, Codable {
public let description: String?
}
Loading

0 comments on commit 64f77f3

Please sign in to comment.