Migrate /commercial command to the Helix API (#4094)

Co-authored-by: pajlada <rasmus.karlsson@pajlada.com>
This commit is contained in:
xel86
2022-11-05 05:43:31 -04:00
committed by GitHub
parent f0ad606d7a
commit f00f766eeb
7 changed files with 302 additions and 0 deletions
@@ -3033,6 +3033,55 @@ void CommandController::initialize(Settings &, Paths &paths)
return errorMessage;
};
auto formatStartCommercialError = [](HelixStartCommercialError error,
const QString &message) -> QString {
using Error = HelixStartCommercialError;
QString errorMessage = "Failed to start commercial - ";
switch (error)
{
case Error::UserMissingScope: {
errorMessage += "Missing required scope. Re-login with your "
"account and try again.";
}
break;
case Error::TokenMustMatchBroadcaster: {
errorMessage += "Only the broadcaster of the channel can run "
"commercials.";
}
break;
case Error::BroadcasterNotStreaming: {
errorMessage += "You must be streaming live to run "
"commercials.";
}
break;
case Error::Ratelimited: {
errorMessage += "You must wait until your cooldown period "
"expires before you can run another "
"commercial.";
}
break;
case Error::Forwarded: {
errorMessage += message;
}
break;
case Error::Unknown:
default: {
errorMessage +=
QString("An unknown error has occurred (%1).").arg(message);
}
break;
}
return errorMessage;
};
this->registerCommand(
"/vips",
[formatVIPListError](const QStringList &words,
@@ -3174,6 +3223,99 @@ void CommandController::initialize(Settings &, Paths &paths)
"/r9kbeta", [uniqueChatLambda](const QStringList &words, auto channel) {
return uniqueChatLambda(words, channel, true);
});
this->registerCommand(
"/commercial",
[formatStartCommercialError](const QStringList &words,
auto channel) -> QString {
const auto *usageStr = "Usage: \"/commercial <length>\" - Starts a "
"commercial with the "
"specified duration for the current "
"channel. Valid length options "
"are 30, 60, 90, 120, 150, and 180 seconds.";
switch (getSettings()->helixTimegateCommercial.getValue())
{
case HelixTimegateOverride::Timegate: {
if (areIRCCommandsStillAvailable())
{
return useIRCCommand(words);
}
// fall through to Helix logic
}
break;
case HelixTimegateOverride::AlwaysUseIRC: {
return useIRCCommand(words);
}
break;
case HelixTimegateOverride::AlwaysUseHelix: {
// do nothing and fall through to Helix logic
}
break;
}
if (words.size() < 2)
{
channel->addMessage(makeSystemMessage(usageStr));
return "";
}
auto user = getApp()->accounts->twitch.getCurrent();
// Avoid Helix calls without Client ID and/or OAuth Token
if (user->isAnon())
{
channel->addMessage(makeSystemMessage(
"You must be logged in to use the /commercial command"));
return "";
}
auto *tc = dynamic_cast<TwitchChannel *>(channel.get());
if (tc == nullptr)
{
return "";
}
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<int> 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 "
"live and not all viewers will receive a "
"commercial. "
"You may run another commercial in %1 seconds.")
.arg(response.retryAfter)));
},
[channel, formatStartCommercialError](auto error,
auto message) {
auto errorMessage =
formatStartCommercialError(error, message);
channel->addMessage(makeSystemMessage(errorMessage));
});
return "";
});
}
void CommandController::save()