Support commands with spaces

This commit is contained in:
Rasmus Karlsson 2018-11-03 14:52:38 +01:00
parent a4fd7b5366
commit b469c24154
2 changed files with 334 additions and 320 deletions

View file

@ -47,6 +47,19 @@ void CommandController::initialize(Settings &, Paths &paths)
break;
}
}
int maxSpaces = 0;
for (const Command &cmd : this->items_.getVector())
{
auto localMaxSpaces = cmd.name.count(' ');
if (localMaxSpaces > maxSpaces)
{
maxSpaces = localMaxSpaces;
}
}
this->maxSpaces_ = maxSpaces;
};
this->items_.itemInserted.connect(addFirstMatchToMap);
this->items_.itemRemoved.connect(addFirstMatchToMap);
@ -96,9 +109,7 @@ QString CommandController::execCommand(const QString &textNoEmoji,
{
QString text = getApp()->emotes->emojis.replaceShortCodes(textNoEmoji);
QStringList words = text.split(' ', QString::SkipEmptyParts);
Command command;
{
std::lock_guard<std::mutex> lock(this->mutex_);
if (words.length() == 0)
@ -166,8 +177,7 @@ QString CommandController::execCommand(const QString &textNoEmoji,
}
} // bttv/ffz emote
{ // emoji/text
for (auto &variant :
app->emotes->emojis.parse(words[i]))
for (auto &variant : app->emotes->emojis.parse(words[i]))
{
constexpr const static struct {
void operator()(EmotePtr emote,
@ -226,8 +236,7 @@ QString CommandController::execCommand(const QString &textNoEmoji,
{
const auto &streamStatus = twitchChannel->accessStreamStatus();
QString messageText = streamStatus->live
? streamStatus->uptime
QString messageText = streamStatus->live ? streamStatus->uptime
: "Channel is not live.";
channel->addMessage(makeSystemMessage(messageText));
@ -238,8 +247,7 @@ QString CommandController::execCommand(const QString &textNoEmoji,
{
if (words.size() < 2)
{
channel->addMessage(
makeSystemMessage("Usage: /ignore [user]"));
channel->addMessage(makeSystemMessage("Usage: /ignore [user]"));
return "";
}
auto app = getApp();
@ -254,8 +262,8 @@ QString CommandController::execCommand(const QString &textNoEmoji,
return "";
}
user->ignore(
target, [channel](auto resultCode, const QString &message) {
user->ignore(target,
[channel](auto resultCode, const QString &message) {
channel->addMessage(makeSystemMessage(message));
});
@ -281,8 +289,8 @@ QString CommandController::execCommand(const QString &textNoEmoji,
return "";
}
user->unignore(
target, [channel](auto resultCode, const QString &message) {
user->unignore(target,
[channel](auto resultCode, const QString &message) {
channel->addMessage(makeSystemMessage(message));
});
@ -292,8 +300,7 @@ QString CommandController::execCommand(const QString &textNoEmoji,
{
if (words.size() < 2)
{
channel->addMessage(
makeSystemMessage("Usage: /follow [user]"));
channel->addMessage(makeSystemMessage("Usage: /follow [user]"));
return "";
}
auto app = getApp();
@ -391,10 +398,7 @@ QString CommandController::execCommand(const QString &textNoEmoji,
}
auto logsChannel =
app->twitch.server->getChannelOrEmpty(channelName);
if (logsChannel == nullptr)
{
}
else
if (logsChannel != nullptr)
{
logs->setInfo(logsChannel, target);
}
@ -411,15 +415,24 @@ QString CommandController::execCommand(const QString &textNoEmoji,
// check if custom command exists
auto it = this->commandsMap_.find(commandName);
if (it == this->commandsMap_.end())
if (it != this->commandsMap_.end())
{
return this->execCustomCommand(words, it.value());
}
auto maxSpaces = std::min(this->maxSpaces_, words.length() - 1);
for (int i = 0; i < maxSpaces; ++i)
{
commandName += ' ' + words[i + 1];
auto it = this->commandsMap_.find(commandName);
if (it != this->commandsMap_.end())
{
return this->execCustomCommand(words, it.value());
}
}
return text;
}
command = it.value();
}
return this->execCustomCommand(words, command);
}
QString CommandController::execCustomCommand(const QStringList &words,

View file

@ -37,6 +37,7 @@ private:
void load(Paths &paths);
QMap<QString, Command> commandsMap_;
int maxSpaces_ = 0;
std::mutex mutex_;