Skip to content
This repository has been archived by the owner on Jan 3, 2024. It is now read-only.

Commit

Permalink
Move Webhook regex check to its own method
Browse files Browse the repository at this point in the history
  • Loading branch information
Defxult committed Oct 14, 2023
1 parent aed2ba5 commit 09292fd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
15 changes: 4 additions & 11 deletions Sources/Discord/Discord.swift
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ public class Bot {
for addedListener in listeners {
let currentListenerNames = self.listeners.map({ $0.name })
if currentListenerNames.contains(addedListener.name) {
throw DiscordError.generic("An event listener with the name '\(addedListener.name)' has already been added")
throw DiscordError.generic("an event listener with the name '\(addedListener.name)' has already been added")
} else {
self.listeners.append(addedListener)
}
Expand Down Expand Up @@ -607,19 +607,12 @@ public class Bot {
return try await http.getWebhook(webhookId: id)
}

/// Request a webhook by its URL. Unlike ``requestWebhookFrom(id:)``, this does not require authentification. Meaning this can be called prior to connecting to Discord via ``connect()``.
/// Request a webhook by its URL. Unlike ``requestWebhookFrom(id:)``, this does not require authentification. Meaning this can be called prior to connecting to Discord.
/// - Parameter url: The webhooks URL.
/// - Returns: The webhook matching the given URL.
public func requestWebhookFrom(url: String) async throws -> Webhook {
let webhookUrlRegex = #/https://discord\.com/api/webhooks/[0-9]{17,20}/\S+/#
guard let _ = url.wholeMatch(of: webhookUrlRegex) else {
throw DiscordError.generic("Invalid webhook URL")
}
let split = Array(url.split(separator: "/").suffix(2))
let webhookId = split[0].description
let webhookToken = split[1].description

return try await http.getWebhookWithToken(webhookId: webhookId, webhookToken: webhookToken)
let info = try Webhook.extractFromURL(url)
return try await http.getWebhookWithToken(webhookId: info.id, webhookToken: info.token!)
}

/// Connect to Discord and blocks the app from exiting.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Discord/Http.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1619,7 +1619,7 @@ class HTTPClient {

/// Same as ``getWebhook()``, except this call does not require authentication and returns no user in the webhook object.
/// https://discord.com/developers/docs/resources/webhook#get-webhook-with-token
func getWebhookWithToken(webhookId: String, webhookToken: String) async throws -> Webhook {
func getWebhookWithToken(webhookId: Snowflake, webhookToken: String) async throws -> Webhook {
let data = try await request(.GET, route("/webhooks/\(webhookId)/\(webhookToken)")) as! JSON
return .init(bot: bot, webhookData: data)
}
Expand Down
16 changes: 16 additions & 0 deletions Sources/Discord/Models/Webhook.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ public struct Webhook : Object {
url = token == nil ? nil : "https://discord.com/api/webhooks/\(id)/\(token!)"
}

static func extractFromURL(_ url: String) throws -> (id: Snowflake, token: String?) {
let webhookUrlRegex = #/https://discord\.com/api/webhooks/[0-9]{17,20}/\S+/#
let invalid = "invalid webhook URL"
guard let _ = url.wholeMatch(of: webhookUrlRegex) else {
throw DiscordError.generic(invalid)
}
let split = Array(url.split(separator: "/").suffix(2))
// Verify the first element are all numbers (the ID)
if !split[0].allSatisfy(\.isNumber) {
throw DiscordError.generic(invalid)
}
let webhookId = split[0].description
let webhookToken = split.count > 1 ? split[1].description : nil
return (Conversions.snowflakeToUInt(webhookId), webhookToken)
}

/// Deletes the webhook.
public func delete() async throws {
try await bot!.http.deleteWebhook(webhookId: id)
Expand Down

0 comments on commit 09292fd

Please sign in to comment.