-
-
Notifications
You must be signed in to change notification settings - Fork 455
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Rasmus Karlsson <rasmus.karlsson@pajlada.com>
- Loading branch information
Showing
8 changed files
with
247 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
#include "controllers/commands/builtin/twitch/ShieldMode.hpp" | ||
|
||
#include "Application.hpp" | ||
#include "controllers/accounts/AccountController.hpp" | ||
#include "controllers/commands/CommandContext.hpp" | ||
#include "messages/MessageBuilder.hpp" | ||
#include "providers/twitch/api/Helix.hpp" | ||
#include "providers/twitch/TwitchAccount.hpp" | ||
#include "providers/twitch/TwitchChannel.hpp" | ||
|
||
namespace chatterino::commands { | ||
|
||
QString toggleShieldMode(const CommandContext &ctx, bool isActivating) | ||
{ | ||
const QString command = | ||
isActivating ? QStringLiteral("/shield") : QStringLiteral("/shieldoff"); | ||
|
||
if (ctx.twitchChannel == nullptr) | ||
{ | ||
ctx.channel->addMessage(makeSystemMessage( | ||
QStringLiteral("The %1 command only works in Twitch channels") | ||
.arg(command))); | ||
return {}; | ||
} | ||
|
||
auto user = getApp()->accounts->twitch.getCurrent(); | ||
|
||
// Avoid Helix calls without Client ID and/or OAuth Token | ||
if (user->isAnon()) | ||
{ | ||
ctx.channel->addMessage(makeSystemMessage( | ||
QStringLiteral("You must be logged in to use the %1 command") | ||
.arg(command))); | ||
return {}; | ||
} | ||
|
||
getHelix()->updateShieldMode( | ||
ctx.twitchChannel->roomId(), user->getUserId(), isActivating, | ||
[channel = ctx.channel](const auto &res) { | ||
if (!res.isActive) | ||
{ | ||
channel->addMessage( | ||
makeSystemMessage("Shield mode was deactivated.")); | ||
return; | ||
} | ||
|
||
channel->addMessage( | ||
makeSystemMessage("Shield mode was activated.")); | ||
}, | ||
[channel = ctx.channel](const auto error, const auto &message) { | ||
using Error = HelixUpdateShieldModeError; | ||
QString errorMessage = "Failed to update shield mode - "; | ||
|
||
switch (error) | ||
{ | ||
case Error::UserMissingScope: { | ||
errorMessage += | ||
"Missing required scope. Re-login with your " | ||
"account and try again."; | ||
} | ||
break; | ||
|
||
case Error::MissingPermission: { | ||
errorMessage += "You must be a moderator of the channel."; | ||
} | ||
break; | ||
|
||
case Error::Forwarded: { | ||
errorMessage += message; | ||
} | ||
break; | ||
|
||
case Error::Unknown: | ||
default: { | ||
errorMessage += | ||
QString("An unknown error has occurred (%1).") | ||
.arg(message); | ||
} | ||
break; | ||
} | ||
channel->addMessage(makeSystemMessage(errorMessage)); | ||
}); | ||
|
||
return {}; | ||
} | ||
|
||
QString shieldModeOn(const CommandContext &ctx) | ||
{ | ||
return toggleShieldMode(ctx, true); | ||
} | ||
|
||
QString shieldModeOff(const CommandContext &ctx) | ||
{ | ||
return toggleShieldMode(ctx, false); | ||
} | ||
|
||
} // namespace chatterino::commands |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#pragma once | ||
|
||
#include <QString> | ||
|
||
namespace chatterino { | ||
|
||
struct CommandContext; | ||
|
||
} // namespace chatterino | ||
|
||
namespace chatterino::commands { | ||
|
||
QString shieldModeOn(const CommandContext &ctx); | ||
QString shieldModeOff(const CommandContext &ctx); | ||
|
||
} // namespace chatterino::commands |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters