From ecf2a369ccaaa3bb9d6df39e2c387c703d6c0995 Mon Sep 17 00:00:00 2001 From: Chris Miles Date: Sat, 29 Apr 2023 19:49:06 -0500 Subject: [PATCH] [Discord] Add Discord webhook callback processing to world (#3322) --- world/ucs.h | 1 + world/world_boot.cpp | 17 +++++++++++++++++ world/world_boot.h | 13 +++++++++++++ 3 files changed, 31 insertions(+) diff --git a/world/ucs.h b/world/ucs.h index 679229736..d226e4dc9 100644 --- a/world/ucs.h +++ b/world/ucs.h @@ -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 &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; } diff --git a/world/world_boot.cpp b/world/world_boot.cpp index d3775fa5b..e39b39be6 100644 --- a/world/world_boot.cpp +++ b/world/world_boot.cpp @@ -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); + } +} + diff --git a/world/world_boot.h b/world/world_boot.h index 19af9bba7..de3b4db6f 100644 --- a/world/world_boot.h +++ b/world/world_boot.h @@ -3,6 +3,9 @@ #include #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)); + }; };