-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import D2NetAPIs | ||
|
||
public class DadJokeCommand: VoidCommand { | ||
public let info = CommandInfo( | ||
category: .fun, | ||
shortDescription: "Fetches a random dad joke", | ||
requiredPermissionLevel: .basic | ||
) | ||
|
||
public init() {} | ||
|
||
public func invoke(output: any CommandOutput, context: CommandContext) async { | ||
do { | ||
let joke = try await DadJokeQuery().perform() | ||
await output.append(joke.joke) | ||
} catch { | ||
await output.append(error, errorText: "Could not fetch dad joke") | ||
} | ||
} | ||
} |
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,4 @@ | ||
public struct DadJoke: Codable, Identifiable, Sendable { | ||
public let id: String | ||
public let joke: String | ||
} |
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,16 @@ | ||
import Utils | ||
|
||
public struct DadJokeQuery: Sendable { | ||
public init() {} | ||
|
||
public func perform() async throws -> DadJoke { | ||
try await HTTPRequest( | ||
host: "icanhazdadjoke.com", | ||
path: "/", | ||
headers: [ | ||
"Accept": "application/json", | ||
"User-Agent": "D2 (https://github.com/fwcd/d2)" | ||
] | ||
).fetchJSON(as: DadJoke.self) | ||
} | ||
} |