2019-09-08 22:27:57 +02:00
|
|
|
#include "CommandController.hpp"
|
|
|
|
|
|
|
|
#include "Application.hpp"
|
|
|
|
#include "common/SignalVector.hpp"
|
|
|
|
#include "controllers/accounts/AccountController.hpp"
|
|
|
|
#include "controllers/commands/Command.hpp"
|
|
|
|
#include "controllers/commands/CommandModel.hpp"
|
|
|
|
#include "messages/Message.hpp"
|
|
|
|
#include "messages/MessageBuilder.hpp"
|
|
|
|
#include "messages/MessageElement.hpp"
|
2019-09-18 13:03:16 +02:00
|
|
|
#include "providers/twitch/TwitchIrcServer.hpp"
|
2020-03-14 12:13:57 +01:00
|
|
|
#include "providers/twitch/api/Helix.hpp"
|
2019-09-08 22:27:57 +02:00
|
|
|
#include "singletons/Emotes.hpp"
|
|
|
|
#include "singletons/Paths.hpp"
|
|
|
|
#include "singletons/Settings.hpp"
|
|
|
|
#include "singletons/Theme.hpp"
|
2020-10-31 16:42:48 +01:00
|
|
|
#include "singletons/WindowManager.hpp"
|
2019-09-08 22:27:57 +02:00
|
|
|
#include "util/CombinePath.hpp"
|
2021-01-30 15:39:01 +01:00
|
|
|
#include "util/FormatTime.hpp"
|
2020-05-10 12:45:19 +02:00
|
|
|
#include "util/Twitch.hpp"
|
2020-10-31 16:42:48 +01:00
|
|
|
#include "widgets/Window.hpp"
|
2020-01-25 12:59:31 +01:00
|
|
|
#include "widgets/dialogs/UserInfoPopup.hpp"
|
2019-09-08 22:27:57 +02:00
|
|
|
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QRegularExpression>
|
|
|
|
|
2020-09-26 10:01:00 +02:00
|
|
|
#define TWITCH_DEFAULT_COMMANDS \
|
|
|
|
"/help", "/w", "/me", "/disconnect", "/mods", "/color", "/ban", "/unban", \
|
|
|
|
"/timeout", "/untimeout", "/slow", "/slowoff", "/r9kbeta", \
|
|
|
|
"/r9kbetaoff", "/emoteonly", "/emoteonlyoff", "/clear", \
|
|
|
|
"/subscribers", "/subscribersoff", "/followers", "/followersoff"
|
2019-09-08 22:27:57 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
using namespace chatterino;
|
|
|
|
|
|
|
|
static const QStringList whisperCommands{"/w", ".w"};
|
|
|
|
|
|
|
|
void sendWhisperMessage(const QString &text)
|
|
|
|
{
|
|
|
|
// (hemirt) pajlada: "we should not be sending whispers through jtv, but
|
|
|
|
// rather to your own username"
|
|
|
|
auto app = getApp();
|
|
|
|
app->twitch.server->sendMessage("jtv", text.simplified());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool appendWhisperMessageWordsLocally(const QStringList &words)
|
|
|
|
{
|
|
|
|
auto app = getApp();
|
|
|
|
|
|
|
|
MessageBuilder b;
|
|
|
|
|
|
|
|
b.emplace<TimestampElement>();
|
|
|
|
b.emplace<TextElement>(app->accounts->twitch.getCurrent()->getUserName(),
|
|
|
|
MessageElementFlag::Text, MessageColor::Text,
|
|
|
|
FontStyle::ChatMediumBold);
|
|
|
|
b.emplace<TextElement>("->", MessageElementFlag::Text,
|
|
|
|
getApp()->themes->messages.textColors.system);
|
|
|
|
b.emplace<TextElement>(words[1] + ":", MessageElementFlag::Text,
|
|
|
|
MessageColor::Text, FontStyle::ChatMediumBold);
|
|
|
|
|
|
|
|
const auto &acc = app->accounts->twitch.getCurrent();
|
|
|
|
const auto &accemotes = *acc->accessEmotes();
|
|
|
|
const auto &bttvemotes = app->twitch.server->getBttvEmotes();
|
|
|
|
const auto &ffzemotes = app->twitch.server->getFfzEmotes();
|
|
|
|
auto flags = MessageElementFlags();
|
|
|
|
auto emote = boost::optional<EmotePtr>{};
|
|
|
|
for (int i = 2; i < words.length(); i++)
|
|
|
|
{
|
|
|
|
{ // twitch emote
|
|
|
|
auto it = accemotes.emotes.find({words[i]});
|
|
|
|
if (it != accemotes.emotes.end())
|
|
|
|
{
|
|
|
|
b.emplace<EmoteElement>(it->second,
|
|
|
|
MessageElementFlag::TwitchEmote);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} // twitch emote
|
|
|
|
|
|
|
|
{ // bttv/ffz emote
|
|
|
|
if ((emote = bttvemotes.emote({words[i]})))
|
|
|
|
{
|
|
|
|
flags = MessageElementFlag::BttvEmote;
|
|
|
|
}
|
|
|
|
else if ((emote = ffzemotes.emote({words[i]})))
|
|
|
|
{
|
|
|
|
flags = MessageElementFlag::FfzEmote;
|
|
|
|
}
|
|
|
|
if (emote)
|
|
|
|
{
|
|
|
|
b.emplace<EmoteElement>(emote.get(), flags);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} // bttv/ffz emote
|
|
|
|
{ // emoji/text
|
|
|
|
for (auto &variant : app->emotes->emojis.parse(words[i]))
|
|
|
|
{
|
|
|
|
constexpr const static struct {
|
|
|
|
void operator()(EmotePtr emote, MessageBuilder &b) const
|
|
|
|
{
|
|
|
|
b.emplace<EmoteElement>(emote,
|
|
|
|
MessageElementFlag::EmojiAll);
|
|
|
|
}
|
|
|
|
void operator()(const QString &string,
|
|
|
|
MessageBuilder &b) const
|
|
|
|
{
|
|
|
|
auto linkString = b.matchLink(string);
|
|
|
|
if (linkString.isEmpty())
|
|
|
|
{
|
|
|
|
b.emplace<TextElement>(string,
|
|
|
|
MessageElementFlag::Text);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
b.addLink(string, linkString);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} visitor;
|
2020-11-08 12:02:19 +01:00
|
|
|
boost::apply_visitor(
|
|
|
|
[&b](auto &&arg) {
|
|
|
|
visitor(arg, b);
|
|
|
|
},
|
|
|
|
variant);
|
2019-09-08 22:27:57 +02:00
|
|
|
} // emoji/text
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b->flags.set(MessageFlag::DoNotTriggerNotification);
|
|
|
|
b->flags.set(MessageFlag::Whisper);
|
|
|
|
auto messagexD = b.release();
|
|
|
|
|
|
|
|
app->twitch.server->whispersChannel->addMessage(messagexD);
|
|
|
|
|
|
|
|
auto overrideFlags = boost::optional<MessageFlags>(messagexD->flags);
|
|
|
|
overrideFlags->set(MessageFlag::DoNotLog);
|
|
|
|
|
|
|
|
if (getSettings()->inlineWhispers)
|
|
|
|
{
|
|
|
|
app->twitch.server->forEachChannel(
|
|
|
|
[&messagexD, overrideFlags](ChannelPtr _channel) {
|
|
|
|
_channel->addMessage(messagexD, overrideFlags);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool appendWhisperMessageStringLocally(const QString &textNoEmoji)
|
|
|
|
{
|
|
|
|
QString text = getApp()->emotes->emojis.replaceShortCodes(textNoEmoji);
|
|
|
|
QStringList words = text.split(' ', QString::SkipEmptyParts);
|
|
|
|
|
|
|
|
if (words.length() == 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString commandName = words[0];
|
|
|
|
|
|
|
|
if (whisperCommands.contains(commandName, Qt::CaseInsensitive))
|
|
|
|
{
|
|
|
|
if (words.length() > 2)
|
|
|
|
{
|
|
|
|
return appendWhisperMessageWordsLocally(words);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
|
|
|
|
void CommandController::initialize(Settings &, Paths &paths)
|
|
|
|
{
|
2020-09-26 10:01:00 +02:00
|
|
|
this->commandAutoCompletions_ = QStringList{TWITCH_DEFAULT_COMMANDS};
|
|
|
|
|
2019-09-08 22:27:57 +02:00
|
|
|
// Update commands map when the vector of commands has been updated
|
|
|
|
auto addFirstMatchToMap = [this](auto args) {
|
2020-09-26 10:01:00 +02:00
|
|
|
this->userCommands_.remove(args.item.name);
|
2019-09-08 22:27:57 +02:00
|
|
|
|
|
|
|
for (const Command &cmd : this->items_)
|
|
|
|
{
|
|
|
|
if (cmd.name == args.item.name)
|
|
|
|
{
|
2020-09-26 10:01:00 +02:00
|
|
|
this->userCommands_[cmd.name] = cmd;
|
2019-09-08 22:27:57 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int maxSpaces = 0;
|
|
|
|
|
|
|
|
for (const Command &cmd : this->items_)
|
|
|
|
{
|
|
|
|
auto localMaxSpaces = cmd.name.count(' ');
|
|
|
|
if (localMaxSpaces > maxSpaces)
|
|
|
|
{
|
|
|
|
maxSpaces = localMaxSpaces;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this->maxSpaces_ = maxSpaces;
|
|
|
|
};
|
|
|
|
this->items_.itemInserted.connect(addFirstMatchToMap);
|
|
|
|
this->items_.itemRemoved.connect(addFirstMatchToMap);
|
|
|
|
|
|
|
|
// Initialize setting manager for commands.json
|
|
|
|
auto path = combinePath(paths.settingsDirectory, "commands.json");
|
|
|
|
this->sm_ = std::make_shared<pajlada::Settings::SettingManager>();
|
|
|
|
this->sm_->setPath(path.toStdString());
|
|
|
|
|
|
|
|
// Delayed initialization of the setting storing all commands
|
|
|
|
this->commandsSetting_.reset(
|
|
|
|
new pajlada::Settings::Setting<std::vector<Command>>("/commands",
|
|
|
|
this->sm_));
|
|
|
|
|
|
|
|
// Update the setting when the vector of commands has been updated (most
|
|
|
|
// likely from the settings dialog)
|
2020-11-08 12:02:19 +01:00
|
|
|
this->items_.delayedItemsChanged.connect([this] {
|
2020-02-23 17:45:59 +01:00
|
|
|
this->commandsSetting_->setValue(this->items_.raw());
|
2019-09-08 22:27:57 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// Load commands from commands.json
|
|
|
|
this->sm_->load();
|
|
|
|
|
|
|
|
// Add loaded commands to our vector of commands (which will update the map
|
|
|
|
// of commands)
|
|
|
|
for (const auto &command : this->commandsSetting_->getValue())
|
|
|
|
{
|
2020-02-23 19:44:13 +01:00
|
|
|
this->items_.append(command);
|
2019-09-08 22:27:57 +02:00
|
|
|
}
|
|
|
|
|
2020-09-26 10:01:00 +02:00
|
|
|
this->registerCommand("/debug-args", [](const auto &words, auto channel) {
|
|
|
|
QString msg = QApplication::instance()->arguments().join(' ');
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2020-09-26 10:01:00 +02:00
|
|
|
channel->addMessage(makeSystemMessage(msg));
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2020-09-26 10:01:00 +02:00
|
|
|
return "";
|
|
|
|
});
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2020-09-26 10:01:00 +02:00
|
|
|
this->registerCommand("/uptime", [](const auto &words, auto channel) {
|
|
|
|
auto *twitchChannel = dynamic_cast<TwitchChannel *>(channel.get());
|
|
|
|
if (twitchChannel == nullptr)
|
|
|
|
{
|
|
|
|
channel->addMessage(makeSystemMessage(
|
|
|
|
"The /uptime command only works in Twitch Channels"));
|
|
|
|
return "";
|
|
|
|
}
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2020-09-26 10:01:00 +02:00
|
|
|
const auto &streamStatus = twitchChannel->accessStreamStatus();
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2020-09-26 10:01:00 +02:00
|
|
|
QString messageText =
|
|
|
|
streamStatus->live ? streamStatus->uptime : "Channel is not live.";
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2020-09-26 10:01:00 +02:00
|
|
|
channel->addMessage(makeSystemMessage(messageText));
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2020-09-26 10:01:00 +02:00
|
|
|
return "";
|
|
|
|
});
|
|
|
|
|
|
|
|
this->registerCommand("/ignore", [](const auto &words, auto channel) {
|
|
|
|
if (words.size() < 2)
|
|
|
|
{
|
|
|
|
channel->addMessage(makeSystemMessage("Usage: /ignore [user]"));
|
2019-09-08 22:27:57 +02:00
|
|
|
return "";
|
|
|
|
}
|
2020-09-26 10:01:00 +02:00
|
|
|
auto app = getApp();
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2020-09-26 10:01:00 +02:00
|
|
|
auto user = app->accounts->twitch.getCurrent();
|
|
|
|
auto target = words.at(1);
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2020-09-26 10:01:00 +02:00
|
|
|
if (user->isAnon())
|
2019-09-08 22:27:57 +02:00
|
|
|
{
|
2020-09-26 10:01:00 +02:00
|
|
|
channel->addMessage(
|
|
|
|
makeSystemMessage("You must be logged in to ignore someone"));
|
2019-09-08 22:27:57 +02:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2020-09-26 10:01:00 +02:00
|
|
|
user->ignore(target,
|
|
|
|
[channel](auto resultCode, const QString &message) {
|
|
|
|
channel->addMessage(makeSystemMessage(message));
|
|
|
|
});
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2020-09-26 10:01:00 +02:00
|
|
|
return "";
|
|
|
|
});
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2020-09-26 10:01:00 +02:00
|
|
|
this->registerCommand("/unignore", [](const auto &words, auto channel) {
|
|
|
|
if (words.size() < 2)
|
|
|
|
{
|
|
|
|
channel->addMessage(makeSystemMessage("Usage: /unignore [user]"));
|
2019-09-08 22:27:57 +02:00
|
|
|
return "";
|
|
|
|
}
|
2020-09-26 10:01:00 +02:00
|
|
|
auto app = getApp();
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2020-09-26 10:01:00 +02:00
|
|
|
auto user = app->accounts->twitch.getCurrent();
|
|
|
|
auto target = words.at(1);
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2020-09-26 10:01:00 +02:00
|
|
|
if (user->isAnon())
|
|
|
|
{
|
|
|
|
channel->addMessage(
|
|
|
|
makeSystemMessage("You must be logged in to ignore someone"));
|
2019-09-08 22:27:57 +02:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2020-09-26 10:01:00 +02:00
|
|
|
user->unignore(target,
|
|
|
|
[channel](auto resultCode, const QString &message) {
|
|
|
|
channel->addMessage(makeSystemMessage(message));
|
|
|
|
});
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2020-09-26 10:01:00 +02:00
|
|
|
return "";
|
|
|
|
});
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2020-09-26 10:01:00 +02:00
|
|
|
this->registerCommand("/follow", [](const auto &words, auto channel) {
|
|
|
|
if (words.size() < 2)
|
|
|
|
{
|
|
|
|
channel->addMessage(makeSystemMessage("Usage: /follow [user]"));
|
2019-09-08 22:27:57 +02:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2020-12-22 09:55:58 +01:00
|
|
|
auto currentUser = getApp()->accounts->twitch.getCurrent();
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2020-12-22 09:55:58 +01:00
|
|
|
if (currentUser->isAnon())
|
2020-09-26 10:01:00 +02:00
|
|
|
{
|
|
|
|
channel->addMessage(
|
|
|
|
makeSystemMessage("You must be logged in to follow someone"));
|
|
|
|
return "";
|
|
|
|
}
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2020-12-22 09:55:58 +01:00
|
|
|
auto target = words.at(1);
|
|
|
|
|
2020-09-26 10:01:00 +02:00
|
|
|
getHelix()->getUserByName(
|
|
|
|
target,
|
2020-12-22 09:55:58 +01:00
|
|
|
[currentUser, channel, target](const auto &targetUser) {
|
|
|
|
getHelix()->followUser(
|
|
|
|
currentUser->getUserId(), targetUser.id,
|
|
|
|
[channel, target]() {
|
|
|
|
channel->addMessage(makeSystemMessage(
|
|
|
|
"You successfully followed " + target));
|
|
|
|
},
|
|
|
|
[channel, target]() {
|
|
|
|
channel->addMessage(makeSystemMessage(
|
|
|
|
QString("User %1 could not be followed, an unknown "
|
2021-01-17 20:19:10 +01:00
|
|
|
"error occurred!")
|
2020-12-22 09:55:58 +01:00
|
|
|
.arg(target)));
|
|
|
|
});
|
2020-09-26 10:01:00 +02:00
|
|
|
},
|
|
|
|
[channel, target] {
|
2020-12-22 09:55:58 +01:00
|
|
|
channel->addMessage(
|
|
|
|
makeSystemMessage(QString("User %1 could not be followed, "
|
|
|
|
"no user with that name found!")
|
|
|
|
.arg(target)));
|
2020-09-26 10:01:00 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return "";
|
|
|
|
});
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2020-09-26 10:01:00 +02:00
|
|
|
this->registerCommand("/unfollow", [](const auto &words, auto channel) {
|
|
|
|
if (words.size() < 2)
|
|
|
|
{
|
|
|
|
channel->addMessage(makeSystemMessage("Usage: /unfollow [user]"));
|
2019-09-08 22:27:57 +02:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2020-12-22 09:55:58 +01:00
|
|
|
auto currentUser = getApp()->accounts->twitch.getCurrent();
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2020-12-22 09:55:58 +01:00
|
|
|
if (currentUser->isAnon())
|
2020-09-26 10:01:00 +02:00
|
|
|
{
|
|
|
|
channel->addMessage(
|
|
|
|
makeSystemMessage("You must be logged in to follow someone"));
|
|
|
|
return "";
|
|
|
|
}
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2020-12-22 09:55:58 +01:00
|
|
|
auto target = words.at(1);
|
|
|
|
|
2020-09-26 10:01:00 +02:00
|
|
|
getHelix()->getUserByName(
|
|
|
|
target,
|
2020-12-22 09:55:58 +01:00
|
|
|
[currentUser, channel, target](const auto &targetUser) {
|
|
|
|
getHelix()->unfollowUser(
|
|
|
|
currentUser->getUserId(), targetUser.id,
|
|
|
|
[channel, target]() {
|
|
|
|
channel->addMessage(makeSystemMessage(
|
|
|
|
"You successfully unfollowed " + target));
|
|
|
|
},
|
|
|
|
[channel, target]() {
|
|
|
|
channel->addMessage(makeSystemMessage(
|
|
|
|
"An error occurred while unfollowing " + target));
|
|
|
|
});
|
2020-09-26 10:01:00 +02:00
|
|
|
},
|
|
|
|
[channel, target] {
|
|
|
|
channel->addMessage(makeSystemMessage(
|
2020-12-22 09:55:58 +01:00
|
|
|
QString("User %1 could not be followed!").arg(target)));
|
2020-09-26 10:01:00 +02:00
|
|
|
});
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2020-09-26 10:01:00 +02:00
|
|
|
return "";
|
|
|
|
});
|
|
|
|
|
|
|
|
this->registerCommand("/logs", [](const auto & /*words*/, auto channel) {
|
|
|
|
channel->addMessage(makeSystemMessage(
|
|
|
|
"Online logs functionality has been removed. If you're a "
|
|
|
|
"moderator, you can use the /user command"));
|
|
|
|
return "";
|
|
|
|
});
|
|
|
|
|
|
|
|
this->registerCommand("/user", [](const auto &words, auto channel) {
|
|
|
|
if (words.size() < 2)
|
2019-09-08 22:27:57 +02:00
|
|
|
{
|
2020-09-26 10:01:00 +02:00
|
|
|
channel->addMessage(
|
|
|
|
makeSystemMessage("Usage /user [user] (channel)"));
|
2019-09-08 22:27:57 +02:00
|
|
|
return "";
|
|
|
|
}
|
2020-09-26 10:01:00 +02:00
|
|
|
QString channelName = channel->getName();
|
|
|
|
if (words.size() > 2)
|
2019-09-08 22:27:57 +02:00
|
|
|
{
|
2020-09-26 10:01:00 +02:00
|
|
|
channelName = words[2];
|
|
|
|
if (channelName[0] == '#')
|
2019-09-08 22:27:57 +02:00
|
|
|
{
|
2020-09-26 10:01:00 +02:00
|
|
|
channelName.remove(0, 1);
|
2019-09-08 22:27:57 +02:00
|
|
|
}
|
2020-09-26 10:01:00 +02:00
|
|
|
}
|
|
|
|
openTwitchUsercard(channelName, words[1]);
|
2020-05-10 12:45:19 +02:00
|
|
|
|
2020-09-26 10:01:00 +02:00
|
|
|
return "";
|
|
|
|
});
|
|
|
|
|
|
|
|
this->registerCommand("/usercard", [](const auto &words, auto channel) {
|
|
|
|
if (words.size() < 2)
|
|
|
|
{
|
|
|
|
channel->addMessage(makeSystemMessage("Usage /usercard [user]"));
|
2019-09-08 22:27:57 +02:00
|
|
|
return "";
|
|
|
|
}
|
2020-09-26 10:01:00 +02:00
|
|
|
|
2020-10-31 16:42:48 +01:00
|
|
|
auto *userPopup = new UserInfoPopup(
|
|
|
|
getSettings()->autoCloseUserPopup,
|
|
|
|
static_cast<QWidget *>(&(getApp()->windows->getMainWindow())));
|
2020-09-26 10:01:00 +02:00
|
|
|
userPopup->setData(words[1], channel);
|
|
|
|
userPopup->move(QCursor::pos());
|
|
|
|
userPopup->show();
|
|
|
|
return "";
|
|
|
|
});
|
2021-01-16 13:58:11 +01:00
|
|
|
|
|
|
|
this->registerCommand(
|
|
|
|
"/chatters", [](const auto & /*words*/, auto channel) {
|
|
|
|
auto twitchChannel = dynamic_cast<TwitchChannel *>(channel.get());
|
|
|
|
|
|
|
|
if (twitchChannel == nullptr)
|
|
|
|
{
|
|
|
|
channel->addMessage(makeSystemMessage(
|
|
|
|
"The /chatters command only works in Twitch Channels"));
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
channel->addMessage(makeSystemMessage(
|
|
|
|
QString("Chatter count: %1")
|
|
|
|
.arg(QString::number(twitchChannel->chatterCount()))));
|
|
|
|
|
|
|
|
return "";
|
|
|
|
});
|
2021-01-17 14:47:34 +01:00
|
|
|
|
|
|
|
this->registerCommand("/clip", [](const auto &words, auto channel) {
|
|
|
|
if (!channel->isTwitchChannel())
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
auto *twitchChannel = dynamic_cast<TwitchChannel *>(channel.get());
|
|
|
|
|
|
|
|
twitchChannel->createClip();
|
|
|
|
|
|
|
|
return "";
|
|
|
|
});
|
2021-01-30 15:39:01 +01:00
|
|
|
|
|
|
|
this->registerCommand("/marker", [](const QStringList &words,
|
|
|
|
auto channel) {
|
|
|
|
if (!channel->isTwitchChannel())
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Avoid Helix calls without Client ID and/or OAuth Token
|
|
|
|
if (getApp()->accounts->twitch.getCurrent()->isAnon())
|
|
|
|
{
|
|
|
|
channel->addMessage(makeSystemMessage(
|
|
|
|
"You need to be logged in to create stream markers!"));
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
auto *twitchChannel = dynamic_cast<TwitchChannel *>(channel.get());
|
|
|
|
|
|
|
|
// Exact same message as in webchat
|
|
|
|
if (!twitchChannel->isLive())
|
|
|
|
{
|
|
|
|
channel->addMessage(makeSystemMessage(
|
|
|
|
"You can only add stream markers during live streams. Try "
|
|
|
|
"again when the channel is live streaming."));
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
auto arguments = words;
|
|
|
|
arguments.removeFirst();
|
|
|
|
|
|
|
|
getHelix()->createStreamMarker(
|
|
|
|
// Limit for description is 140 characters, webchat just crops description
|
|
|
|
// if it's >140 characters, so we're doing the same thing
|
|
|
|
twitchChannel->roomId(), arguments.join(" ").left(140),
|
|
|
|
[channel, arguments](const HelixStreamMarker &streamMarker) {
|
|
|
|
channel->addMessage(makeSystemMessage(
|
|
|
|
QString("Successfully added a stream marker at %1%2")
|
|
|
|
.arg(formatTime(streamMarker.positionSeconds))
|
|
|
|
.arg(streamMarker.description.isEmpty()
|
|
|
|
? ""
|
|
|
|
: QString(": \"%1\"")
|
|
|
|
.arg(streamMarker.description))));
|
|
|
|
},
|
|
|
|
[channel](auto error) {
|
|
|
|
QString errorMessage("Failed to create stream marker - ");
|
|
|
|
|
|
|
|
switch (error)
|
|
|
|
{
|
|
|
|
case HelixStreamMarkerError::UserNotAuthorized: {
|
|
|
|
errorMessage +=
|
|
|
|
"you don't have permission to perform that action.";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HelixStreamMarkerError::UserNotAuthenticated: {
|
|
|
|
errorMessage += "you need to re-authenticate.";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
// This would most likely happen if the service is down, or if the JSON payload returned has changed format
|
|
|
|
case HelixStreamMarkerError::Unknown:
|
|
|
|
default: {
|
|
|
|
errorMessage += "an unknown error occurred.";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
channel->addMessage(makeSystemMessage(errorMessage));
|
|
|
|
});
|
|
|
|
|
|
|
|
return "";
|
|
|
|
});
|
2020-09-26 10:01:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CommandController::save()
|
|
|
|
{
|
|
|
|
this->sm_->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
CommandModel *CommandController::createModel(QObject *parent)
|
|
|
|
{
|
|
|
|
CommandModel *model = new CommandModel(parent);
|
|
|
|
model->initialize(&this->items_);
|
|
|
|
|
|
|
|
return model;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString CommandController::execCommand(const QString &textNoEmoji,
|
|
|
|
ChannelPtr channel, bool dryRun)
|
|
|
|
{
|
|
|
|
QString text = getApp()->emotes->emojis.replaceShortCodes(textNoEmoji);
|
|
|
|
QStringList words = text.split(' ', QString::SkipEmptyParts);
|
|
|
|
|
|
|
|
if (words.length() == 0)
|
|
|
|
{
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString commandName = words[0];
|
|
|
|
|
|
|
|
// works in a valid twitch channel and /whispers, etc...
|
|
|
|
if (!dryRun && channel->isTwitchChannel())
|
|
|
|
{
|
|
|
|
if (whisperCommands.contains(commandName, Qt::CaseInsensitive))
|
2020-01-25 12:59:31 +01:00
|
|
|
{
|
2020-09-26 10:01:00 +02:00
|
|
|
if (words.length() > 2)
|
2020-01-25 12:59:31 +01:00
|
|
|
{
|
2020-09-26 10:01:00 +02:00
|
|
|
appendWhisperMessageWordsLocally(words);
|
|
|
|
sendWhisperMessage(text);
|
2020-01-25 12:59:31 +01:00
|
|
|
}
|
2020-08-30 11:57:46 +02:00
|
|
|
|
2020-01-25 12:59:31 +01:00
|
|
|
return "";
|
|
|
|
}
|
2019-09-08 22:27:57 +02:00
|
|
|
}
|
|
|
|
|
2020-09-26 10:01:00 +02:00
|
|
|
auto *twitchChannel = dynamic_cast<TwitchChannel *>(channel.get());
|
|
|
|
|
|
|
|
// works only in a valid twitch channel
|
|
|
|
if (!dryRun && twitchChannel != nullptr)
|
2019-09-08 22:27:57 +02:00
|
|
|
{
|
2020-09-26 10:01:00 +02:00
|
|
|
// check if command exists
|
|
|
|
const auto it = this->commands_.find(commandName);
|
|
|
|
if (it != this->commands_.end())
|
|
|
|
{
|
|
|
|
return it.value()(words, channel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// check if user command exists
|
|
|
|
const auto it = this->userCommands_.find(commandName);
|
|
|
|
if (it != this->userCommands_.end())
|
2019-09-08 22:27:57 +02:00
|
|
|
{
|
|
|
|
return this->execCustomCommand(words, it.value(), dryRun);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto maxSpaces = std::min(this->maxSpaces_, words.length() - 1);
|
|
|
|
for (int i = 0; i < maxSpaces; ++i)
|
|
|
|
{
|
|
|
|
commandName += ' ' + words[i + 1];
|
|
|
|
|
2020-09-26 10:01:00 +02:00
|
|
|
const auto it = this->userCommands_.find(commandName);
|
|
|
|
if (it != this->userCommands_.end())
|
2019-09-08 22:27:57 +02:00
|
|
|
{
|
|
|
|
return this->execCustomCommand(words, it.value(), dryRun);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2020-09-26 10:01:00 +02:00
|
|
|
void CommandController::registerCommand(QString commandName,
|
|
|
|
CommandFunction commandFunction)
|
|
|
|
{
|
2021-01-16 13:58:11 +01:00
|
|
|
assert(!this->commands_.contains(commandName));
|
2020-09-26 10:01:00 +02:00
|
|
|
|
|
|
|
this->commands_[commandName] = commandFunction;
|
|
|
|
|
|
|
|
this->commandAutoCompletions_.append(commandName);
|
|
|
|
}
|
|
|
|
|
2019-09-08 22:27:57 +02:00
|
|
|
QString CommandController::execCustomCommand(const QStringList &words,
|
|
|
|
const Command &command,
|
|
|
|
bool dryRun)
|
|
|
|
{
|
|
|
|
QString result;
|
|
|
|
|
|
|
|
static QRegularExpression parseCommand("(^|[^{])({{)*{(\\d+\\+?)}");
|
|
|
|
|
|
|
|
int lastCaptureEnd = 0;
|
|
|
|
|
|
|
|
auto globalMatch = parseCommand.globalMatch(command.func);
|
|
|
|
int matchOffset = 0;
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
QRegularExpressionMatch match =
|
|
|
|
parseCommand.match(command.func, matchOffset);
|
|
|
|
|
|
|
|
if (!match.hasMatch())
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
result += command.func.mid(lastCaptureEnd,
|
|
|
|
match.capturedStart() - lastCaptureEnd + 1);
|
|
|
|
|
|
|
|
lastCaptureEnd = match.capturedEnd();
|
|
|
|
matchOffset = lastCaptureEnd - 1;
|
|
|
|
|
|
|
|
QString wordIndexMatch = match.captured(3);
|
|
|
|
|
|
|
|
bool plus = wordIndexMatch.at(wordIndexMatch.size() - 1) == '+';
|
|
|
|
wordIndexMatch = wordIndexMatch.replace("+", "");
|
|
|
|
|
|
|
|
bool ok;
|
|
|
|
int wordIndex = wordIndexMatch.replace("=", "").toInt(&ok);
|
|
|
|
if (!ok || wordIndex == 0)
|
|
|
|
{
|
|
|
|
result += "{" + match.captured(3) + "}";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (words.length() <= wordIndex)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (plus)
|
|
|
|
{
|
|
|
|
bool first = true;
|
|
|
|
for (int i = wordIndex; i < words.length(); i++)
|
|
|
|
{
|
|
|
|
if (!first)
|
|
|
|
{
|
|
|
|
result += " ";
|
|
|
|
}
|
|
|
|
result += words[i];
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result += words[wordIndex];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result += command.func.mid(lastCaptureEnd);
|
|
|
|
|
|
|
|
if (result.size() > 0 && result.at(0) == '{')
|
|
|
|
{
|
|
|
|
result = result.mid(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto res = result.replace("{{", "{");
|
|
|
|
|
|
|
|
if (dryRun || !appendWhisperMessageStringLocally(res))
|
|
|
|
{
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sendWhisperMessage(res);
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList CommandController::getDefaultTwitchCommandList()
|
|
|
|
{
|
2020-09-26 10:01:00 +02:00
|
|
|
return this->commandAutoCompletions_;
|
2019-09-08 22:27:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace chatterino
|