mirror of
https://gitlab.com/niansa/SomeBot.git
synced 2025-03-06 20:48:26 +01:00
Added command_alias_mode setting
This commit is contained in:
parent
5ff93aceef
commit
f43bfe8e7c
2 changed files with 75 additions and 40 deletions
24
bot.hpp
24
bot.hpp
|
@ -32,11 +32,6 @@ class Bot {
|
|||
on_deinit;
|
||||
|
||||
public:
|
||||
enum class ModulesMode {
|
||||
whitelist,
|
||||
blacklist
|
||||
};
|
||||
|
||||
template<class interactionCallbackDataT>
|
||||
struct Interaction {
|
||||
std::vector<std::string> names;
|
||||
|
@ -71,8 +66,6 @@ public:
|
|||
private:
|
||||
bool inited = false;
|
||||
|
||||
std::vector<std::string> modules;
|
||||
ModulesMode modules_mode = ModulesMode::blacklist;
|
||||
std::unordered_map<std::string_view, void*> loaded_modules;
|
||||
|
||||
struct Commands {
|
||||
|
@ -94,6 +87,19 @@ public:
|
|||
management_guild_id = 123;
|
||||
bool _private = false;
|
||||
bool reregister_commands = false;
|
||||
|
||||
std::vector<std::string> modules;
|
||||
|
||||
enum class ModulesMode {
|
||||
whitelist,
|
||||
blacklist
|
||||
} modules_mode = ModulesMode::blacklist;
|
||||
|
||||
enum class CommandAliasMode {
|
||||
all,
|
||||
first,
|
||||
last
|
||||
} command_alias_mode = CommandAliasMode::all;
|
||||
} config;
|
||||
std::unordered_map<std::string_view, std::pair<void*, std::mutex*>> properties;
|
||||
|
||||
|
@ -103,9 +109,7 @@ public:
|
|||
void start();
|
||||
static inline void add_on_init(const std::function<void (Bot*)>& cb) {on_init.push_back(cb);}
|
||||
static inline void add_on_deinit(const std::function<void (Bot*)>& cb) {on_deinit.push_back(cb);}
|
||||
void add_module(const std::string& name) {modules.push_back(name);}
|
||||
void set_modules_mode(ModulesMode mode) {modules_mode = mode;}
|
||||
ModulesMode get_modules_mode(ModulesMode mode) const {return modules_mode;}
|
||||
void add_module(const std::string& name) {config.modules.push_back(name);}
|
||||
template<class T>
|
||||
void add_loaded_module(std::string_view name, T *instance) {loaded_modules[name] = reinterpret_cast<void*>(instance);}
|
||||
template<class T>
|
||||
|
|
91
main.cpp
91
main.cpp
|
@ -77,7 +77,7 @@ Bot::Bot(const std::string& token, const std::string& database) : cluster(token)
|
|||
event.reply(":sos: Unhandled exception at `"+Util::int_as_hex(&e)+"`: "+e.what());
|
||||
}
|
||||
});
|
||||
cluster.on_ready([this](const dpp::ready_t& event) {
|
||||
cluster.on_ready([this](const dpp::ready_t& event) { //TODO: Clear this macro mess without compromising performance... actually, does that even matter here?
|
||||
// Register bot commands once
|
||||
if (dpp::run_once<struct register_bot_commands>()) {
|
||||
std::thread([this]() {
|
||||
|
@ -97,70 +97,88 @@ Bot::Bot(const std::string& token, const std::string& database) : cluster(token)
|
|||
}
|
||||
}
|
||||
// Add global commands
|
||||
#define ADD_SC_RESPECTING_CMD_ALIAS_MODE \
|
||||
switch (config.command_alias_mode) { \
|
||||
case Config::CommandAliasMode::all: { \
|
||||
for (const auto& name : command.names) { \
|
||||
sc.set_name(name); \
|
||||
slashcommands.push_back(sc); \
|
||||
} \
|
||||
} break; \
|
||||
case Config::CommandAliasMode::first: { \
|
||||
sc.set_name(command.names.front()); \
|
||||
slashcommands.push_back(sc); \
|
||||
} break; \
|
||||
case Config::CommandAliasMode::last: { \
|
||||
sc.set_name(command.names.back()); \
|
||||
slashcommands.push_back(sc); \
|
||||
} break; \
|
||||
}
|
||||
{
|
||||
std::vector<dpp::slashcommand> slashcommands;
|
||||
for (const auto& command : commands.chat_commands) {
|
||||
auto sc = command.misc;
|
||||
sc.set_description(command.description);
|
||||
sc.set_type(dpp::slashcommand_contextmenu_type::ctxm_chat_input);
|
||||
for (const auto& name : command.names) {
|
||||
sc.set_name(name);
|
||||
slashcommands.push_back(sc);
|
||||
}
|
||||
ADD_SC_RESPECTING_CMD_ALIAS_MODE
|
||||
}
|
||||
for (const auto& command : commands.user_commands) {
|
||||
auto sc = command.misc;
|
||||
sc.set_description(command.description);
|
||||
sc.set_type(dpp::slashcommand_contextmenu_type::ctxm_user);
|
||||
for (const auto& name : command.names) {
|
||||
sc.set_name(name);
|
||||
slashcommands.push_back(sc);
|
||||
}
|
||||
ADD_SC_RESPECTING_CMD_ALIAS_MODE
|
||||
}
|
||||
for (const auto& command : commands.message_commands) {
|
||||
auto sc = command.misc;
|
||||
sc.set_description(command.description);
|
||||
sc.set_type(dpp::slashcommand_contextmenu_type::ctxm_message);
|
||||
for (const auto& name : command.names) {
|
||||
sc.set_name(name);
|
||||
slashcommands.push_back(sc);
|
||||
}
|
||||
ADD_SC_RESPECTING_CMD_ALIAS_MODE
|
||||
}
|
||||
cluster.global_bulk_command_create(slashcommands);
|
||||
}
|
||||
// Add guild commands
|
||||
#undef ADD_SC_RESPECTING_CMD_ALIAS_MODE
|
||||
#define ADD_SC_RESPECTING_CMD_ALIAS_MODE \
|
||||
switch (config.command_alias_mode) { \
|
||||
case Config::CommandAliasMode::all: { \
|
||||
for (const auto& name : command.names) { \
|
||||
sc.set_name(name); \
|
||||
cluster.guild_command_create(sc, guild_id); \
|
||||
} \
|
||||
} break; \
|
||||
case Config::CommandAliasMode::first: { \
|
||||
sc.set_name(command.names.front()); \
|
||||
cluster.guild_command_create(sc, guild_id); \
|
||||
} break; \
|
||||
case Config::CommandAliasMode::last: { \
|
||||
sc.set_name(command.names.back()); \
|
||||
cluster.guild_command_create(sc, guild_id); \
|
||||
} break; \
|
||||
}
|
||||
{
|
||||
for (const auto& [command, guild_id] : commands.guild_chat_commands) {
|
||||
auto sc = command.misc;
|
||||
sc.set_description(command.description);
|
||||
sc.set_type(dpp::slashcommand_contextmenu_type::ctxm_chat_input);
|
||||
sc.set_application_id(cluster.me.id);
|
||||
for (const auto& name : command.names) {
|
||||
sc.set_name(name);
|
||||
cluster.guild_command_create(sc, guild_id);
|
||||
}
|
||||
ADD_SC_RESPECTING_CMD_ALIAS_MODE
|
||||
}
|
||||
for (const auto& [command, guild_id] : commands.guild_user_commands) {
|
||||
auto sc = command.misc;
|
||||
sc.set_description(command.description);
|
||||
sc.set_type(dpp::slashcommand_contextmenu_type::ctxm_user);
|
||||
sc.set_application_id(cluster.me.id);
|
||||
for (const auto& name : command.names) {
|
||||
sc.set_name(name);
|
||||
cluster.guild_command_create(sc, guild_id);
|
||||
}
|
||||
ADD_SC_RESPECTING_CMD_ALIAS_MODE
|
||||
}
|
||||
for (const auto& [command, guild_id] : commands.guild_message_commands) {
|
||||
auto sc = command.misc;
|
||||
sc.set_description(command.description);
|
||||
sc.set_type(dpp::slashcommand_contextmenu_type::ctxm_message);
|
||||
sc.set_application_id(cluster.me.id);
|
||||
for (const auto& name : command.names) {
|
||||
sc.set_name(name);
|
||||
cluster.guild_command_create(sc, guild_id);
|
||||
}
|
||||
ADD_SC_RESPECTING_CMD_ALIAS_MODE
|
||||
}
|
||||
}
|
||||
#undef ADD_SC_RESPECTING_CMD_ALIAS_MODE
|
||||
// We're done
|
||||
config.reregister_commands = false;
|
||||
}).detach();
|
||||
|
@ -192,8 +210,8 @@ void Bot::start() {
|
|||
}
|
||||
|
||||
bool Bot::is_module_enabled(std::string_view name) {
|
||||
auto fres = std::find(modules.begin(), modules.end(), name) != modules.end();
|
||||
if (modules_mode == ModulesMode::blacklist) {
|
||||
auto fres = std::find(config.modules.begin(), config.modules.end(), name) != config.modules.end();
|
||||
if (config.modules_mode == Config::ModulesMode::blacklist) {
|
||||
fres = !fres;
|
||||
}
|
||||
return fres;
|
||||
|
@ -281,19 +299,32 @@ int main(int argc, char **argv) {
|
|||
for (const auto& module_name : config_entry["modules"]) {
|
||||
bot.add_module(std::string(module_name));
|
||||
}
|
||||
bot.set_modules_mode(Bot::ModulesMode::whitelist);
|
||||
bot.config.modules_mode = Bot::Config::ModulesMode::whitelist;
|
||||
}
|
||||
if (config_entry.contains("modules_mode")) {
|
||||
const std::string& mode = config_entry;
|
||||
if (mode == "blacklist") {
|
||||
bot.set_modules_mode(Bot::ModulesMode::blacklist);
|
||||
bot.config.modules_mode = Bot::Config::ModulesMode::blacklist;
|
||||
} else if (mode == "whitelist") {
|
||||
bot.set_modules_mode(Bot::ModulesMode::whitelist);
|
||||
bot.config.modules_mode = Bot::Config::ModulesMode::whitelist;
|
||||
} else {
|
||||
std::cerr << "Invalid modules mode \"" << mode << "\"! Accepted values: \"whitelist\", \"blacklist\"" << std::endl;
|
||||
exit(-5);
|
||||
}
|
||||
}
|
||||
if (config_entry.contains("command_alias_mode")) {
|
||||
std::string mode = config_entry["command_alias_mode"];
|
||||
if (mode == "all") {
|
||||
bot.config.command_alias_mode = Bot::Config::CommandAliasMode::all;
|
||||
} else if (mode == "first") {
|
||||
bot.config.command_alias_mode = Bot::Config::CommandAliasMode::first;
|
||||
} else if (mode == "last") {
|
||||
bot.config.command_alias_mode = Bot::Config::CommandAliasMode::last;
|
||||
} else {
|
||||
std::cerr << "Invalid command alias mode \"" << mode << "\"! Accepted values: \"all\", \"first\", \"last\"" << std::endl;
|
||||
exit(-5);
|
||||
}
|
||||
}
|
||||
// Misc setup
|
||||
std::ofstream logfile(bot.config.id+"-log.txt", std::ios_base::app);
|
||||
bot.cluster.on_log([&](const dpp::log_t& event) {
|
||||
|
|
Loading…
Add table
Reference in a new issue