add "/shoutout" command (#4638)

Co-authored-by: Rasmus Karlsson <rasmus.karlsson@pajlada.com>
This commit is contained in:
olafyang 2023-05-20 18:32:06 +02:00 committed by GitHub
parent e1a6c24cf3
commit 21d4b2cacc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 274 additions and 0 deletions

View file

@ -2,6 +2,7 @@
## Unversioned ## Unversioned
- Minor: Added `/shoutout <username>` commands to shoutout specified user. (#4638)
- Dev: Added command to set Qt's logging filter/rules at runtime (`/c2-set-logging-rules`). (#4637) - Dev: Added command to set Qt's logging filter/rules at runtime (`/c2-set-logging-rules`). (#4637)
- Dev: Added the ability to see & load custom themes from the Themes directory. No stable promises are made of this feature, changes might be made that breaks custom themes without notice. (#4570) - Dev: Added the ability to see & load custom themes from the Themes directory. No stable promises are made of this feature, changes might be made that breaks custom themes without notice. (#4570)

View file

@ -379,6 +379,14 @@ public:
failureCallback)), failureCallback)),
(override)); (override));
// /shoutout
MOCK_METHOD(
void, sendShoutout,
(QString fromBroadcasterID, QString toBroadcasterID,
QString moderatorID, ResultCallback<> successCallback,
(FailureCallback<HelixSendShoutoutError, QString> failureCallback)),
(override));
MOCK_METHOD(void, update, (QString clientId, QString oauthToken), MOCK_METHOD(void, update, (QString clientId, QString oauthToken),
(override)); (override));

View file

@ -66,6 +66,8 @@ set(SOURCE_FILES
controllers/commands/builtin/twitch/ChatSettings.hpp controllers/commands/builtin/twitch/ChatSettings.hpp
controllers/commands/builtin/twitch/ShieldMode.cpp controllers/commands/builtin/twitch/ShieldMode.cpp
controllers/commands/builtin/twitch/ShieldMode.hpp controllers/commands/builtin/twitch/ShieldMode.hpp
controllers/commands/builtin/twitch/Shoutout.cpp
controllers/commands/builtin/twitch/Shoutout.hpp
controllers/commands/CommandContext.hpp controllers/commands/CommandContext.hpp
controllers/commands/CommandController.cpp controllers/commands/CommandController.cpp
controllers/commands/CommandController.hpp controllers/commands/CommandController.hpp

View file

@ -10,6 +10,7 @@
#include "controllers/commands/builtin/chatterino/Debugging.hpp" #include "controllers/commands/builtin/chatterino/Debugging.hpp"
#include "controllers/commands/builtin/twitch/ChatSettings.hpp" #include "controllers/commands/builtin/twitch/ChatSettings.hpp"
#include "controllers/commands/builtin/twitch/ShieldMode.hpp" #include "controllers/commands/builtin/twitch/ShieldMode.hpp"
#include "controllers/commands/builtin/twitch/Shoutout.hpp"
#include "controllers/commands/Command.hpp" #include "controllers/commands/Command.hpp"
#include "controllers/commands/CommandContext.hpp" #include "controllers/commands/CommandContext.hpp"
#include "controllers/commands/CommandModel.hpp" #include "controllers/commands/CommandModel.hpp"
@ -3213,6 +3214,8 @@ void CommandController::initialize(Settings &, Paths &paths)
this->registerCommand("/shield", &commands::shieldModeOn); this->registerCommand("/shield", &commands::shieldModeOn);
this->registerCommand("/shieldoff", &commands::shieldModeOff); this->registerCommand("/shieldoff", &commands::shieldModeOff);
this->registerCommand("/shoutout", &commands::sendShoutout);
this->registerCommand("/c2-set-logging-rules", &commands::setLoggingRules); this->registerCommand("/c2-set-logging-rules", &commands::setLoggingRules);
} }

View file

@ -0,0 +1,112 @@
#include "controllers/commands/builtin/twitch/Shoutout.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 sendShoutout(const CommandContext &ctx)
{
auto *twitchChannel = ctx.twitchChannel;
auto channel = ctx.channel;
auto words = &ctx.words;
if (twitchChannel == nullptr)
{
channel->addMessage(makeSystemMessage(
"The /shoutout command only works in Twitch channels"));
return "";
}
auto currentUser = getApp()->accounts->twitch.getCurrent();
if (currentUser->isAnon())
{
channel->addMessage(
makeSystemMessage("You must be logged in to send shoutout"));
return "";
}
if (words->size() < 2)
{
channel->addMessage(
makeSystemMessage("Usage: \"/shoutout <username>\" - Sends a "
"shoutout to the specified twitch user"));
return "";
}
const auto target = words->at(1);
using Error = HelixSendShoutoutError;
getHelix()->getUserByName(
target,
[twitchChannel, channel, currentUser, &target](const auto targetUser) {
getHelix()->sendShoutout(
twitchChannel->roomId(), targetUser.id,
currentUser->getUserId(),
[channel, targetUser]() {
channel->addMessage(makeSystemMessage(
QString("Sent shoutout to %1").arg(targetUser.login)));
},
[channel](auto error, auto message) {
QString errorMessage = "Failed to send shoutout - ";
switch (error)
{
case Error::UserNotAuthorized: {
errorMessage += "You don't have permission to "
"perform that action.";
}
break;
case Error::UserMissingScope: {
errorMessage += "Missing required scope. "
"Re-login with your "
"account and try again.";
}
break;
case Error::Ratelimited: {
errorMessage +=
"You are being ratelimited by Twitch. "
"Try again in a few seconds.";
}
break;
case Error::UserIsBroadcaster: {
errorMessage += "The broadcaster may not give "
"themselves a Shoutout.";
}
break;
case Error::BroadcasterNotLive: {
errorMessage +=
"The broadcaster is not streaming live or "
"does not have one or more viewers.";
}
break;
case Error::Unknown: {
errorMessage += message;
}
break;
}
channel->addMessage(makeSystemMessage(errorMessage));
});
},
[channel, target] {
// Equivalent error from IRC
channel->addMessage(
makeSystemMessage(QString("Invalid username: %1").arg(target)));
});
return "";
}
} // namespace chatterino::commands

View file

@ -0,0 +1,15 @@
#pragma once
#include <QString>
namespace chatterino {
struct CommandContext;
} // namespace chatterino
namespace chatterino::commands {
QString sendShoutout(const CommandContext &ctx);
} // namespace chatterino::commands

View file

@ -2597,6 +2597,107 @@ void Helix::updateShieldMode(
.execute(); .execute();
} }
// https://dev.twitch.tv/docs/api/reference/#send-a-shoutout
void Helix::sendShoutout(
QString fromBroadcasterID, QString toBroadcasterID, QString moderatorID,
ResultCallback<> successCallback,
FailureCallback<HelixSendShoutoutError, QString> failureCallback)
{
using Error = HelixSendShoutoutError;
QUrlQuery urlQuery;
urlQuery.addQueryItem("from_broadcaster_id", fromBroadcasterID);
urlQuery.addQueryItem("to_broadcaster_id", toBroadcasterID);
urlQuery.addQueryItem("moderator_id", moderatorID);
this->makePost("chat/shoutouts", urlQuery)
.header("Content-Type", "application/json")
.onSuccess([successCallback](NetworkResult result) -> Outcome {
if (result.status() != 204)
{
qCWarning(chatterinoTwitch)
<< "Success result for sending shoutout was "
<< result.status() << "but we expected it to be 204";
}
successCallback();
return Success;
})
.onError([failureCallback](NetworkResult result) -> void {
const auto obj = result.parseJson();
auto message = obj["message"].toString();
switch (result.status())
{
case 400: {
if (message.startsWith("The broadcaster may not give "
"themselves a Shoutout.",
Qt::CaseInsensitive))
{
failureCallback(Error::UserIsBroadcaster, message);
}
else if (message.startsWith(
"The broadcaster is not streaming live or "
"does not have one or more viewers.",
Qt::CaseInsensitive))
{
failureCallback(Error::BroadcasterNotLive, message);
}
else
{
failureCallback(Error::UserNotAuthorized, message);
}
}
break;
case 401: {
if (message.startsWith("Missing scope",
Qt::CaseInsensitive))
{
failureCallback(Error::UserMissingScope, message);
}
else
{
failureCallback(Error::UserNotAuthorized, message);
}
}
break;
case 403: {
failureCallback(Error::UserNotAuthorized, message);
}
break;
case 429: {
failureCallback(Error::Ratelimited, message);
}
break;
case 500: {
// Helix returns 500 when user is not mod,
if (message.isEmpty())
{
failureCallback(Error::Unknown,
"Twitch internal server error");
}
else
{
failureCallback(Error::Unknown, message);
}
}
break;
default: {
qCWarning(chatterinoTwitch)
<< "Helix send shoutout, unhandled error data:"
<< result.status() << result.getData() << obj;
failureCallback(Error::Unknown, message);
}
}
})
.execute();
}
NetworkRequest Helix::makeRequest(const QString &url, const QUrlQuery &urlQuery, NetworkRequest Helix::makeRequest(const QString &url, const QUrlQuery &urlQuery,
NetworkRequestType type) NetworkRequestType type)
{ {

View file

@ -637,6 +637,18 @@ enum class HelixListVIPsError { // /vips
Forwarded, Forwarded,
}; // /vips }; // /vips
enum class HelixSendShoutoutError {
Unknown,
// 400
UserIsBroadcaster,
BroadcasterNotLive,
// 401
UserNotAuthorized,
UserMissingScope,
Ratelimited,
};
struct HelixStartCommercialResponse { struct HelixStartCommercialResponse {
// Length of the triggered commercial // Length of the triggered commercial
int length; int length;
@ -1013,6 +1025,12 @@ public:
FailureCallback<HelixUpdateShieldModeError, QString> FailureCallback<HelixUpdateShieldModeError, QString>
failureCallback) = 0; failureCallback) = 0;
// https://dev.twitch.tv/docs/api/reference/#send-a-shoutout
virtual void sendShoutout(
QString fromBroadcasterID, QString toBroadcasterID, QString moderatorID,
ResultCallback<> successCallback,
FailureCallback<HelixSendShoutoutError, QString> failureCallback) = 0;
virtual void update(QString clientId, QString oauthToken) = 0; virtual void update(QString clientId, QString oauthToken) = 0;
protected: protected:
@ -1318,6 +1336,12 @@ public:
FailureCallback<HelixUpdateShieldModeError, QString> FailureCallback<HelixUpdateShieldModeError, QString>
failureCallback) final; failureCallback) final;
// https://dev.twitch.tv/docs/api/reference/#send-a-shoutout
void sendShoutout(
QString fromBroadcasterID, QString toBroadcasterID, QString moderatorID,
ResultCallback<> successCallback,
FailureCallback<HelixSendShoutoutError, QString> failureCallback) final;
void update(QString clientId, QString oauthToken) final; void update(QString clientId, QString oauthToken) final;
static void initialize(); static void initialize();

View file

@ -170,6 +170,14 @@ URL: https://dev.twitch.tv/docs/api/reference/#get-chatters
Used for the viewer list for moderators/broadcasters. Used for the viewer list for moderators/broadcasters.
### Send Shoutout
URL: https://dev.twitch.tv/docs/api/reference/#send-a-shoutout
Used in:
- `controllers/commands/CommandController.cpp` to send Twitch native shoutout using "/shoutout <username>"
## PubSub ## PubSub
### Whispers ### Whispers