Migrate /mod command to Helix API (#4000)

Co-authored-by: Felanbird <41973452+Felanbird@users.noreply.github.com>
Co-authored-by: pajlada <rasmus.karlsson@pajlada.com>
This commit is contained in:
Aiden
2022-09-23 17:12:34 +01:00
committed by GitHub
parent 838e156a04
commit 28de3e637d
5 changed files with 233 additions and 0 deletions
@@ -1355,6 +1355,117 @@ void CommandController::initialize(Settings &, Paths &paths)
return deleteMessages(channel, messageID);
});
this->registerCommand("/mod", [](const QStringList &words, auto channel) {
if (words.size() < 2)
{
channel->addMessage(makeSystemMessage(
"Usage: \"/mod <username>\" - Grant moderator status to a "
"user. Use \"/mods\" to list the moderators of this channel."));
return "";
}
auto currentUser = getApp()->accounts->twitch.getCurrent();
if (currentUser->isAnon())
{
channel->addMessage(
makeSystemMessage("You must be logged in to mod someone!"));
return "";
}
auto *twitchChannel = dynamic_cast<TwitchChannel *>(channel.get());
if (twitchChannel == nullptr)
{
channel->addMessage(makeSystemMessage(
"The /mod command only works in Twitch channels"));
return "";
}
auto target = words.at(1);
stripChannelName(target);
getHelix()->getUserByName(
target,
[twitchChannel, channel](const HelixUser &targetUser) {
getHelix()->addChannelModerator(
twitchChannel->roomId(), targetUser.id,
[channel, targetUser] {
channel->addMessage(makeSystemMessage(
QString("You have added %1 as a moderator of this "
"channel.")
.arg(targetUser.displayName)));
},
[channel, targetUser](auto error, auto message) {
QString errorMessage =
QString("Failed to add channel moderator - ");
using Error = HelixAddChannelModeratorError;
switch (error)
{
case Error::UserMissingScope: {
// TODO(pajlada): Phrase MISSING_REQUIRED_SCOPE
errorMessage += "Missing required scope. "
"Re-login with your "
"account and try again.";
}
break;
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::TargetIsVIP: {
errorMessage +=
QString("%1 is currently a VIP, \"/unvip\" "
"them and "
"retry this command.")
.arg(targetUser.displayName);
}
break;
case Error::TargetAlreadyModded: {
// Equivalent irc error
errorMessage =
QString("%1 is already a moderator of this "
"channel.")
.arg(targetUser.displayName);
}
break;
case Error::Forwarded: {
errorMessage += message;
}
break;
case Error::Unknown:
default: {
errorMessage +=
"An unknown error has occurred.";
}
break;
}
channel->addMessage(makeSystemMessage(errorMessage));
});
},
[channel, target] {
// Equivalent error from IRC
channel->addMessage(makeSystemMessage(
QString("Invalid username: %1").arg(target)));
});
return "";
});
}
void CommandController::save()