From 75fbd177616309bb2ccdcfdba6c52cc2dff33d4f Mon Sep 17 00:00:00 2001 From: starnakin Date: Wed, 11 Oct 2023 18:59:32 +0000 Subject: [PATCH] clean: split code --- src/main.cpp | 67 +++++------------------------------------------- src/message.cpp | 51 ++++++++++++++++++++++++++++++++++++ src/pymenu.hpp | 6 +++++ src/reaction.cpp | 23 +++++++++++++++++ 4 files changed, 86 insertions(+), 61 deletions(-) create mode 100644 src/message.cpp create mode 100644 src/pymenu.hpp create mode 100644 src/reaction.cpp diff --git a/src/main.cpp b/src/main.cpp index 6e1bf77..6fb4a5c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,74 +1,19 @@ #include #include #include -#include #include "utils.hpp" -#include "config.h" +#include "pymenu.hpp" -int main(int ac, char **av) +int main(int ac, char **av, char** env) { - dpp::cluster bot(ac == 2 ? av[1] : BOT_TOKEN, dpp::i_default_intents | dpp::i_message_content); + (void) ac; + dpp::cluster bot(get_token(av, env), dpp::i_default_intents | dpp::i_message_content); bot.on_log(dpp::utility::cout_logger()); - bot.on_message_create([&bot](const dpp::message_create_t& event) - { - if (event.msg.author.id == bot.me.id) - { - bot.message_add_reaction(event.msg, dpp::unicode_emoji::white_check_mark); - } - else - { - dpp::channel channel = bot.channel_get_sync(event.msg.channel_id); - dpp::channel category = bot.channel_get_sync(channel.parent_id); - - - if (category.is_category() == false || category.name != GROCERY_LIST_CATEGORY) - return; - - dpp::embed embed = dpp::embed() - .set_color(dpp::colors::sea_green) - .set_title(event.msg.content) - .set_author(event.msg.author.username, "", "") - .set_footer(dpp::embed_footer().set_text("PyMenuVersion:2.0")); - - std::string str = event.msg.content; - - unsigned int quantity; - float price; - - get_article_data(str, quantity, price); - - if (quantity != 0) - embed.add_field("Quantity", std::to_string(quantity)); - - if (price != 0) - embed.add_field("Price", std::to_string(price) + "€"); - - embed.set_title(str); - - dpp::message msg(event.msg.channel_id, embed); - event.send(msg); - - bot.message_delete(event.msg.id, event.msg.channel_id); - } - }); - - bot.on_message_reaction_add([&bot](const dpp::message_reaction_add_t& event) - { - dpp::channel channel = bot.channel_get_sync(event.channel_id); - dpp::channel category = bot.channel_get_sync(channel.parent_id); - - if (category.is_category() == false || category.name != GROCERY_LIST_CATEGORY) - return; - - if (event.reacting_user.id == bot.me.id) - return; - dpp::emoji emoji = event.reacting_emoji; - if (emoji.name == dpp::unicode_emoji::white_check_mark) - bot.message_delete(event.message_id, event.channel_id); - }); + on_react(bot); + on_message(bot); bot.on_ready([&bot](const dpp::ready_t& event) { diff --git a/src/message.cpp b/src/message.cpp new file mode 100644 index 0000000..c333cc6 --- /dev/null +++ b/src/message.cpp @@ -0,0 +1,51 @@ +#include +#include +#include + +#include "config.h" +#include "utils.hpp" + +void on_message(dpp::cluster& bot) +{ + bot.on_message_create([&bot](const dpp::message_create_t& event) + { + if (event.msg.author.id == bot.me.id) + { + bot.message_add_reaction(event.msg, dpp::unicode_emoji::white_check_mark); + } + else + { + dpp::channel channel = bot.channel_get_sync(event.msg.channel_id); + dpp::channel category = bot.channel_get_sync(channel.parent_id); + + if (category.is_category() == false || category.name != GROCERY_LIST_CATEGORY) + return; + + dpp::embed embed = dpp::embed() + .set_color(dpp::colors::sea_green) + .set_title(event.msg.content) + .set_author(event.msg.author.username, "", "") + .set_footer(dpp::embed_footer().set_text("PyMenuVersion:2.0")); + + std::string str = event.msg.content; + + unsigned int quantity; + float price; + + get_article_data(str, quantity, price); + + if (quantity != 0) + embed.add_field("Quantity", std::to_string(quantity)); + + if (price != 0) + embed.add_field("Price", std::to_string(price) + "€"); + + embed.set_title(str); + + dpp::message msg(event.msg.channel_id, embed); + event.send(msg); + + bot.message_delete(event.msg.id, event.msg.channel_id); + } + }); +} \ No newline at end of file diff --git a/src/pymenu.hpp b/src/pymenu.hpp new file mode 100644 index 0000000..d1b1f32 --- /dev/null +++ b/src/pymenu.hpp @@ -0,0 +1,6 @@ +#pragma once + +#include + +void on_react(dpp::cluster& bot); +void on_message(dpp::cluster& bot); \ No newline at end of file diff --git a/src/reaction.cpp b/src/reaction.cpp new file mode 100644 index 0000000..0ccf744 --- /dev/null +++ b/src/reaction.cpp @@ -0,0 +1,23 @@ +#include +#include +#include + +#include "config.h" + +void on_react(dpp::cluster& bot) +{ + bot.on_message_reaction_add([&bot](const dpp::message_reaction_add_t& event) + { + dpp::channel channel = bot.channel_get_sync(event.channel_id); + dpp::channel category = bot.channel_get_sync(channel.parent_id); + + if (category.is_category() == false || category.name != GROCERY_LIST_CATEGORY) + return; + + if (event.reacting_user.id == bot.me.id) + return; + dpp::emoji emoji = event.reacting_emoji; + if (emoji.name == dpp::unicode_emoji::white_check_mark) + bot.message_delete(event.message_id, event.channel_id); + }); +} \ No newline at end of file