Skip to content

Commit

Permalink
Add ban user by id command /banid (#4411)
Browse files Browse the repository at this point in the history
Co-authored-by: Felanbird <41973452+Felanbird@users.noreply.github.com>
Co-authored-by: pajlada <rasmus.karlsson@pajlada.com>
  • Loading branch information
3 people authored Feb 26, 2023
1 parent f9b2388 commit b5b8550
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
55 changes: 51 additions & 4 deletions src/controllers/commands/CommandController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -2829,6 +2829,53 @@ void CommandController::initialize(Settings &, Paths &paths)
return "";
});

this->registerCommand("/banid", [formatBanTimeoutError](
const QStringList &words,
auto channel) {
auto *twitchChannel = dynamic_cast<TwitchChannel *>(channel.get());
if (twitchChannel == nullptr)
{
channel->addMessage(makeSystemMessage(
QString("The /banid command only works in Twitch channels")));
return "";
}

const auto *usageStr =
"Usage: \"/banid <userID> [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) {
Expand Down

0 comments on commit b5b8550

Please sign in to comment.