This commit is contained in:
fourtf 2018-01-05 02:05:59 +01:00
parent 43568556be
commit 947589358b
3 changed files with 57 additions and 5 deletions

View file

@ -4,6 +4,9 @@
#include <QFile>
#include <QRegularExpression>
#include "channel.hpp"
#include "twitch/twitchchannel.hpp"
namespace chatterino {
namespace singletons {
CommandManager &CommandManager::getInstance()
@ -58,10 +61,11 @@ QStringList CommandManager::getCommands()
return this->commandsStringList;
}
QString CommandManager::execCommand(QString text, bool dryRun)
QString CommandManager::execCommand(const QString &text, std::shared_ptr<Channel> channel,
bool dryRun)
{
Command command;
QStringList words = text.split(' ', QString::SkipEmptyParts);
Command command;
{
std::lock_guard<std::mutex> lock(this->mutex);
@ -72,6 +76,44 @@ QString CommandManager::execCommand(QString text, bool dryRun)
QString commandName = words[0];
// check if default command exists
auto *twitchChannel = dynamic_cast<twitch::TwitchChannel *>(channel.get());
if (!dryRun && twitchChannel != nullptr) {
if (commandName == "/uptime") {
QString messageText =
twitchChannel->isLive ? twitchChannel->streamUptime : "Channel is not live.";
messages::SharedMessage message(
messages::Message::createSystemMessage(messageText));
channel->addMessage(message);
return "";
} else if (commandName == "/ignore" && words.size() >= 2) {
QString messageText;
if (IrcManager::getInstance().tryAddIgnoredUser(words.at(1), messageText)) {
messageText = "Ignored user \"" + words.at(1) + "\".";
}
messages::SharedMessage message(
messages::Message::createSystemMessage(messageText));
channel->addMessage(message);
return "";
} else if (commandName == "/unignore") {
QString messageText;
if (IrcManager::getInstance().tryRemoveIgnoredUser(words.at(1), messageText)) {
messageText = "Ignored user \"" + words.at(1) + "\".";
}
messages::SharedMessage message(
messages::Message::createSystemMessage(messageText));
channel->addMessage(message);
return "";
}
}
// check if custom command exists
auto it = this->commands.find(commandName);
if (it == this->commands.end()) {
@ -81,6 +123,11 @@ QString CommandManager::execCommand(QString text, bool dryRun)
command = it.value();
}
return this->execCustomCommand(words, command);
}
QString CommandManager::execCustomCommand(const QStringList &words, const Command &command)
{
QString result;
static QRegularExpression parseCommand("(^|[^{])({{)*{(\\d+\\+?)}");

View file

@ -5,6 +5,8 @@
#include <mutex>
namespace chatterino {
class Channel;
namespace singletons {
//
@ -18,7 +20,7 @@ class CommandManager
public:
static CommandManager &getInstance();
QString execCommand(QString text, bool dryRun);
QString execCommand(const QString &text, std::shared_ptr<Channel> channel, bool dryRun);
void loadCommands();
void saveCommands();
@ -38,6 +40,8 @@ private:
QMap<QString, Command> commands;
std::mutex mutex;
QStringList commandsStringList;
QString execCustomCommand(const QStringList &words, const Command &command);
};
}
}

View file

@ -82,7 +82,7 @@ SplitInput::SplitInput(Split *_chatWidget)
QString message = textInput.toPlainText();
QString sendMessage =
singletons::CommandManager::getInstance().execCommand(message, false);
singletons::CommandManager::getInstance().execCommand(message, c, false);
sendMessage = sendMessage.replace('\n', ' ');
c->sendMessage(sendMessage);
@ -232,7 +232,8 @@ void SplitInput::editTextChanged()
static QRegularExpression spaceRegex("\\s\\s+");
text = text.replace(spaceRegex, " ");
text = singletons::CommandManager::getInstance().execCommand(text, true);
text = singletons::CommandManager::getInstance().execCommand(
text, this->chatWidget->getChannel(), true);
QString labelText;