Skip to content

Commit

Permalink
Add dad joke command
Browse files Browse the repository at this point in the history
  • Loading branch information
fwcd committed Nov 5, 2024
1 parent 919a47d commit f7fdb02
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Sources/D2Commands/Fun/DadJokeCommand.swift
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")
}
}
}
1 change: 1 addition & 0 deletions Sources/D2Handlers/D2Receiver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ public class D2Receiver: Receiver {
registry["pickupline", aka: ["pickup"]] = PickupLineCommand()
registry["chucknorrisjoke", aka: ["cnj"]] = ChuckNorrisJokeCommand()
registry["joke"] = JokeCommand()
registry["dadjoke"] = DadJokeCommand()
registry["pat"] = PatCommand(inventoryManager: inventoryManager)
registry["hug"] = HugCommand(inventoryManager: inventoryManager)
registry["wouldyourather", aka: ["wyr"]] = WouldYouRatherCommand(partyGameDB: partyGameDB)
Expand Down
4 changes: 4 additions & 0 deletions Sources/D2NetAPIs/DadJoke/DadJoke.swift
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
}
16 changes: 16 additions & 0 deletions Sources/D2NetAPIs/DadJoke/DadJokeQuery.swift
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)
}
}

0 comments on commit f7fdb02

Please sign in to comment.