summaryrefslogtreecommitdiff
path: root/src/commands/command.cpp
blob: a5ead406d69ba4a907f0ed44c628397004a82acb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "command.hpp"

#include <algorithm>
#include <memory>
#include <optional>

#include "../bundle.hpp"
#include "../modules/ping.hpp"
#include "request.hpp"

namespace bot {
  namespace command {
    CommandLoader::CommandLoader() {
      this->add_command(std::make_unique<mod::Ping>());
    }

    void CommandLoader::add_command(std::unique_ptr<Command> command) {
      this->commands.push_back(std::move(command));
    }

    std::optional<std::variant<std::vector<std::string>, std::string>>
    CommandLoader::run(const InstanceBundle &bundle,
                       const Request &request) const {
      auto command = std::find_if(
          this->commands.begin(), this->commands.end(),
          [&](const auto &x) { return x->get_name() == request.command_id; });

      if (command == this->commands.end()) {
        return std::nullopt;
      }

      if ((*command)->get_permission_level() >
          request.user_rights.get_level()) {
        return std::nullopt;
      }

      return (*command)->run(bundle, request);
    }
  }
}