mirror of
https://github.com/Syping/dtranslatebot.git
synced 2024-12-22 11:55:30 +01:00
add basic avatar support to webhook
This commit is contained in:
parent
5e78c2728a
commit
502cabd7a7
11 changed files with 128 additions and 14 deletions
|
@ -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)
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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())
|
||||
|
|
|
@ -48,4 +48,4 @@ namespace bot {
|
|||
};
|
||||
}
|
||||
|
||||
#endif //MESSAGE_QUEUE_H
|
||||
#endif // MESSAGE_QUEUE_H
|
||||
|
|
|
@ -21,6 +21,11 @@
|
|||
#include <iostream>
|
||||
#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();
|
||||
|
||||
|
|
|
@ -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<bot::settings::guild> m_guilds;
|
||||
bot::settings::translate m_translate;
|
||||
|
@ -68,4 +70,4 @@ namespace bot {
|
|||
}
|
||||
}
|
||||
|
||||
#endif //SETTINGS_H
|
||||
#endif // SETTINGS_H
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
|
||||
#include <thread>
|
||||
#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;
|
||||
|
|
|
@ -26,6 +26,7 @@
|
|||
namespace bot {
|
||||
struct translated_message {
|
||||
std::string author;
|
||||
std::string avatar;
|
||||
std::string message;
|
||||
std::string webhook;
|
||||
};
|
||||
|
|
58
src/webhook_push.cpp
Normal file
58
src/webhook_push.cpp
Normal 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
40
src/webhook_push.h
Normal 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
|
Loading…
Reference in a new issue