Migrate Remaining Chat Settings Commands to Helix API (#4040)

This commit is contained in:
nerix
2022-10-03 19:42:02 +02:00
committed by GitHub
parent 4c2e97bea6
commit 25bccc90b4
7 changed files with 846 additions and 39 deletions
+326 -38
View File
@@ -2108,46 +2108,74 @@ void CommandController::initialize(Settings &, Paths &paths)
return "";
}); // /raid
const auto formatChatSettingsError =
[](const HelixUpdateChatSettingsError error, const QString &message) {
QString errorMessage = QString("Failed to update - ");
using Error = HelixUpdateChatSettingsError;
switch (error)
{
case Error::UserMissingScope: {
// TODO(pajlada): Phrase MISSING_REQUIRED_SCOPE
errorMessage += "Missing required scope. "
"Re-login with your "
"account and try again.";
}
break;
const auto formatChatSettingsError = [](const HelixUpdateChatSettingsError
error,
const QString &message,
int durationUnitMultiplier = 1) {
static const QRegularExpression invalidRange("(\\d+) through (\\d+)");
case Error::UserNotAuthorized: {
// TODO(pajlada): Phrase MISSING_PERMISSION
errorMessage += "You don't have permission to "
"perform that action.";
}
break;
case Error::Ratelimited: {
errorMessage += "You are being ratelimited by Twitch. Try "
"again in a few seconds.";
}
break;
case Error::Forwarded: {
errorMessage = message;
}
break;
case Error::Unknown:
default: {
errorMessage += "An unknown error has occurred.";
}
break;
QString errorMessage = QString("Failed to update - ");
using Error = HelixUpdateChatSettingsError;
switch (error)
{
case Error::UserMissingScope: {
// TODO(pajlada): Phrase MISSING_REQUIRED_SCOPE
errorMessage += "Missing required scope. "
"Re-login with your "
"account and try again.";
}
return errorMessage;
};
break;
case Error::UserNotAuthorized:
case Error::Forbidden: {
// TODO(pajlada): Phrase MISSING_PERMISSION
errorMessage += "You don't have permission to "
"perform that action.";
}
break;
case Error::Ratelimited: {
errorMessage += "You are being ratelimited by Twitch. Try "
"again in a few seconds.";
}
break;
case Error::OutOfRange: {
QRegularExpressionMatch matched = invalidRange.match(message);
if (matched.hasMatch())
{
auto from = matched.captured(1).toInt();
auto to = matched.captured(2).toInt();
errorMessage +=
QString("The duration is out of the valid range: "
"%1 through %2.")
.arg(from == 0 ? "0s"
: formatTime(from *
durationUnitMultiplier),
to == 0
? "0s"
: formatTime(to * durationUnitMultiplier));
}
else
{
errorMessage += message;
}
}
break;
case Error::Forwarded: {
errorMessage = message;
}
break;
case Error::Unknown:
default: {
errorMessage += "An unknown error has occurred.";
}
break;
}
return errorMessage;
};
this->registerCommand("/emoteonly", [formatChatSettingsError](
const QStringList & /* words */,
@@ -2223,6 +2251,266 @@ void CommandController::initialize(Settings &, Paths &paths)
});
return "";
});
this->registerCommand(
"/subscribers", [formatChatSettingsError](
const QStringList & /* words */, auto channel) {
auto currentUser = getApp()->accounts->twitch.getCurrent();
if (currentUser->isAnon())
{
channel->addMessage(makeSystemMessage(
"You must be logged in to update chat settings!"));
return "";
}
auto *twitchChannel = dynamic_cast<TwitchChannel *>(channel.get());
if (twitchChannel == nullptr)
{
channel->addMessage(makeSystemMessage(
"The /subscribers command only works in Twitch channels"));
return "";
}
if (twitchChannel->accessRoomModes()->submode)
{
channel->addMessage(makeSystemMessage(
"This room is already in subscribers-only mode."));
return "";
}
getHelix()->updateSubscriberMode(
twitchChannel->roomId(), currentUser->getUserId(), true,
[](auto) {
//we'll get a message from irc
},
[channel, formatChatSettingsError](auto error, auto message) {
channel->addMessage(makeSystemMessage(
formatChatSettingsError(error, message)));
});
return "";
});
this->registerCommand("/subscribersoff", [formatChatSettingsError](
const QStringList
& /* words */,
auto channel) {
auto currentUser = getApp()->accounts->twitch.getCurrent();
if (currentUser->isAnon())
{
channel->addMessage(makeSystemMessage(
"You must be logged in to update chat settings!"));
return "";
}
auto *twitchChannel = dynamic_cast<TwitchChannel *>(channel.get());
if (twitchChannel == nullptr)
{
channel->addMessage(makeSystemMessage(
"The /subscribersoff command only works in Twitch channels"));
return "";
}
if (!twitchChannel->accessRoomModes()->submode)
{
channel->addMessage(makeSystemMessage(
"This room is not in subscribers-only mode."));
return "";
}
getHelix()->updateSubscriberMode(
twitchChannel->roomId(), currentUser->getUserId(), false,
[](auto) {
// we'll get a message from irc
},
[channel, formatChatSettingsError](auto error, auto message) {
channel->addMessage(
makeSystemMessage(formatChatSettingsError(error, message)));
});
return "";
});
this->registerCommand("/slow", [formatChatSettingsError](
const QStringList &words, auto channel) {
auto currentUser = getApp()->accounts->twitch.getCurrent();
if (currentUser->isAnon())
{
channel->addMessage(makeSystemMessage(
"You must be logged in to update chat settings!"));
return "";
}
auto *twitchChannel = dynamic_cast<TwitchChannel *>(channel.get());
if (twitchChannel == nullptr)
{
channel->addMessage(makeSystemMessage(
"The /slow command only works in Twitch channels"));
return "";
}
int duration = 30;
if (words.length() >= 2)
{
bool ok = false;
duration = words.at(1).toInt(&ok);
if (!ok || duration <= 0)
{
channel->addMessage(makeSystemMessage(
"Usage: \"/slow [duration]\" - Enables slow mode (limit "
"how often users may send messages). Duration (optional, "
"default=30) must be a positive number of seconds. Use "
"\"slowoff\" to disable. "));
return "";
}
}
if (twitchChannel->accessRoomModes()->slowMode == duration)
{
channel->addMessage(makeSystemMessage(
QString("This room is already in %1-second slow mode.")
.arg(duration)));
return "";
}
getHelix()->updateSlowMode(
twitchChannel->roomId(), currentUser->getUserId(), duration,
[](auto) {
//we'll get a message from irc
},
[channel, formatChatSettingsError](auto error, auto message) {
channel->addMessage(
makeSystemMessage(formatChatSettingsError(error, message)));
});
return "";
});
this->registerCommand(
"/slowoff", [formatChatSettingsError](const QStringList & /* words */,
auto channel) {
auto currentUser = getApp()->accounts->twitch.getCurrent();
if (currentUser->isAnon())
{
channel->addMessage(makeSystemMessage(
"You must be logged in to update chat settings!"));
return "";
}
auto *twitchChannel = dynamic_cast<TwitchChannel *>(channel.get());
if (twitchChannel == nullptr)
{
channel->addMessage(makeSystemMessage(
"The /slowoff command only works in Twitch channels"));
return "";
}
if (twitchChannel->accessRoomModes()->slowMode <= 0)
{
channel->addMessage(
makeSystemMessage("This room is not in slow mode."));
return "";
}
getHelix()->updateSlowMode(
twitchChannel->roomId(), currentUser->getUserId(), boost::none,
[](auto) {
// we'll get a message from irc
},
[channel, formatChatSettingsError](auto error, auto message) {
channel->addMessage(makeSystemMessage(
formatChatSettingsError(error, message)));
});
return "";
});
this->registerCommand("/followers", [formatChatSettingsError](
const QStringList &words,
auto channel) {
auto currentUser = getApp()->accounts->twitch.getCurrent();
if (currentUser->isAnon())
{
channel->addMessage(makeSystemMessage(
"You must be logged in to update chat settings!"));
return "";
}
auto *twitchChannel = dynamic_cast<TwitchChannel *>(channel.get());
if (twitchChannel == nullptr)
{
channel->addMessage(makeSystemMessage(
"The /followers command only works in Twitch channels"));
return "";
}
int duration = 0;
if (words.length() >= 2)
{
auto parsed = parseDurationToSeconds(words.mid(1).join(' '), 60);
duration = (int)(parsed / 60);
// -1 / 60 == 0 => use parsed
if (parsed < 0)
{
channel->addMessage(makeSystemMessage(
"Usage: \"/followers [duration]\" - Enables followers-only"
" mode (only users who have followed for 'duration' may "
"chat). Examples: \"30m\", \"1 week\", \"5 days 12 "
"hours\". Must be less than 3 months. "));
return "";
}
}
if (twitchChannel->accessRoomModes()->followerOnly == duration)
{
channel->addMessage(makeSystemMessage(
QString("This room is already in %1 followers-only mode.")
.arg(formatTime(duration * 60))));
return "";
}
getHelix()->updateFollowerMode(
twitchChannel->roomId(), currentUser->getUserId(), duration,
[](auto) {
//we'll get a message from irc
},
[channel, formatChatSettingsError](auto error, auto message) {
channel->addMessage(makeSystemMessage(
formatChatSettingsError(error, message, 60)));
});
return "";
});
this->registerCommand("/followersoff", [formatChatSettingsError](
const QStringList & /* words */,
auto channel) {
auto currentUser = getApp()->accounts->twitch.getCurrent();
if (currentUser->isAnon())
{
channel->addMessage(makeSystemMessage(
"You must be logged in to update chat settings!"));
return "";
}
auto *twitchChannel = dynamic_cast<TwitchChannel *>(channel.get());
if (twitchChannel == nullptr)
{
channel->addMessage(makeSystemMessage(
"The /followersoff command only works in Twitch channels"));
return "";
}
if (twitchChannel->accessRoomModes()->followerOnly < 0)
{
channel->addMessage(
makeSystemMessage("This room is not in followers-only mode. "));
return "";
}
getHelix()->updateFollowerMode(
twitchChannel->roomId(), currentUser->getUserId(), boost::none,
[](auto) {
// we'll get a message from irc
},
[channel, formatChatSettingsError](auto error, auto message) {
channel->addMessage(
makeSystemMessage(formatChatSettingsError(error, message)));
});
return "";
});
}
void CommandController::save()