From 46cdb8949827026d119a425247b54f4986a4acba Mon Sep 17 00:00:00 2001 From: xel86 Date: Fri, 11 Nov 2022 18:17:50 -0500 Subject: [PATCH] Allow Commercial API endpoint to handle commercial lengths (#4141) --- CHANGELOG.md | 2 +- .../commands/CommandController.cpp | 28 ++++++++----------- src/providers/twitch/api/Helix.cpp | 5 ++++ src/providers/twitch/api/Helix.hpp | 1 + 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 726f3d89a..ed1f4e8e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -71,7 +71,7 @@ - Minor: Migrated /vips to Helix API. Chat command will continue to be used until February 11th 2023. (#4053) - Minor: Migrated /uniquechat and /r9kbeta to Helix API. (#4057) - Minor: Migrated /uniquechatoff and /r9kbetaoff to Helix API. (#4057) -- Minor: Migrated /commercial to Helix API. (#4094) +- Minor: Migrated /commercial to Helix API. (#4094, #4141) - Minor: Added stream titles to windows live toast notifications. (#1297) - Minor: Make menus and placeholders display appropriate custom key combos. (#4045) - Minor: Migrated /chatters to Helix API. (#4088, #4097, #4114) diff --git a/src/controllers/commands/CommandController.cpp b/src/controllers/commands/CommandController.cpp index 9da458afa..9c276328e 100644 --- a/src/controllers/commands/CommandController.cpp +++ b/src/controllers/commands/CommandController.cpp @@ -2847,6 +2847,13 @@ void CommandController::initialize(Settings &, Paths &paths) } break; + case Error::MissingLengthParameter: { + errorMessage += + "Command must include a desired commercial break " + "length that is greater than zero."; + } + break; + case Error::Ratelimited: { errorMessage += "You must wait until your cooldown period " "expires before you can run another " @@ -3009,29 +3016,16 @@ void CommandController::initialize(Settings &, Paths &paths) auto broadcasterID = tc->roomId(); auto length = words.at(1).toInt(); - // We would prefer not to early out here and rather handle the API error - // like the rest of them, but the API doesn't give us a proper length error. - // Valid lengths can be found in the length body parameter description - // https://dev.twitch.tv/docs/api/reference#start-commercial - const QList validLengths = {30, 60, 90, 120, 150, 180}; - if (!validLengths.contains(length)) - { - channel->addMessage(makeSystemMessage( - "Invalid commercial duration length specified. Valid " - "options " - "are 30, 60, 90, 120, 150, and 180 seconds")); - return ""; - } - getHelix()->startCommercial( broadcasterID, length, [channel](auto response) { channel->addMessage(makeSystemMessage( - QString("Starting commercial break. Keep in mind you " - "are still " + QString("Starting %1 second long commercial break. " + "Keep in mind you are still " "live and not all viewers will receive a " "commercial. " - "You may run another commercial in %1 seconds.") + "You may run another commercial in %2 seconds.") + .arg(response.length) .arg(response.retryAfter))); }, [channel, formatStartCommercialError](auto error, diff --git a/src/providers/twitch/api/Helix.cpp b/src/providers/twitch/api/Helix.cpp index 1c6f717f3..6a7ec76f5 100644 --- a/src/providers/twitch/api/Helix.cpp +++ b/src/providers/twitch/api/Helix.cpp @@ -2398,6 +2398,11 @@ void Helix::startCommercial( failureCallback(Error::BroadcasterNotStreaming, message); } + else if (message.startsWith("Missing required parameter", + Qt::CaseInsensitive)) + { + failureCallback(Error::MissingLengthParameter, message); + } else { failureCallback(Error::Forwarded, message); diff --git a/src/providers/twitch/api/Helix.hpp b/src/providers/twitch/api/Helix.hpp index 4ee3fee08..c5c0a768f 100644 --- a/src/providers/twitch/api/Helix.hpp +++ b/src/providers/twitch/api/Helix.hpp @@ -620,6 +620,7 @@ enum class HelixStartCommercialError { TokenMustMatchBroadcaster, UserMissingScope, BroadcasterNotStreaming, + MissingLengthParameter, Ratelimited, // The error message is forwarded directly from the Twitch API