mirror of
https://github.com/Syping/dtranslatebot.git
synced 2024-12-22 11:55:30 +01:00
CMakeLists.txt and Webhook push improvements
This commit is contained in:
parent
0e205682a3
commit
ccd2736c63
8 changed files with 50 additions and 51 deletions
|
@ -17,36 +17,40 @@
|
|||
****************************************************************************]]
|
||||
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(dtranslatebot VERSION 0.1 DESCRIPTION "Discord Translation Bot")
|
||||
project(dtranslatebot VERSION 0.1 LANGUAGES CXX)
|
||||
include(GNUInstallDirs)
|
||||
|
||||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
|
||||
|
||||
add_executable(${PROJECT_NAME}
|
||||
src/main.cpp
|
||||
src/message_queue.cpp
|
||||
# dtranslatebot Source files
|
||||
set(DTRANSLATEBOT_HEADERS
|
||||
src/message_queue.h
|
||||
src/settings.cpp
|
||||
src/settings.h
|
||||
src/submit_queue.cpp
|
||||
src/submit_queue.h
|
||||
src/webhook_push.cpp
|
||||
src/webhook_push.h
|
||||
)
|
||||
set(DTRANSLATEBOT_SOURCES
|
||||
src/main.cpp
|
||||
src/message_queue.cpp
|
||||
src/settings.cpp
|
||||
src/submit_queue.cpp
|
||||
src/webhook_push.cpp
|
||||
)
|
||||
|
||||
set(THREADS_PREFER_PTHREAD_FLAG ON)
|
||||
# dtranslatebot Module Path
|
||||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
|
||||
|
||||
# D++ Discord API Library for Bots
|
||||
find_package(DPP REQUIRED)
|
||||
|
||||
# pthread Support
|
||||
set(THREADS_PREFER_PTHREAD_FLAG ON)
|
||||
find_package(Threads REQUIRED)
|
||||
|
||||
target_link_libraries(${PROJECT_NAME}
|
||||
${DPP_LIBRARIES}
|
||||
Threads::Threads
|
||||
)
|
||||
|
||||
target_include_directories(${PROJECT_NAME} PRIVATE
|
||||
${DPP_INCLUDE_DIR}
|
||||
)
|
||||
|
||||
set_target_properties(${PROJECT_NAME} PROPERTIES
|
||||
# dtranslatebot Target + Installs
|
||||
add_executable(dtranslatebot ${DTRANSLATEBOT_HEADERS} ${DTRANSLATEBOT_SOURCES})
|
||||
target_link_libraries(dtranslatebot Threads::Threads ${DPP_LIBRARIES})
|
||||
target_include_directories(dtranslatebot PRIVATE ${DPP_INCLUDE_DIR})
|
||||
set_target_properties(dtranslatebot PROPERTIES
|
||||
CXX_STANDARD 17
|
||||
CXX_STANDARD_REQUIRED ON
|
||||
)
|
||||
install(TARGETS dtranslatebot DESTINATION "${CMAKE_INSTALL_BINDIR}")
|
||||
|
|
|
@ -52,14 +52,14 @@ int main(int argc, char* argv[]) {
|
|||
}
|
||||
|
||||
const std::lock_guard<bot::settings::settings> guard(settings);
|
||||
bot::settings::channel *channel = settings.get_channel(event.msg.guild_id, event.msg.channel_id);
|
||||
const bot::settings::channel *channel = settings.get_channel(event.msg.guild_id, event.msg.channel_id);
|
||||
if (channel) {
|
||||
bot::message message;
|
||||
message.id = event.msg.id;
|
||||
|
||||
message.author = event.msg.member.get_nickname();
|
||||
if (message.author.empty())
|
||||
message.author = event.msg.author.format_username();
|
||||
message.author = event.msg.author.global_name;
|
||||
|
||||
message.avatar = event.msg.member.get_avatar_url(settings.get_avatar_size());
|
||||
if (message.avatar.empty())
|
||||
|
|
|
@ -49,7 +49,7 @@ void bot::message_queue::run(bot::settings::settings *settings, bot::submit_queu
|
|||
m_mutex.unlock();
|
||||
|
||||
settings->lock();
|
||||
bot::settings::translate *translate = settings->get_translate();
|
||||
const bot::settings::translate *translate = settings->get_translate();
|
||||
const std::string tr_apiKey = translate->apiKey;
|
||||
const std::string tr_hostname = translate->hostname;
|
||||
const uint16_t tr_port = translate->port;
|
||||
|
@ -68,8 +68,9 @@ void bot::message_queue::run(bot::settings::settings *settings, bot::submit_queu
|
|||
if (!tr_apiKey.empty())
|
||||
json_body.emplace("apiKey", tr_apiKey);
|
||||
|
||||
dpp::http_headers http_headers;
|
||||
http_headers.emplace("Content-Type", "application/json");
|
||||
const dpp::http_headers http_headers = {
|
||||
{"Content-Type", "application/json"}
|
||||
};
|
||||
|
||||
std::string tr_message = message.message;
|
||||
|
||||
|
|
|
@ -26,7 +26,7 @@ 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)
|
||||
const 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++) {
|
||||
if (channel->id == channel_id)
|
||||
|
@ -35,7 +35,7 @@ bot::settings::channel* bot::settings::settings::get_channel(bot::settings::guil
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
bot::settings::channel* bot::settings::settings::get_channel(uint64_t guild_id, uint64_t channel_id)
|
||||
const bot::settings::channel *bot::settings::settings::get_channel(uint64_t guild_id, uint64_t channel_id)
|
||||
{
|
||||
for (auto guild = m_guilds.begin(); guild != m_guilds.end(); guild++) {
|
||||
if (guild->id == guild_id) {
|
||||
|
@ -48,7 +48,7 @@ bot::settings::channel* bot::settings::settings::get_channel(uint64_t guild_id,
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
bot::settings::guild* bot::settings::settings::get_guild(uint64_t guild_id)
|
||||
const bot::settings::guild *bot::settings::settings::get_guild(uint64_t guild_id)
|
||||
{
|
||||
for (auto guild = m_guilds.begin(); guild != m_guilds.end(); guild++) {
|
||||
if (guild->id == guild_id)
|
||||
|
@ -57,7 +57,7 @@ bot::settings::guild* bot::settings::settings::get_guild(uint64_t guild_id)
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
bot::settings::translate* bot::settings::settings::get_translate()
|
||||
const bot::settings::translate *bot::settings::settings::get_translate()
|
||||
{
|
||||
return &m_translate;
|
||||
}
|
||||
|
|
|
@ -49,10 +49,10 @@ 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);
|
||||
bot::settings::translate* get_translate();
|
||||
const bot::settings::channel* get_channel(bot::settings::guild *guild, uint64_t channel_id);
|
||||
const bot::settings::channel* get_channel(uint64_t guild_id, uint64_t channel_id);
|
||||
const bot::settings::guild* get_guild(uint64_t guild_id);
|
||||
const bot::settings::translate* get_translate();
|
||||
const std::string get_token();
|
||||
bool is_translatebot(uint64_t webhook_id);
|
||||
void lock();
|
||||
|
|
|
@ -38,8 +38,7 @@ void bot::submit_queue::run(dpp::cluster *bot)
|
|||
m_queue.erase(m_queue.begin());
|
||||
m_mutex.unlock();
|
||||
|
||||
dpp::webhook webhook(message.webhook);
|
||||
bot::webhook_push webhook_push(webhook, message);
|
||||
bot::webhook_push webhook_push(message.webhook, message, bot);
|
||||
|
||||
std::this_thread::yield();
|
||||
}
|
||||
|
|
|
@ -16,11 +16,10 @@
|
|||
* responsible for anything with use of the software, you are self responsible.
|
||||
*****************************************************************************/
|
||||
|
||||
#include <sstream>
|
||||
#include "webhook_push.h"
|
||||
|
||||
bot::webhook_push::webhook_push(const dpp::webhook &webhook, const bot::translated_message &message) :
|
||||
m_webhook(webhook), m_message(message)
|
||||
bot::webhook_push::webhook_push(const std::string &webhook, const bot::translated_message &message, dpp::cluster *bot) :
|
||||
m_message(message)
|
||||
{
|
||||
const dpp::json json_body = {
|
||||
{"content", message.message},
|
||||
|
@ -28,22 +27,19 @@ bot::webhook_push::webhook_push(const dpp::webhook &webhook, const bot::translat
|
|||
{"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();
|
||||
dpp::http_request webhook_request(webhook, nullptr, dpp::m_post, json_body.dump(), "application/json");
|
||||
const dpp::http_request_completion_t result = webhook_request.run(bot);
|
||||
if (result.status != 204)
|
||||
std::cerr << "Webhook push returned unexpected code " << result.status << " with response: " << result.body << std::endl;
|
||||
m_content = result.body;
|
||||
m_status = result.status;
|
||||
}
|
||||
catch (const std::exception &exception) {
|
||||
std::cerr << "Exception thrown while submitting: " << exception.what() << std::endl;
|
||||
std::cerr << "Exception thrown while Webhook push: " << exception.what() << std::endl;
|
||||
}
|
||||
catch (...) {
|
||||
std::cerr << "Exception thrown while submitting: unknown" << std::endl;
|
||||
std::cerr << "Exception thrown while Webhook push: unknown" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -25,14 +25,13 @@
|
|||
namespace bot {
|
||||
class webhook_push {
|
||||
public:
|
||||
explicit webhook_push(const dpp::webhook &webhook, const bot::translated_message &message);
|
||||
explicit webhook_push(const std::string &webhook, const bot::translated_message &message, dpp::cluster *bot);
|
||||
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;
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue