diff --git a/CMakeLists.txt b/CMakeLists.txt index f34f92b..1c398bd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,6 +29,8 @@ add_executable(${PROJECT_NAME} src/settings.h src/submit_queue.cpp src/submit_queue.h + src/webhook_push.cpp + src/webhook_push.h ) set(THREADS_PREFER_PTHREAD_FLAG ON) diff --git a/etc/dtranslatebot.json b/etc/dtranslatebot.json index 8c43ddc..78e8590 100644 --- a/etc/dtranslatebot.json +++ b/etc/dtranslatebot.json @@ -1,5 +1,5 @@ { - "token": "$bot_token", + "avatar_size": 256, "guilds": { "$guild1_id": { "$channel1_id": { @@ -26,6 +26,7 @@ } } }, + "token": "$bot_token", "translate": { "hostname": "127.0.0.1", "port": 80, diff --git a/src/main.cpp b/src/main.cpp index 1a25e0a..c68dd15 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -57,7 +57,7 @@ int main(int argc, char* argv[]) { bot::message message; message.id = event.msg.id; message.author = event.msg.author.format_username(); - message.avatar = event.msg.author.avatar.to_string(); + message.avatar = event.msg.author.get_avatar_url(settings.get_avatar_size()); message.message = event.msg.content; message.source = channel->source; message.targets = channel->targets; diff --git a/src/message_queue.cpp b/src/message_queue.cpp index f10cb89..03ff513 100644 --- a/src/message_queue.cpp +++ b/src/message_queue.cpp @@ -25,6 +25,7 @@ inline bot::translated_message make_translated_message(const bot::message &messa { bot::translated_message tr_message; tr_message.author = message.author; + tr_message.avatar = message.avatar; tr_message.message = translated_message; tr_message.webhook = webhook; return tr_message; @@ -61,7 +62,7 @@ void bot::message_queue::run(bot::settings::settings *settings, bot::submit_queu {"q", message.message}, {"source", message.source}, {"target", target->target}, - {"format", "text"}, + {"format", "text"} }; if (!tr_apiKey.empty()) diff --git a/src/message_queue.h b/src/message_queue.h index a82186f..9b788a7 100644 --- a/src/message_queue.h +++ b/src/message_queue.h @@ -48,4 +48,4 @@ namespace bot { }; } -#endif //MESSAGE_QUEUE_H +#endif // MESSAGE_QUEUE_H diff --git a/src/settings.cpp b/src/settings.cpp index a361aee..2f33576 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -21,6 +21,11 @@ #include #include "settings.h" +uint16_t bot::settings::settings::get_avatar_size() +{ + return m_avatarSize; +} + bot::settings::channel* bot::settings::settings::get_channel(bot::settings::guild *guild, uint64_t channel_id) { for (auto channel = guild->channel.begin(); channel != guild->channel.end(); channel++) { @@ -147,6 +152,17 @@ bool bot::settings::settings::parse(const std::string &filename) else m_translate.apiKey.clear(); + auto json_avatarSize = json.find("avatar_size"); + if (json_avatarSize != json.end()) { + m_avatarSize = json_avatarSize.value(); + if (m_avatarSize < 16) + m_avatarSize = 16; + else if (m_avatarSize > 4096) + m_avatarSize = 4096; + } + else + m_avatarSize = 256; + m_guilds.clear(); m_webhookIds.clear(); diff --git a/src/settings.h b/src/settings.h index f21d4ca..fd7209f 100644 --- a/src/settings.h +++ b/src/settings.h @@ -48,6 +48,7 @@ namespace bot { class settings { public: + uint16_t get_avatar_size(); bot::settings::channel* get_channel(bot::settings::guild *guild, uint64_t channel_id); bot::settings::channel* get_channel(uint64_t guild_id, uint64_t channel_id); bot::settings::guild* get_guild(uint64_t guild_id); @@ -59,6 +60,7 @@ namespace bot { void unlock(); private: + uint16_t m_avatarSize; std::recursive_mutex m_mutex; std::vector m_guilds; bot::settings::translate m_translate; @@ -68,4 +70,4 @@ namespace bot { } } -#endif //SETTINGS_H +#endif // SETTINGS_H diff --git a/src/submit_queue.cpp b/src/submit_queue.cpp index ef2e8cf..08f1519 100644 --- a/src/submit_queue.cpp +++ b/src/submit_queue.cpp @@ -18,6 +18,7 @@ #include #include "submit_queue.h" +#include "webhook_push.h" using namespace std::chrono_literals; void bot::submit_queue::add(const bot::translated_message &message) @@ -38,14 +39,7 @@ void bot::submit_queue::run(dpp::cluster *bot) m_mutex.unlock(); dpp::webhook webhook(message.webhook); - webhook.name = message.author; - - try { - bot->execute_webhook_sync(webhook, dpp::message(message.message)); - } - catch (const dpp::rest_exception &exception) { - std::cerr << "REST Error: " << exception.what() << std::endl; - } + bot::webhook_push webhook_push(webhook, message); std::this_thread::yield(); } @@ -56,7 +50,6 @@ void bot::submit_queue::run(dpp::cluster *bot) } } - void bot::submit_queue::terminate() { m_running = false; diff --git a/src/submit_queue.h b/src/submit_queue.h index a1f2367..22ca530 100644 --- a/src/submit_queue.h +++ b/src/submit_queue.h @@ -26,6 +26,7 @@ namespace bot { struct translated_message { std::string author; + std::string avatar; std::string message; std::string webhook; }; diff --git a/src/webhook_push.cpp b/src/webhook_push.cpp new file mode 100644 index 0000000..9826416 --- /dev/null +++ b/src/webhook_push.cpp @@ -0,0 +1,58 @@ +/***************************************************************************** +* dtranslatebot Discord Translate Bot +* Copyright (C) 2024 Syping +* +* Redistribution and use in source and binary forms, with or without modification, +* are permitted provided that the following conditions are met: +* +* 1. Redistributions of source code must retain the above copyright notice, +* this list of conditions and the following disclaimer. +* +* 2. Redistributions in binary form must reproduce the above copyright notice, +* this list of conditions and the following disclaimer in the documentation +* and/or other materials provided with the distribution. +* +* This software is provided as-is, no warranties are given to you, we are not +* responsible for anything with use of the software, you are self responsible. +*****************************************************************************/ + +#include +#include "webhook_push.h" + +bot::webhook_push::webhook_push(const dpp::webhook &webhook, const bot::translated_message &message) : + m_webhook(webhook), m_message(message) +{ + const dpp::json json_body = { + {"content", message.message}, + {"username", message.author}, + {"avatar_url", message.avatar} + }; + + dpp::http_headers http_headers; + http_headers.emplace("Content-Type", "application/json"); + + std::ostringstream webhook_url; + webhook_url << "/api/webhooks/" << m_webhook.id << "/" << m_webhook.token; + + try { + dpp::https_client https_request("discord.com", 443, webhook_url.str(), "POST", json_body.dump(), http_headers); + m_content = https_request.get_content(); + m_status = https_request.get_status(); + } + catch (const std::exception &exception) { + std::cerr << "Exception thrown while submitting: " << exception.what() << std::endl; + } + catch (...) { + std::cerr << "Exception thrown while submitting: unknown" << std::endl; + } +} + +const std::string bot::webhook_push::get_content() const +{ + return m_content; +} + +uint16_t bot::webhook_push::get_status() const +{ + return m_status; +} diff --git a/src/webhook_push.h b/src/webhook_push.h new file mode 100644 index 0000000..196b590 --- /dev/null +++ b/src/webhook_push.h @@ -0,0 +1,40 @@ +/***************************************************************************** +* dtranslatebot Discord Translate Bot +* Copyright (C) 2024 Syping +* +* Redistribution and use in source and binary forms, with or without modification, +* are permitted provided that the following conditions are met: +* +* 1. Redistributions of source code must retain the above copyright notice, +* this list of conditions and the following disclaimer. +* +* 2. Redistributions in binary form must reproduce the above copyright notice, +* this list of conditions and the following disclaimer in the documentation +* and/or other materials provided with the distribution. +* +* This software is provided as-is, no warranties are given to you, we are not +* responsible for anything with use of the software, you are self responsible. +*****************************************************************************/ + +#ifndef WEBHOOK_PUSH_H +#define WEBHOOK_PUSH_H + +#include +#include "submit_queue.h" + +namespace bot { + class webhook_push { + public: + explicit webhook_push(const dpp::webhook &webhook, const bot::translated_message &message); + const std::string get_content() const; + uint16_t get_status() const; + + private: + std::string m_content; + uint16_t m_status; + dpp::webhook m_webhook; + bot::translated_message m_message; + }; +} + +#endif // WEBHOOK_PUSH_H