From b5b85501ee4446961b5f3d412c441de6adaaabc3 Mon Sep 17 00:00:00 2001 From: iProdigy <8106344+iProdigy@users.noreply.github.com> Date: Sun, 26 Feb 2023 12:03:14 -0800 Subject: [PATCH] Add ban user by id command `/banid` (#4411) Co-authored-by: Felanbird <41973452+Felanbird@users.noreply.github.com> Co-authored-by: pajlada --- CHANGELOG.md | 1 + .../commands/CommandController.cpp | 55 +++++++++++++++++-- 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d0bddeaad..36505727d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unversioned - Minor: Delete all but the last 5 crashdumps on application start. (#4392) +- Minor: Added `/banid` command that allows banning by user ID. (#4411) - Bugfix: Fixed uploaded AppImage not being able most web requests. (#4400) - Bugfix: Fixed a potential race condition due to using the wrong lock when loading 7TV badges. (#4402) - Dev: Add capability to build Chatterino with Qt6. (#4393) diff --git a/src/controllers/commands/CommandController.cpp b/src/controllers/commands/CommandController.cpp index 42fa76e4f..a77dc722f 100644 --- a/src/controllers/commands/CommandController.cpp +++ b/src/controllers/commands/CommandController.cpp @@ -2632,7 +2632,7 @@ void CommandController::initialize(Settings &, Paths &paths) auto formatBanTimeoutError = [](const char *operation, HelixBanUserError error, - const QString &message, const QString &userDisplayName) -> QString { + const QString &message, const QString &userTarget) -> QString { using Error = HelixBanUserError; QString errorMessage = QString("Failed to %1 user - ").arg(operation); @@ -2659,7 +2659,7 @@ void CommandController::initialize(Settings &, Paths &paths) case Error::TargetBanned: { // Equivalent IRC error errorMessage += QString("%1 is already banned in this channel.") - .arg(userDisplayName); + .arg(userTarget); } break; @@ -2669,8 +2669,8 @@ void CommandController::initialize(Settings &, Paths &paths) // The messages from IRC are formatted like this: // "You cannot {op} moderator {mod} unless you are the owner of this channel." // "You cannot {op} the broadcaster." - errorMessage += QString("You cannot %1 %2.") - .arg(operation, userDisplayName); + errorMessage += + QString("You cannot %1 %2.").arg(operation, userTarget); } break; @@ -2829,6 +2829,53 @@ void CommandController::initialize(Settings &, Paths &paths) return ""; }); + this->registerCommand("/banid", [formatBanTimeoutError]( + const QStringList &words, + auto channel) { + auto *twitchChannel = dynamic_cast(channel.get()); + if (twitchChannel == nullptr) + { + channel->addMessage(makeSystemMessage( + QString("The /banid command only works in Twitch channels"))); + return ""; + } + + const auto *usageStr = + "Usage: \"/banid [reason]\" - Permanently prevent a user " + "from chatting via their user ID. Reason is optional and will be " + "shown to the target user and other moderators."; + if (words.size() < 2) + { + channel->addMessage(makeSystemMessage(usageStr)); + return ""; + } + + auto currentUser = getApp()->accounts->twitch.getCurrent(); + if (currentUser->isAnon()) + { + channel->addMessage( + makeSystemMessage("You must be logged in to ban someone!")); + return ""; + } + + auto target = words.at(1); + auto reason = words.mid(2).join(' '); + + getHelix()->banUser( + twitchChannel->roomId(), currentUser->getUserId(), target, + boost::none, reason, + [] { + // No response for bans, they're emitted over pubsub/IRC instead + }, + [channel, target, formatBanTimeoutError](auto error, auto message) { + auto errorMessage = + formatBanTimeoutError("ban", error, message, "#" + target); + channel->addMessage(makeSystemMessage(errorMessage)); + }); + + return ""; + }); + for (const auto &cmd : TWITCH_WHISPER_COMMANDS) { this->registerCommand(cmd, [](const QStringList &words, auto channel) {