From f604e9c8def68d15829e41f896d1cb2a5072922a Mon Sep 17 00:00:00 2001 From: Salanto <62221668+Salanto@users.noreply.github.com> Date: Wed, 11 Aug 2021 23:52:53 +0200 Subject: [PATCH 1/9] Split discord configuration into different file The current config.ini is both long and full of text. This reduces readability and combines essential configuration with non-essential configuration. Splitting optional features like Discord into its own config helps the readability issue by not beating the users to death with information. --- bin/config_sample/config.ini | 30 ------------------------------ bin/config_sample/discord.ini | 32 ++++++++++++++++++++++++++++++++ core/include/config_manager.h | 14 +++++++++++++- core/src/config_manager.cpp | 25 ++++++++++++++++--------- 4 files changed, 61 insertions(+), 40 deletions(-) create mode 100644 bin/config_sample/discord.ini diff --git a/bin/config_sample/config.ini b/bin/config_sample/config.ini index 882106d..f89cfd9 100644 --- a/bin/config_sample/config.ini +++ b/bin/config_sample/config.ini @@ -82,36 +82,6 @@ max_value=100 ; The maximum number of dice that can be rolled at once. max_dice=100 -[Discord] -; Whether the discord webhook is enabled or not. -; The server will send messages to this webhook whenever a modcall is sent. -; Changing this requires a server restart. -webhook_enabled=false - -; The URL of the discord webhook to send messages to. Must contain the webhook ID and token. -webhook_modcall_url= - -; Whether to attach a file containing the area log when a modcall message is sent to the webhook. -webhook_modcall_sendfile=false - -; Additional text to send with the webhook message. Usually for adding tags for role. Ensure the format is <@&[RoleID]>. -webhook_content= - -; Enables the ban webhook. -webhook_ban_enabled = false - -; The URL of the ban discord webhook to send messages to. Must contain the webhook ID and token. -webhook_ban_url= - -; Enables Uptime Webhook. -webhook_uptime_enabled = false - -; The time between message posting. This time is in minutes, with a default of 60. -webhook_uptime_time = 60 - -; The URL of the uptime discord webhook to send messages to. Must contain the webhook ID and token. -webhook_uptime_url= - [Password] ; Whether or not to enforce password requirements. Only applicable under advanced authorization. password_requirements = true diff --git a/bin/config_sample/discord.ini b/bin/config_sample/discord.ini new file mode 100644 index 0000000..dcefa23 --- /dev/null +++ b/bin/config_sample/discord.ini @@ -0,0 +1,32 @@ +[Discord] +; Whether the discord webhook is enabled or not. +; The server will send messages to this webhook whenever a modcall is sent. +; Changing this requires a server restart. +webhook_enabled=false + +;Enables the modcall webhook to post a notification when a modcall is made. +webhook_modcall_enabled=false + +; The URL of the discord webhook to send messages to. Must contain the webhook ID and token. +webhook_modcall_url= + +; Additional text to send with the webhook message. Usually for adding tags for role. Ensure the format is <@&[RoleID]>. +webhook_modcall_content= + +; Whether to attach a file containing the area log when a modcall message is sent to the webhook. +webhook_modcall_sendfile=false + +; Enables the ban webhook. +webhook_ban_enabled = false + +; The URL of the ban discord webhook to send messages to. Must contain the webhook ID and token. +webhook_ban_url= + +; Enables Uptime Webhook. +webhook_uptime_enabled = false + +; The time between message posting. This time is in minutes, with a default of 60. +webhook_uptime_time = 60 + +; The URL of the uptime discord webhook to send messages to. Must contain the webhook ID and token. +webhook_uptime_url= diff --git a/core/include/config_manager.h b/core/include/config_manager.h index 4103ff8..60e9a41 100644 --- a/core/include/config_manager.h +++ b/core/include/config_manager.h @@ -191,12 +191,19 @@ class ConfigManager { static int diceMaxDice(); /** - * @brief Returns true if the discord webhook is enabled. + * @brief Returns true if the discord webhook integration is enabled. * * @return See short description. */ static bool discordWebhookEnabled(); + /** + * @brief Returns true if the discord modcall webhook is enabled. + * + * @return See short description. + */ + static bool discordModcallWebhookEnabled(); + /** * @brief Returns the discord webhook URL. * @@ -406,6 +413,11 @@ private: */ static QSettings* m_settings; + /** + * @brief Stores all discord webhook configuration values. + */ + static QSettings* m_discord; + /** * @brief Returns a stringlist with the contents of a .txt file from config/text/. * diff --git a/core/src/config_manager.cpp b/core/src/config_manager.cpp index 36b4e2e..af4718f 100644 --- a/core/src/config_manager.cpp +++ b/core/src/config_manager.cpp @@ -18,6 +18,7 @@ #include "include/config_manager.h" QSettings* ConfigManager::m_settings = new QSettings("config/config.ini", QSettings::IniFormat); +QSettings* ConfigManager::m_discord = new QSettings("config/discord.ini", QSettings::IniFormat); ConfigManager::CommandSettings* ConfigManager::m_commands = new CommandSettings(); bool ConfigManager::verifyServerConfig() @@ -90,6 +91,7 @@ bool ConfigManager::verifyServerConfig() void ConfigManager::reloadSettings() { m_settings->sync(); + m_discord->sync(); } QStringList ConfigManager::loadConfigFile(const QString filename) @@ -267,43 +269,48 @@ int ConfigManager::diceMaxDice() bool ConfigManager::discordWebhookEnabled() { - return m_settings->value("Discord/webhook_enabled", false).toBool(); + return m_discord->value("Discord/webhook_enabled", false).toBool(); +} + +bool ConfigManager::discordModcallWebhookEnabled() +{ + return m_discord->value("Discord/webhook_modcall_enabled", false).toBool(); } QString ConfigManager::discordModcallWebhookUrl() { - return m_settings->value("Discord/webhook_modcall_url", "").toString(); + return m_discord->value("Discord/webhook_modcall_url", "").toString(); } QString ConfigManager::discordWebhookContent() { - return m_settings->value("Discord/webhook_content", "").toString(); + return m_discord->value("Discord/webhook_content", "").toString(); } bool ConfigManager::discordModcallWebhookSendFile() { - return m_settings->value("Discord/webhook_modcall_sendfile", false).toBool(); + return m_discord->value("Discord/webhook_modcall_sendfile", false).toBool(); } bool ConfigManager::discordBanWebhookEnabled() { - return m_settings->value("Discord/webhook_ban_enabled", false).toBool(); + return m_discord->value("Discord/webhook_ban_enabled", false).toBool(); } QString ConfigManager::discordBanWebhookUrl() { - return m_settings->value("Discord/webhook_ban_url", "").toString(); + return m_discord->value("Discord/webhook_ban_url", "").toString(); } bool ConfigManager::discordUptimeEnabled() { - return m_settings->value("Discord/webhook_uptime_enabled","false").toBool(); + return m_discord->value("Discord/webhook_uptime_enabled","false").toBool(); } int ConfigManager::discordUptimeTime() { bool ok; - int l_aliveTime = m_settings->value("Discord/webhook_uptime_time","60").toInt(&ok); + int l_aliveTime = m_discord->value("Discord/webhook_uptime_time","60").toInt(&ok); if (!ok) { qWarning("alive_time is not an int"); l_aliveTime = 60; @@ -313,7 +320,7 @@ int ConfigManager::discordUptimeTime() QString ConfigManager::discordUptimeWebhookUrl() { - return m_settings->value("Discord/webhook_uptime_url", "").toString(); + return m_discord->value("Discord/webhook_uptime_url", "").toString(); } bool ConfigManager::passwordRequirements() From 893b3f6cb1804c23a869f0368cda5319b68b4e02 Mon Sep 17 00:00:00 2001 From: Salanto <62221668+Salanto@users.noreply.github.com> Date: Thu, 12 Aug 2021 20:43:03 +0200 Subject: [PATCH 2/9] Document ban webhook signal --- core/include/server.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/core/include/server.h b/core/include/server.h index 8ebd5de..1065d53 100644 --- a/core/include/server.h +++ b/core/include/server.h @@ -260,7 +260,14 @@ class Server : public QObject { */ void modcallWebhookRequest(const QString& f_name, const QString& f_area, const QString& f_reason, const QQueue& f_buffer); - + /** + * @brief Sends a ban webhook request, emitted by AOClient::cmdBan + * @param f_ipid The IPID of the banned client. + * @param f_moderator The moderator who issued the ban. + * @param f_duration The duration of the ban in a human readable format. + * @param f_reason The reason for the ban. + * @param f_banID The ID of the issued ban. + */ void banWebhookRequest(const QString& f_ipid, const QString& f_moderator, const QString& f_duration, const QString& f_reason, const int& f_banID); private: From 51ca518aeb54f419f4057e9f327751603b9bd022 Mon Sep 17 00:00:00 2001 From: Salanto <62221668+Salanto@users.noreply.github.com> Date: Thu, 12 Aug 2021 21:33:13 +0200 Subject: [PATCH 3/9] Add QElapsedTimer to track server uptime Had no better place to put it without making it needlessly complicated. --- core/include/config_manager.h | 11 +++++++++++ core/src/config_manager.cpp | 8 ++++++++ 2 files changed, 19 insertions(+) diff --git a/core/include/config_manager.h b/core/include/config_manager.h index 60e9a41..7f73c47 100644 --- a/core/include/config_manager.h +++ b/core/include/config_manager.h @@ -29,6 +29,7 @@ #include #include #include +#include /** * @brief The config file handler class. @@ -355,6 +356,11 @@ class ConfigManager { */ static QUrl advertiserHTTPIP(); + /** + * @brief Returns the uptime of the server in miliseconds. + */ + static qint64 uptime(); + /** * @brief Sets the server's authorization type. * @@ -424,6 +430,11 @@ private: * @param Name of the file to load. */ static QStringList loadConfigFile(const QString filename); + + /** + * @brief Pointer to QElapsedTimer to track the uptime of the server. + */ + static QElapsedTimer* uptimeTimer; }; diff --git a/core/src/config_manager.cpp b/core/src/config_manager.cpp index af4718f..085799b 100644 --- a/core/src/config_manager.cpp +++ b/core/src/config_manager.cpp @@ -85,6 +85,9 @@ bool ConfigManager::verifyServerConfig() m_commands->reprimands = (loadConfigFile("reprimands")); m_commands->gimps = (loadConfigFile("gimp")); + uptimeTimer = new QElapsedTimer; + uptimeTimer->start(); + return true; } @@ -421,6 +424,11 @@ QUrl ConfigManager::advertiserHTTPIP() return m_settings->value("ModernAdvertiser/ms_ip","").toUrl(); } +qint64 ConfigManager::uptime() +{ + return uptimeTimer->elapsed(); +} + void ConfigManager::setMotd(const QString f_motd) { m_settings->setValue("Options/motd", f_motd); From 9148e522aa3bb534690237a36831c05ac81e954f Mon Sep 17 00:00:00 2001 From: Salanto <62221668+Salanto@users.noreply.github.com> Date: Thu, 12 Aug 2021 21:44:53 +0200 Subject: [PATCH 4/9] Make webhook reloadable --- core/include/server.h | 7 +++++++ core/src/commands/moderation.cpp | 1 + core/src/server.cpp | 29 ++++++++++++++++++++++------- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/core/include/server.h b/core/include/server.h index 1065d53..aefe125 100644 --- a/core/include/server.h +++ b/core/include/server.h @@ -228,6 +228,13 @@ class Server : public QObject { */ void allowMessage(); + /** + * @brief Method to construct and reconstruct Discord Webhook Integration. + * + * @details Constructs or rebuilds Discord Object during server startup and configuration reload. + */ + void handleDiscordIntegration(); + signals: /** diff --git a/core/src/commands/moderation.cpp b/core/src/commands/moderation.cpp index d4b0433..caea60a 100644 --- a/core/src/commands/moderation.cpp +++ b/core/src/commands/moderation.cpp @@ -417,6 +417,7 @@ void AOClient::cmdReload(int argc, QStringList argv) ConfigManager::reloadSettings(); emit server->reloadRequest(ConfigManager::serverName(), ConfigManager::serverDescription()); server->updateHTTPAdvertiserConfig(); + server->handleDiscordIntegration(); sendServerMessage("Reloaded configurations"); } diff --git a/core/src/server.cpp b/core/src/server.cpp index 775ca69..7187874 100644 --- a/core/src/server.cpp +++ b/core/src/server.cpp @@ -52,13 +52,7 @@ void Server::start() qDebug() << "Server listening on" << port; } - if (ConfigManager::discordWebhookEnabled()) { - discord = new Discord(this); - connect(this, &Server::modcallWebhookRequest, - discord, &Discord::onModcallWebhookRequested); - connect(this, &Server::banWebhookRequest, - discord, &Discord::onBanWebhookRequested); - } + handleDiscordIntegration(); if (ConfigManager::advertiseHTTPServer()) { httpAdvertiserTimer = new QTimer(this); @@ -289,6 +283,27 @@ void Server::allowMessage() can_send_ic_messages = true; } +void Server::handleDiscordIntegration() +{ + if (discord != nullptr) { + discord->deleteLater(); + return; + } + + if (ConfigManager::discordWebhookEnabled()) { + discord = new Discord(this); + + if (ConfigManager::discordModcallWebhookEnabled()) + connect(this, &Server::modcallWebhookRequest, + discord, &Discord::onModcallWebhookRequested); + + if (ConfigManager::discordBanWebhookEnabled()) + connect(this, &Server::banWebhookRequest, + discord, &Discord::onBanWebhookRequested); + } + return; +} + Server::~Server() { for (AOClient* client : clients) { From 931365d1bd9ab5c11116af4de63fea946c7d2cb3 Mon Sep 17 00:00:00 2001 From: Salanto <62221668+Salanto@users.noreply.github.com> Date: Thu, 12 Aug 2021 22:03:42 +0200 Subject: [PATCH 5/9] Resolve compile error --- core/include/config_manager.h | 10 +++++----- core/src/config_manager.cpp | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/core/include/config_manager.h b/core/include/config_manager.h index 7f73c47..9af71b3 100644 --- a/core/include/config_manager.h +++ b/core/include/config_manager.h @@ -424,17 +424,17 @@ private: */ static QSettings* m_discord; + /** + * @brief Pointer to QElapsedTimer to track the uptime of the server. + */ + static QElapsedTimer* m_uptimeTimer; + /** * @brief Returns a stringlist with the contents of a .txt file from config/text/. * * @param Name of the file to load. */ static QStringList loadConfigFile(const QString filename); - - /** - * @brief Pointer to QElapsedTimer to track the uptime of the server. - */ - static QElapsedTimer* uptimeTimer; }; diff --git a/core/src/config_manager.cpp b/core/src/config_manager.cpp index 085799b..49e1401 100644 --- a/core/src/config_manager.cpp +++ b/core/src/config_manager.cpp @@ -20,6 +20,7 @@ QSettings* ConfigManager::m_settings = new QSettings("config/config.ini", QSettings::IniFormat); QSettings* ConfigManager::m_discord = new QSettings("config/discord.ini", QSettings::IniFormat); ConfigManager::CommandSettings* ConfigManager::m_commands = new CommandSettings(); +QElapsedTimer* ConfigManager::m_uptimeTimer = new QElapsedTimer; bool ConfigManager::verifyServerConfig() { @@ -85,8 +86,7 @@ bool ConfigManager::verifyServerConfig() m_commands->reprimands = (loadConfigFile("reprimands")); m_commands->gimps = (loadConfigFile("gimp")); - uptimeTimer = new QElapsedTimer; - uptimeTimer->start(); + m_uptimeTimer->start(); return true; } @@ -426,7 +426,7 @@ QUrl ConfigManager::advertiserHTTPIP() qint64 ConfigManager::uptime() { - return uptimeTimer->elapsed(); + return m_uptimeTimer->elapsed(); } void ConfigManager::setMotd(const QString f_motd) From f4038d4678a295979b9f475f19d11892b124a244 Mon Sep 17 00:00:00 2001 From: Salanto <62221668+Salanto@users.noreply.github.com> Date: Thu, 12 Aug 2021 22:55:27 +0200 Subject: [PATCH 6/9] Cleanup Discord class and fix reload error --- core/include/discord.h | 11 ----------- core/include/server.h | 2 +- core/src/discord.cpp | 12 ++++-------- core/src/server.cpp | 2 +- 4 files changed, 6 insertions(+), 21 deletions(-) diff --git a/core/include/discord.h b/core/include/discord.h index 439e485..d207308 100644 --- a/core/include/discord.h +++ b/core/include/discord.h @@ -141,17 +141,6 @@ private: */ QTimer* m_uptimePostTimer; - /** - * @brief Stores how long the interval between postings is. - **/ - int m_uptimeInterval; - - /** - * @brief Proof that Salanto does not know what he is doing. - * @details Counts how often the server alive counter has been posted. - */ - int m_uptimeCounter; - private slots: /** * @brief Handles a network reply from a webhook POST request. diff --git a/core/include/server.h b/core/include/server.h index aefe125..b08f5a9 100644 --- a/core/include/server.h +++ b/core/include/server.h @@ -293,7 +293,7 @@ class Server : public QObject { /** * @brief Handles Discord webhooks. */ - Discord* discord; + Discord* discord = nullptr; /** * @brief Handles HTTP server advertising. diff --git a/core/src/discord.cpp b/core/src/discord.cpp index 8d52292..f300cb5 100644 --- a/core/src/discord.cpp +++ b/core/src/discord.cpp @@ -26,12 +26,9 @@ Discord::Discord(QObject* parent) : if (ConfigManager::discordUptimeEnabled()){ m_uptimePostTimer = new QTimer; - m_uptimeInterval = ConfigManager::discordUptimeTime() * 60000; - m_uptimeCounter = 0; - connect(m_uptimePostTimer, &QTimer::timeout, this, &Discord::onUptimeWebhookRequested); - m_uptimePostTimer->start(m_uptimeInterval); + m_uptimePostTimer->start(ConfigManager::discordUptimeTime() * 60000); onUptimeWebhookRequested(); } } @@ -57,7 +54,7 @@ void Discord::onBanWebhookRequested(const QString &f_ipid, const QString &f_mode void Discord::onUptimeWebhookRequested() { - ulong l_expiredTimeSeconds = (m_uptimeCounter * m_uptimeInterval) / 1000; + qint64 l_expiredTimeSeconds = ConfigManager::uptime() / 1000; int minutes = (l_expiredTimeSeconds / 60) % 60; int hours = (l_expiredTimeSeconds / (60 * 60)) % 24; int days = (l_expiredTimeSeconds / (60 * 60 * 24)) % 365; @@ -66,7 +63,6 @@ void Discord::onUptimeWebhookRequested() QString f_timeExpired = QString::number(days) + " days, " + QString::number(hours) + " hours and " + QString::number(minutes) + " minutes."; QJsonDocument l_json = constructUptimeJson(f_timeExpired); postJsonWebhook(l_json); - m_uptimeCounter++; } QJsonDocument Discord::constructModcallJson(const QString &f_name, const QString &f_area, const QString &f_reason) const @@ -107,8 +103,8 @@ QJsonDocument Discord::constructUptimeJson(const QString& f_timeExpired) QJsonArray l_array; QJsonObject l_object { {"color", "13312842"}, - {"title", "Your server is still running!"}, - {"description", "Hello World!\nYour server has been online for " + f_timeExpired} + {"title", "Your server is online!"}, + {"description", "Your server has been online for " + f_timeExpired} }; l_array.append(l_object); l_json["embeds"] = l_array; diff --git a/core/src/server.cpp b/core/src/server.cpp index 7187874..dfe8c1b 100644 --- a/core/src/server.cpp +++ b/core/src/server.cpp @@ -287,7 +287,7 @@ void Server::handleDiscordIntegration() { if (discord != nullptr) { discord->deleteLater(); - return; + discord = nullptr; } if (ConfigManager::discordWebhookEnabled()) { From 5b07bb5557eeb01da9715c1206b4820f6fe3cd43 Mon Sep 17 00:00:00 2001 From: Salanto <62221668+Salanto@users.noreply.github.com> Date: Thu, 12 Aug 2021 23:03:46 +0200 Subject: [PATCH 7/9] Update config descriptions for Discord.ini --- bin/config_sample/discord.ini | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/bin/config_sample/discord.ini b/bin/config_sample/discord.ini index dcefa23..ce3a496 100644 --- a/bin/config_sample/discord.ini +++ b/bin/config_sample/discord.ini @@ -1,19 +1,17 @@ [Discord] -; Whether the discord webhook is enabled or not. -; The server will send messages to this webhook whenever a modcall is sent. -; Changing this requires a server restart. +; Enables the Discord Webhook Integration webhook_enabled=false -;Enables the modcall webhook to post a notification when a modcall is made. +; Enables the modcall webhook to post a message when a modcall is made. webhook_modcall_enabled=false -; The URL of the discord webhook to send messages to. Must contain the webhook ID and token. +; The URL of the modcall webhook. Must contain the webhook ID and token. webhook_modcall_url= -; Additional text to send with the webhook message. Usually for adding tags for role. Ensure the format is <@&[RoleID]>. +; Optional text. Usually for adding tags for roles. Ensure the format is <@&[RoleID]>. webhook_modcall_content= -; Whether to attach a file containing the area log when a modcall message is sent to the webhook. +; Attaches a logfile of the area to the modcall webhook. webhook_modcall_sendfile=false ; Enables the ban webhook. @@ -25,8 +23,8 @@ webhook_ban_url= ; Enables Uptime Webhook. webhook_uptime_enabled = false -; The time between message posting. This time is in minutes, with a default of 60. +; The time between message posting. Time is in minutes. webhook_uptime_time = 60 -; The URL of the uptime discord webhook to send messages to. Must contain the webhook ID and token. +; The URL of the Uptime Webhook. Must contain the webhook ID and token. webhook_uptime_url= From 2efb6edee845eb202c867c7c3afa425620eb77c1 Mon Sep 17 00:00:00 2001 From: Salanto <62221668+Salanto@users.noreply.github.com> Date: Thu, 19 Aug 2021 21:46:05 +0200 Subject: [PATCH 8/9] Privatize Discord + use Signals better None of this is called outside of Discord, so there's no point on having it public. Bonus note on the slots : But if you close your eyes, Does it almost feel like Nothing changed at all? --- core/include/discord.h | 111 ++++++++++++++++++++++------------------- core/src/discord.cpp | 21 +++++--- core/src/packets.cpp | 2 +- core/src/server.cpp | 22 ++++---- 4 files changed, 88 insertions(+), 68 deletions(-) diff --git a/core/include/discord.h b/core/include/discord.h index d207308..d9cc814 100644 --- a/core/include/discord.h +++ b/core/include/discord.h @@ -46,6 +46,67 @@ public: */ ~Discord(); + /** + * @brief Method to start the Uptime Webhook posting timer. + */ + void startUptimeTimer(); + + /** + * @brief Method to stop the Uptime Webhook posting timer. + */ + void stopUptimeTimer(); + + +public slots: + /** + * @brief Handles a modcall webhook request. + * + * @param f_name The name of the modcall sender. + * @param f_area The name of the area the modcall was sent from. + * @param f_reason The reason for the modcall. + * @param f_buffer The area's log buffer. + */ + void onModcallWebhookRequested(const QString& f_name, const QString& f_area, const QString& f_reason, const QQueue& f_buffer); + + /** + * @brief Handles a ban webhook request. + * + * @param f_ipid The IPID of the client. + * @param f_moderator The name of the moderator banning. + * @param f_duration The date the ban expires. + * @param f_reason The reason of the ban. + */ + void onBanWebhookRequested(const QString& f_ipid, const QString& f_moderator, const QString& f_duration, const QString& f_reason, const int& f_banID); + + /** + * @brief Handles a uptime webhook request. + */ + void onUptimeWebhookRequested(); + +private: + /** + * @brief The QNetworkAccessManager for webhooks. + */ + QNetworkAccessManager* m_nam; + + /** + * @brief The QNetworkRequest for webhooks. + */ + QNetworkRequest m_request; + + /** + * @brief Timer to post a message that the server is still alive. + */ + QTimer* m_uptimePostTimer; + +private slots: + /** + * @brief Handles a network reply from a webhook POST request. + * + * @param f_reply Pointer to the QNetworkReply created by the webhook POST request. + */ + void onReplyFinished(QNetworkReply* f_reply); + /** * @brief Sends a webhook POST request with the given JSON document. * @@ -98,56 +159,6 @@ public: * @return A QHttpMultiPart containing the log file. */ QHttpMultiPart* constructLogMultipart(const QQueue& f_buffer) const; - -public slots: - /** - * @brief Handles a modcall webhook request. - * - * @param f_name The name of the modcall sender. - * @param f_area The name of the area the modcall was sent from. - * @param f_reason The reason for the modcall. - * @param f_buffer The area's log buffer. - */ - void onModcallWebhookRequested(const QString& f_name, const QString& f_area, const QString& f_reason, const QQueue& f_buffer); - - /** - * @brief Handles a ban webhook request. - * - * @param f_ipid The IPID of the client. - * @param f_moderator The name of the moderator banning. - * @param f_duration The date the ban expires. - * @param f_reason The reason of the ban. - */ - void onBanWebhookRequested(const QString& f_ipid, const QString& f_moderator, const QString& f_duration, const QString& f_reason, const int& f_banID); - - /** - * @brief Handles a uptime webhook request. - */ - void onUptimeWebhookRequested(); - -private: - /** - * @brief The QNetworkAccessManager for webhooks. - */ - QNetworkAccessManager* m_nam; - - /** - * @brief The QNetworkRequest for webhooks. - */ - QNetworkRequest m_request; - - /** - * @brief Timer to post a message that the server is still alive. - */ - QTimer* m_uptimePostTimer; - -private slots: - /** - * @brief Handles a network reply from a webhook POST request. - * - * @param f_reply Pointer to the QNetworkReply created by the webhook POST request. - */ - void onReplyFinished(QNetworkReply* f_reply); }; #endif // DISCORD_H diff --git a/core/src/discord.cpp b/core/src/discord.cpp index f300cb5..3fd8e94 100644 --- a/core/src/discord.cpp +++ b/core/src/discord.cpp @@ -24,13 +24,9 @@ Discord::Discord(QObject* parent) : connect(m_nam, &QNetworkAccessManager::finished, this, &Discord::onReplyFinished); - if (ConfigManager::discordUptimeEnabled()){ - m_uptimePostTimer = new QTimer; - connect(m_uptimePostTimer, &QTimer::timeout, - this, &Discord::onUptimeWebhookRequested); - m_uptimePostTimer->start(ConfigManager::discordUptimeTime() * 60000); - onUptimeWebhookRequested(); - } + m_uptimePostTimer = new QTimer; + connect(m_uptimePostTimer, &QTimer::timeout, + this, &Discord::onUptimeWebhookRequested); } void Discord::onModcallWebhookRequested(const QString &f_name, const QString &f_area, const QString &f_reason, const QQueue &f_buffer) @@ -162,3 +158,14 @@ Discord::~Discord() { m_nam->deleteLater(); } + +void Discord::startUptimeTimer() +{ + m_uptimePostTimer->start(ConfigManager::discordUptimeTime() * 60000); + onUptimeWebhookRequested(); +} + +void Discord::stopUptimeTimer() +{ + m_uptimePostTimer->stop(); +} diff --git a/core/src/packets.cpp b/core/src/packets.cpp index 5e16d4f..5cab906 100644 --- a/core/src/packets.cpp +++ b/core/src/packets.cpp @@ -352,7 +352,7 @@ void AOClient::pktModCall(AreaData* area, int argc, QStringList argv, AOPacket p } area->log(current_char, ipid, packet); - if (ConfigManager::discordWebhookEnabled()) { + if (ConfigManager::discordModcallWebhookEnabled()) { QString name = ooc_name; if (ooc_name.isEmpty()) name = current_char; diff --git a/core/src/server.cpp b/core/src/server.cpp index dfe8c1b..6da6f31 100644 --- a/core/src/server.cpp +++ b/core/src/server.cpp @@ -52,6 +52,7 @@ void Server::start() qDebug() << "Server listening on" << port; } + discord = new Discord(this); handleDiscordIntegration(); if (ConfigManager::advertiseHTTPServer()) { @@ -285,21 +286,22 @@ void Server::allowMessage() void Server::handleDiscordIntegration() { - if (discord != nullptr) { - discord->deleteLater(); - discord = nullptr; - } + // Prevent double connecting by preemtively disconnecting them. + disconnect(this, nullptr, discord, nullptr); if (ConfigManager::discordWebhookEnabled()) { - discord = new Discord(this); - if (ConfigManager::discordModcallWebhookEnabled()) - connect(this, &Server::modcallWebhookRequest, - discord, &Discord::onModcallWebhookRequested); + connect(this, &Server::modcallWebhookRequest, + discord, &Discord::onModcallWebhookRequested); if (ConfigManager::discordBanWebhookEnabled()) - connect(this, &Server::banWebhookRequest, - discord, &Discord::onBanWebhookRequested); + connect(this, &Server::banWebhookRequest, + discord, &Discord::onBanWebhookRequested); + + if (ConfigManager::discordUptimeEnabled()) + discord->startUptimeTimer(); + else + discord->stopUptimeTimer(); } return; } From 5f3d42d9993a2474f37d1c6c25970375931ad4d4 Mon Sep 17 00:00:00 2001 From: Salanto <62221668+Salanto@users.noreply.github.com> Date: Thu, 19 Aug 2021 21:48:33 +0200 Subject: [PATCH 9/9] Remove unusued nullpointer --- core/include/server.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/include/server.h b/core/include/server.h index b08f5a9..aefe125 100644 --- a/core/include/server.h +++ b/core/include/server.h @@ -293,7 +293,7 @@ class Server : public QObject { /** * @brief Handles Discord webhooks. */ - Discord* discord = nullptr; + Discord* discord; /** * @brief Handles HTTP server advertising.