Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ban user by id command /banid #4411

Merged
merged 7 commits into from
Feb 26, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
iProdigy marked this conversation as resolved.
Show resolved Hide resolved
- 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
56 changes: 52 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,54 @@ void CommandController::initialize(Settings &, Paths &paths)
return "";
});

this->registerCommand("/banid", [formatBanTimeoutError](
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Open to other suggestions for the command name

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this name is fine for me

const QStringList &words,
iProdigy marked this conversation as resolved.
Show resolved Hide resolved
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 "
"ID from chatting. Reason is optional and will be shown to the "
"target user and other moderators. Use \"/unban <username>\" to "
"remove a ban.";
iProdigy marked this conversation as resolved.
Show resolved Hide resolved
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));
Felanbird marked this conversation as resolved.
Show resolved Hide resolved
});

return "";
});

for (const auto &cmd : TWITCH_WHISPER_COMMANDS)
{
this->registerCommand(cmd, [](const QStringList &words, auto channel) {
Expand Down