diff --git a/README.md b/README.md index c41bdf2..8cf9de9 100644 --- a/README.md +++ b/README.md @@ -5,16 +5,15 @@ PyMenu is a discord bot to create grocery list - Add items to your list by sending a message to a channel within the `GROCERY_LIST` category. - Support for multiple lists, one for each channel. - Easily remove items from your list by clicking on the check mark reaction. +- Set prices for items. +- Specify quantities for items. ## Screenshots ![](https://media.discordapp.net/attachments/501841539161522176/1161285342914170970/image.png?ex=6537be41&is=65254941&hm=3ace3096fd0a5d66cb8bfc8c744dbd79dcba84fc02cbaf7a02b01ffbcc7ed58d&=) ## Planned feature -- Set prices for items. -- Specify quantities for items. - Customize the category name. - Add images. -- Complete the Dockerfile for easy deployment - Implement statistics for nerd ## Requirements diff --git a/src/main.cpp b/src/main.cpp index 97a6e4d..6e1bf77 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,6 +3,7 @@ #include #include +#include "utils.hpp" #include "config.h" int main(int ac, char **av) @@ -29,10 +30,23 @@ int main(int ac, char **av) dpp::embed embed = dpp::embed() .set_color(dpp::colors::sea_green) .set_title(event.msg.content) - //.add_field("Quantite", "1", true) - //.add_field("Prix", "3.29") .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); diff --git a/src/utils.cpp b/src/utils.cpp new file mode 100644 index 0000000..1e779d2 --- /dev/null +++ b/src/utils.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include + +static float get_price(std::string& str) +{ + + float price = 0; + const char* str_c = str.c_str(); + char* next; + + for (size_t i = 0; i != str.size(); i++) + { + price = std::strtof(str_c + i, &next); + if (next == NULL) + break; + if (std::strncmp(next, "€", 3) == 0) + { + str.erase(i, next - (str_c + i) + 3); + return price; + } + } + return 0; +} +// pain 2€ 1 +static int get_quantity(std::string& str) +{ + unsigned int quantity = 0; + size_t len = 0, i; + + for (i = 0; i != str.size(); i++) + { + std::cout << str[i] << std::endl; + if (!std::isdigit(str[i])) + { + if (quantity != 0) + break; + continue; + } + quantity = quantity * 10 + str[i] - '0'; + len++; + } + std::cout << str << std::endl; + str.erase(i - len, len); + return quantity; +} + +int get_article_data(std::string& str, unsigned int& quantity, float& price) +{ + price = get_price(str); + std::cout << str << std::endl; + quantity = get_quantity(str); + + return 0; +} \ No newline at end of file diff --git a/src/utils.hpp b/src/utils.hpp new file mode 100644 index 0000000..bf26fb7 --- /dev/null +++ b/src/utils.hpp @@ -0,0 +1,5 @@ +#pragma once + +#include + +int get_article_data(std::string& str, unsigned int& quantity, float& price); \ No newline at end of file