Compare commits
10 Commits
62c7107233
...
main
Author | SHA1 | Date | |
---|---|---|---|
a355447dc6 | |||
a7e87335b1 | |||
5cf17d64f9 | |||
575979c497 | |||
9bf46c857c | |||
2f1089976c | |||
75fbd17761 | |||
fceef8197b | |||
74024e696a | |||
cb5ad765a1 |
@ -10,7 +10,6 @@ RUN yes y | apt install g++
|
|||||||
|
|
||||||
# install the dpp lib
|
# install the dpp lib
|
||||||
RUN yes y | apt install cmake
|
RUN yes y | apt install cmake
|
||||||
RUN yes y | apt install wget
|
|
||||||
RUN yes y | apt install libsodium23
|
RUN yes y | apt install libsodium23
|
||||||
RUN yes y | apt install libopus0
|
RUN yes y | apt install libopus0
|
||||||
RUN wget -O dpp.deb https://dl.dpp.dev/
|
RUN wget -O dpp.deb https://dl.dpp.dev/
|
||||||
@ -18,12 +17,10 @@ RUN dpkg -i dpp.deb
|
|||||||
|
|
||||||
RUN mkdir /app
|
RUN mkdir /app
|
||||||
|
|
||||||
COPY . /app
|
RUN git clone https://git.chauvet.pro/starnakin/PyMenuBOT /app
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
RUN make
|
RUN make
|
||||||
|
|
||||||
COPY src/config.h src/config.h
|
ENTRYPOINT ["./PyMenu"]
|
||||||
|
|
||||||
ENTRYPOINT ["./PyMenu", "$BOT_TOKEN"]
|
|
70
src/grocery.cpp
Normal file
70
src/grocery.cpp
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
#include <dpp/cluster.h>
|
||||||
|
#include <dpp/unicode_emoji.h>
|
||||||
|
#include <dpp/dpp.h>
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#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;
|
||||||
|
|
||||||
|
std::string quantity, price;
|
||||||
|
|
||||||
|
get_article_data(str, quantity, price);
|
||||||
|
|
||||||
|
if (!quantity.empty())
|
||||||
|
embed.add_field("Quantity", quantity);
|
||||||
|
|
||||||
|
if (!price.empty())
|
||||||
|
embed.add_field("Price", 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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
}
|
67
src/main.cpp
67
src/main.cpp
@ -1,74 +1,19 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <dpp/dpp.h>
|
#include <dpp/dpp.h>
|
||||||
#include <dpp/channel.h>
|
#include <dpp/channel.h>
|
||||||
#include <dpp/unicode_emoji.h>
|
|
||||||
|
|
||||||
#include "utils.hpp"
|
#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_log(dpp::utility::cout_logger());
|
||||||
|
|
||||||
bot.on_message_create([&bot](const dpp::message_create_t& event)
|
on_react(bot);
|
||||||
{
|
on_message(bot);
|
||||||
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);
|
|
||||||
});
|
|
||||||
|
|
||||||
bot.on_ready([&bot](const dpp::ready_t& event)
|
bot.on_ready([&bot](const dpp::ready_t& event)
|
||||||
{
|
{
|
||||||
|
6
src/pymenu.hpp
Normal file
6
src/pymenu.hpp
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <dpp/cluster.h>
|
||||||
|
|
||||||
|
void on_react(dpp::cluster& bot);
|
||||||
|
void on_message(dpp::cluster& bot);
|
@ -4,52 +4,67 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
static float get_price(std::string& str)
|
#include "config.h"
|
||||||
{
|
|
||||||
|
|
||||||
float price = 0;
|
static std::string get_price(std::string& str)
|
||||||
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);
|
std::string price;
|
||||||
if (next == NULL)
|
size_t i;
|
||||||
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++)
|
for (i = 0; i != str.size(); i++)
|
||||||
{
|
{
|
||||||
std::cout << str[i] << std::endl;
|
if (std::strcmp(str.substr(i, 3).c_str(), "€") == 0)
|
||||||
if (!std::isdigit(str[i]))
|
|
||||||
{
|
{
|
||||||
if (quantity != 0)
|
if (price.empty())
|
||||||
break;
|
{
|
||||||
|
price = "";
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
quantity = quantity * 10 + str[i] - '0';
|
price += "€";
|
||||||
len++;
|
str.erase(i - (price.size() - 3), price.size());
|
||||||
|
return price;
|
||||||
}
|
}
|
||||||
str.erase(i - len, len);
|
else if (std::isdigit(str[i]) || str[i] == '.')
|
||||||
|
price += str[i];
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::string get_quantity(std::string& str)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
std::string quantity;
|
||||||
|
|
||||||
|
for (i = 0; i != str.size(); i++)
|
||||||
|
{
|
||||||
|
if (!std::isdigit(str[i]))
|
||||||
|
{
|
||||||
|
if (quantity.empty())
|
||||||
|
continue;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
quantity += str[i];
|
||||||
|
}
|
||||||
|
str.erase(i - quantity.size(), quantity.size());
|
||||||
return quantity;
|
return quantity;
|
||||||
}
|
}
|
||||||
|
|
||||||
int get_article_data(std::string& str, unsigned int& quantity, float& price)
|
int get_article_data(std::string& str, std::string& quantity, std::string& price)
|
||||||
{
|
{
|
||||||
price = get_price(str);
|
price = get_price(str);
|
||||||
quantity = get_quantity(str);
|
quantity = get_quantity(str);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string get_token(char** av, char** env)
|
||||||
|
{
|
||||||
|
if (av[1] != NULL)
|
||||||
|
return av[1];
|
||||||
|
for (size_t i = 0; env[i] != NULL; ++i)
|
||||||
|
{
|
||||||
|
if (std::strncmp("BOT_TOKEN", env[i], 9) == 0)
|
||||||
|
return env[i] + 10;
|
||||||
|
}
|
||||||
|
return BOT_TOKEN;
|
||||||
|
}
|
@ -2,4 +2,5 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
int get_article_data(std::string& str, unsigned int& quantity, float& price);
|
int get_article_data(std::string& str, std::string& quantity, std::string& price);
|
||||||
|
std::string get_token(char** av, char** env);
|
Reference in New Issue
Block a user