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

View file

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