Skip to content

Commit 0a68d5a

Browse files
committed
Automod rule listener overhaul
1 parent 2e793d6 commit 0a68d5a

File tree

6 files changed

+323
-45
lines changed

6 files changed

+323
-45
lines changed

src/command_modules/moderation.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "../util.h"
1717
#include <map>
1818

19-
// TODO: Fix thread creation
2019
dpp::task<> moderation::create_ticket(const dpp::slashcommand_t &event, const nlohmann::json &config) {
2120
// Send "thinking" response to allow time for Discord API
2221
dpp::async thinking = event.co_thinking(true);

src/listeners/automod_rules.cpp

Lines changed: 296 additions & 35 deletions
Large diffs are not rendered by default.

src/listeners/automod_rules.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
#include <dpp/dpp.h>
1717

1818
namespace automod_rules {
19+
void add_rule_description_fields(dpp::embed& embed, const dpp::automod_rule& rule);
1920
dpp::task<> on_automod_rule_add(const dpp::automod_rule_create_t &event, const nlohmann::json& config);
20-
dpp::task<> on_automod_rule_remove(const dpp::automod_rule_delete_t &event, const nlohmann::json& config);
21-
dpp::task<> on_automod_rule_edit(const dpp::automod_rule_update_t &event, const nlohmann::json& config);
21+
void on_automod_rule_remove(const dpp::automod_rule_delete_t &event, const nlohmann::json& config);
22+
void on_automod_rule_edit(const dpp::automod_rule_update_t &event, const nlohmann::json& config);
2223
}

src/listeners/messages.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ void messages::add_message_content_fields(dpp::embed& embed, const dpp::message&
4242
}
4343
}
4444

45+
// TODO: Detect when the same message is posted in multiple support channels
4546
void messages::on_message(const dpp::message_create_t& event, const nlohmann::json& config) {
4647
util::MESSAGE_CACHE.push(event.msg);
4748
if (event.msg.author == event.owner->me) {

src/main.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,15 @@ int main(int argc, char* argv[]) {
267267
});
268268

269269
// Listeners
270+
bot.on_automod_rule_create([&config](const dpp::automod_rule_create_t &event) -> dpp::task<> {
271+
co_await automod_rules::on_automod_rule_add(event, config);
272+
});
273+
bot.on_automod_rule_delete([&config](const dpp::automod_rule_delete_t &event) {
274+
automod_rules::on_automod_rule_remove(event, config);
275+
});
276+
bot.on_automod_rule_update([&config](const dpp::automod_rule_update_t &event) {
277+
automod_rules::on_automod_rule_edit(event, config);
278+
});
270279
bot.on_message_create([&config](const dpp::message_create_t &event) {
271280
messages::on_message(event, config);
272281
});
@@ -282,15 +291,9 @@ int main(int argc, char* argv[]) {
282291
bot.on_message_reaction_remove([&config](const dpp::message_reaction_remove_t &event) -> dpp::task<> {
283292
co_await messages::on_reaction_removed(event, config);
284293
});
285-
bot.on_automod_rule_create([&config](const dpp::automod_rule_create_t &event) -> dpp::task<> {
286-
co_await automod_rules::on_automod_rule_add(event, config);
287-
});
288-
bot.on_automod_rule_delete([&config](const dpp::automod_rule_delete_t &event) -> dpp::task<> {
289-
co_await automod_rules::on_automod_rule_remove(event, config);
290-
});
291294

292295
// TODO: Autodetect bump timer on bot restart?
293-
bot.on_ready([&config, &commands, &db, &db_text_commands, &db_embed_commands](const dpp::ready_t &event) {
296+
bot.on_ready([&config, &commands, &db, &db_text_commands, &db_embed_commands](const dpp::ready_t &event) -> dpp::task<> {
294297
if (dpp::run_once<struct register_bot_commands>()) {
295298
std::vector<dpp::slashcommand> global_commands;
296299
std::vector<dpp::slashcommand> tsc_commands;
@@ -461,6 +464,14 @@ int main(int argc, char* argv[]) {
461464
util::log("INFO", log_message);
462465
util::handle_mute(event.owner, db, config, mute);
463466
}
467+
468+
// Add all automod rules to cache
469+
dpp::confirmation_callback_t rule_conf = co_await event.owner->co_automod_rules_get(config["guild_id"]);
470+
if (!rule_conf.is_error()) {
471+
for (const dpp::automod_rule& rule : std::get<dpp::automod_rule_map>(rule_conf.value) | std::views::values) {
472+
util::AUTOMOD_RULE_CACHE.push(rule);
473+
}
474+
}
464475
});
465476

466477
bot.start(dpp::st_wait);

src/util.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ namespace util {
166166
*/
167167
inline cache<dpp::message, 1000> MESSAGE_CACHE;
168168

169+
/**
170+
* Global cache of all automod rules (max 9 per guild)
171+
*/
172+
inline cache<dpp::automod_rule, 9> AUTOMOD_RULE_CACHE;
173+
169174
/**
170175
* Possible result types for a slash command search
171176
*/

0 commit comments

Comments
 (0)