diff --git a/Sources/D2Commands/Fun/AkinatorCommand.swift b/Sources/D2Commands/Fun/AkinatorCommand.swift index f706723f..14b162cc 100644 --- a/Sources/D2Commands/Fun/AkinatorCommand.swift +++ b/Sources/D2Commands/Fun/AkinatorCommand.swift @@ -61,26 +61,25 @@ public class AkinatorCommand: StringCommand { return } - session.answer(with: answer).listen { + // TODO: Remove once onSubscriptionMessage is async + Task { do { - let question = try $0.get() + let question = try await session.answer(with: answer) if question.progression > progressionThreshold { - session.guess().listen { - do { - guard let guess = try $0.get().first else { throw AkinatorError.noGuesses } - output.append(self.embed(of: guess)) - } catch { - output.append(error, errorText: "Error while guessing") - } - - self.sessions[channelId] = nil - context.unsubscribeFromChannel() + do { + guard let guess = try await session.guess().first else { throw AkinatorError.noGuesses } + await output.append(self.embed(of: guess)) + } catch { + await output.append(error, errorText: "Error while guessing") } + + self.sessions[channelId] = nil + context.unsubscribeFromChannel() } else { - output.append(self.embed(of: question)) + await output.append(self.embed(of: question)) } } catch { - output.append(error, errorText: "Error while answering") + await output.append(error, errorText: "Error while answering") } } } diff --git a/Sources/D2NetAPIs/Akinator/AkinatorSession.swift b/Sources/D2NetAPIs/Akinator/AkinatorSession.swift index e3a9e697..4620ffe9 100644 --- a/Sources/D2NetAPIs/Akinator/AkinatorSession.swift +++ b/Sources/D2NetAPIs/Akinator/AkinatorSession.swift @@ -94,29 +94,30 @@ public struct AkinatorSession { ) } - public func answer(with answer: AkinatorAnswer) -> Promise { - Promise.catching { try HTTPRequest(host: serverUrl.host!, port: serverUrl.port, path: "\(serverUrl.path)/answer", query: [ + public func answer(with answer: AkinatorAnswer) async throws -> AkinatorQuestion { + let request = try HTTPRequest(host: serverUrl.host!, port: serverUrl.port, path: "\(serverUrl.path)/answer", query: [ "session": session, "signature": signature, "step": "\(step)", "answer": "\(answer.value)" - ], headers: headers) } - .then { $0.fetchJSONAsync(as: AkinatorResponse.StepInformation.self) } - .mapCatching { - guard let step = Int($0.parameters.step) else { throw AkinatorError.invalidStep($0.parameters.step) } - self.step = step - return try $0.parameters.asQuestion() - } + ], headers: headers) + + let stepInfo = try await request.fetchJSON(as: AkinatorResponse.StepInformation.self) + guard let step = Int(stepInfo.parameters.step) else { throw AkinatorError.invalidStep(stepInfo.parameters.step) } + self.step = step + + return try stepInfo.parameters.asQuestion() } - public func guess() -> Promise<[AkinatorGuess], any Error> { - Promise.catching { try HTTPRequest(host: serverUrl.host!, port: serverUrl.port, path: "\(serverUrl.path)/list", query: [ + public func guess() async throws -> [AkinatorGuess] { + let request = try HTTPRequest(host: serverUrl.host!, port: serverUrl.port, path: "\(serverUrl.path)/list", query: [ "session": session, "signature": signature, "step": "\(step)" - ], headers: headers) } - .then { $0.fetchJSONAsync(as: AkinatorResponse.Guess.self) } - .mapCatching { try $0.parameters.characters.map { try $0.asGuess() } } + ], headers: headers) + + let guess = try await request.fetchJSON(as: AkinatorResponse.Guess.self) + return try guess.parameters.characters.map { try $0.asGuess() } } private static func getApiKey() async throws -> ApiKey {