add basic avatar support to webhook

This commit is contained in:
Syping 2024-01-17 12:38:16 +01:00
parent 5e78c2728a
commit 502cabd7a7
11 changed files with 128 additions and 14 deletions

View file

@ -29,6 +29,8 @@ add_executable(${PROJECT_NAME}
src/settings.h src/settings.h
src/submit_queue.cpp src/submit_queue.cpp
src/submit_queue.h src/submit_queue.h
src/webhook_push.cpp
src/webhook_push.h
) )
set(THREADS_PREFER_PTHREAD_FLAG ON) set(THREADS_PREFER_PTHREAD_FLAG ON)

View file

@ -1,5 +1,5 @@
{ {
"token": "$bot_token", "avatar_size": 256,
"guilds": { "guilds": {
"$guild1_id": { "$guild1_id": {
"$channel1_id": { "$channel1_id": {
@ -26,6 +26,7 @@
} }
} }
}, },
"token": "$bot_token",
"translate": { "translate": {
"hostname": "127.0.0.1", "hostname": "127.0.0.1",
"port": 80, "port": 80,

View file

@ -57,7 +57,7 @@ int main(int argc, char* argv[]) {
bot::message message; bot::message message;
message.id = event.msg.id; message.id = event.msg.id;
message.author = event.msg.author.format_username(); 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.message = event.msg.content;
message.source = channel->source; message.source = channel->source;
message.targets = channel->targets; message.targets = channel->targets;

View file

@ -25,6 +25,7 @@ inline bot::translated_message make_translated_message(const bot::message &messa
{ {
bot::translated_message tr_message; bot::translated_message tr_message;
tr_message.author = message.author; tr_message.author = message.author;
tr_message.avatar = message.avatar;
tr_message.message = translated_message; tr_message.message = translated_message;
tr_message.webhook = webhook; tr_message.webhook = webhook;
return tr_message; return tr_message;
@ -61,7 +62,7 @@ void bot::message_queue::run(bot::settings::settings *settings, bot::submit_queu
{"q", message.message}, {"q", message.message},
{"source", message.source}, {"source", message.source},
{"target", target->target}, {"target", target->target},
{"format", "text"}, {"format", "text"}
}; };
if (!tr_apiKey.empty()) if (!tr_apiKey.empty())

View file

@ -21,6 +21,11 @@
#include <iostream> #include <iostream>
#include "settings.h" #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) 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++) { 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 else
m_translate.apiKey.clear(); 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_guilds.clear();
m_webhookIds.clear(); m_webhookIds.clear();

View file

@ -48,6 +48,7 @@ namespace bot {
class settings { class settings {
public: public:
uint16_t get_avatar_size();
bot::settings::channel* get_channel(bot::settings::guild *guild, uint64_t channel_id); 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::channel* get_channel(uint64_t guild_id, uint64_t channel_id);
bot::settings::guild* get_guild(uint64_t guild_id); bot::settings::guild* get_guild(uint64_t guild_id);
@ -59,6 +60,7 @@ namespace bot {
void unlock(); void unlock();
private: private:
uint16_t m_avatarSize;
std::recursive_mutex m_mutex; std::recursive_mutex m_mutex;
std::vector<bot::settings::guild> m_guilds; std::vector<bot::settings::guild> m_guilds;
bot::settings::translate m_translate; bot::settings::translate m_translate;

View file

@ -18,6 +18,7 @@
#include <thread> #include <thread>
#include "submit_queue.h" #include "submit_queue.h"
#include "webhook_push.h"
using namespace std::chrono_literals; using namespace std::chrono_literals;
void bot::submit_queue::add(const bot::translated_message &message) 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(); m_mutex.unlock();
dpp::webhook webhook(message.webhook); dpp::webhook webhook(message.webhook);
webhook.name = message.author; bot::webhook_push webhook_push(webhook, message);
try {
bot->execute_webhook_sync(webhook, dpp::message(message.message));
}
catch (const dpp::rest_exception &exception) {
std::cerr << "REST Error: " << exception.what() << std::endl;
}
std::this_thread::yield(); std::this_thread::yield();
} }
@ -56,7 +50,6 @@ void bot::submit_queue::run(dpp::cluster *bot)
} }
} }
void bot::submit_queue::terminate() void bot::submit_queue::terminate()
{ {
m_running = false; m_running = false;

View file

@ -26,6 +26,7 @@
namespace bot { namespace bot {
struct translated_message { struct translated_message {
std::string author; std::string author;
std::string avatar;
std::string message; std::string message;
std::string webhook; std::string webhook;
}; };

58
src/webhook_push.cpp Normal file
View file

@ -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 <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)
{
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;
}

40
src/webhook_push.h Normal file
View file

@ -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 <dpp/dpp.h>
#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