[Discord] Add Discord webhook callback processing to world (#3322)

This commit is contained in:
Chris Miles 2023-04-29 19:49:06 -05:00 committed by GitHub
parent 95b306599f
commit ecf2a369cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 0 deletions

View File

@ -17,6 +17,7 @@ public:
void Disconnect() { if(connection && connection->Handle()) connection->Handle()->Disconnect(); }
void SendMessage(const char *From, const char *Message);
const std::shared_ptr<EQ::Net::ServertalkServerConnection> &GetConnection() const;
inline bool IsConnected() const { return connection->Handle() ? connection->Handle()->IsConnected() : false; }
private:
inline std::string GetIP() const { return (connection && connection->Handle()) ? connection->Handle()->RemoteIP() : 0; }

View File

@ -300,6 +300,8 @@ bool WorldBoot::DatabaseLoadRoutines(int argc, char **argv)
->SetLogPath(path.GetLogPath())
->LoadLogDatabaseSettings();
LogSys.SetDiscordHandler(&WorldBoot::DiscordWebhookMessageHandler);
if (!ignore_db) {
LogInfo("Checking Database Conversions");
database.CheckDatabaseConversions();
@ -662,3 +664,18 @@ void WorldBoot::Shutdown()
safe_delete(mutex);
}
void WorldBoot::SendDiscordMessage(int webhook_id, const std::string &message)
{
if (UCSLink.IsConnected()) {
auto pack = new ServerPacket(ServerOP_DiscordWebhookMessage, sizeof(DiscordWebhookMessage_Struct) + 1);
auto *q = (DiscordWebhookMessage_Struct *) pack->pBuffer;
strn0cpy(q->message, message.c_str(), 2000);
q->webhook_id = webhook_id;
UCSLink.SendPacket(pack);
safe_delete(pack);
}
}

View File

@ -3,6 +3,9 @@
#include <string>
#include "../common/types.h"
#include "../common/discord/discord.h"
extern UCSConnection UCSLink;
class WorldBoot {
public:
@ -16,6 +19,16 @@ public:
static bool DatabaseLoadRoutines(int argc, char **argv);
static void CheckForPossibleConfigurationIssues();
static void Shutdown();
static void SendDiscordMessage(int webhook_id, const std::string& message);
static void DiscordWebhookMessageHandler(uint16 log_category, int webhook_id, const std::string &message)
{
std::string message_prefix = fmt::format(
"[**{}**] **World** ",
Logs::LogCategoryName[log_category]
);
SendDiscordMessage(webhook_id, message_prefix + Discord::FormatDiscordMessage(log_category, message));
};
};