Implement Uptime Webhook
This commit implements a functional uptime webhook which posts a message to the config defined webhook URL. The interval is configurable, but not updatable.
This commit is contained in:
parent
ec45370a71
commit
aaccf8e605
@ -103,6 +103,12 @@ 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
|
||||
|
||||
[Password]
|
||||
; Whether or not to enforce password requirements. Only applicable under advanced authorization.
|
||||
password_requirements = true
|
||||
|
@ -224,7 +224,7 @@ class ConfigManager {
|
||||
* @return See short description.
|
||||
*/
|
||||
static bool discordBanWebhookEnabled();
|
||||
|
||||
|
||||
/**
|
||||
* @brief Returns the Discord Ban Webhook URL.
|
||||
*
|
||||
@ -232,6 +232,23 @@ class ConfigManager {
|
||||
*/
|
||||
static QString discordBanWebhookUrl();
|
||||
|
||||
/**
|
||||
* @brief Returns if the Webhook sends an alive message.
|
||||
*/
|
||||
static bool discordUptimeEnabled();
|
||||
|
||||
/**
|
||||
* @brief Returns the time between posting.
|
||||
*/
|
||||
static int discordUptimeTime();
|
||||
|
||||
/**
|
||||
* @brief Returns the Discord Uptime Webhook URL.
|
||||
*
|
||||
* @return See sshort description.
|
||||
*/
|
||||
static QString discordUptimeWebhookUrl();
|
||||
|
||||
/**
|
||||
* @brief Returns true if password requirements should be enforced.
|
||||
*
|
||||
@ -331,7 +348,6 @@ class ConfigManager {
|
||||
*/
|
||||
static QUrl advertiserHTTPIP();
|
||||
|
||||
|
||||
/**
|
||||
* @brief Sets the server's authorization type.
|
||||
*
|
||||
|
@ -82,6 +82,13 @@ public:
|
||||
* @return A JSON document for the ban.
|
||||
*/
|
||||
QJsonDocument constructBanJson(const QString& f_ipid, const QString& f_moderator, const QString& f_duration, const QString& f_reason, const int& f_banID);
|
||||
/**
|
||||
* @brief Constructs a new JSON document for the server alive message.
|
||||
* @param f_timeExpired formatted uptime as a string.
|
||||
*
|
||||
* @return A JSON document for the alive notification.
|
||||
*/
|
||||
QJsonDocument constructUptimeJson(const QString& f_timeExpired);
|
||||
|
||||
/**
|
||||
* @brief Constructs a new QHttpMultiPart document for log files.
|
||||
@ -113,6 +120,11 @@ public slots:
|
||||
*/
|
||||
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.
|
||||
@ -124,6 +136,22 @@ private:
|
||||
*/
|
||||
QNetworkRequest m_request;
|
||||
|
||||
/**
|
||||
* @brief Timer to post a message that the server is still alive.
|
||||
*/
|
||||
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.
|
||||
|
@ -295,6 +295,27 @@ QString ConfigManager::discordBanWebhookUrl()
|
||||
return m_settings->value("Discord/webhook_ban_url", "").toString();
|
||||
}
|
||||
|
||||
bool ConfigManager::discordUptimeEnabled()
|
||||
{
|
||||
return m_settings->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);
|
||||
if (!ok) {
|
||||
qWarning("alive_time is not an int");
|
||||
l_aliveTime = 60;
|
||||
}
|
||||
return l_aliveTime;
|
||||
}
|
||||
|
||||
QString ConfigManager::discordUptimeWebhookUrl()
|
||||
{
|
||||
return m_settings->value("Discord/webhook_uptime_url", "").toString();
|
||||
}
|
||||
|
||||
bool ConfigManager::passwordRequirements()
|
||||
{
|
||||
return m_settings->value("Password/password_requirements", true).toBool();
|
||||
|
@ -23,6 +23,17 @@ Discord::Discord(QObject* parent) :
|
||||
m_nam = new QNetworkAccessManager();
|
||||
connect(m_nam, &QNetworkAccessManager::finished,
|
||||
this, &Discord::onReplyFinished);
|
||||
|
||||
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);
|
||||
onUptimeWebhookRequested();
|
||||
}
|
||||
}
|
||||
|
||||
void Discord::onModcallWebhookRequested(const QString &f_name, const QString &f_area, const QString &f_reason, const QQueue<QString> &f_buffer)
|
||||
@ -44,6 +55,20 @@ void Discord::onBanWebhookRequested(const QString &f_ipid, const QString &f_mode
|
||||
postJsonWebhook(l_json);
|
||||
}
|
||||
|
||||
void Discord::onUptimeWebhookRequested()
|
||||
{
|
||||
ulong l_expiredTimeSeconds = (m_uptimeCounter * m_uptimeInterval) / 1000;
|
||||
int minutes = (l_expiredTimeSeconds / 60) % 60;
|
||||
int hours = (l_expiredTimeSeconds / (60 * 60)) % 24;
|
||||
int days = (l_expiredTimeSeconds / (60 * 60 * 24)) % 365;
|
||||
|
||||
m_request.setUrl(QUrl(ConfigManager::discordUptimeWebhookUrl()));
|
||||
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
|
||||
{
|
||||
QJsonObject l_json;
|
||||
@ -76,6 +101,21 @@ QJsonDocument Discord::constructBanJson(const QString &f_ipid, const QString &f_
|
||||
return QJsonDocument(l_json);
|
||||
}
|
||||
|
||||
QJsonDocument Discord::constructUptimeJson(const QString& f_timeExpired)
|
||||
{
|
||||
QJsonObject l_json;
|
||||
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}
|
||||
};
|
||||
l_array.append(l_object);
|
||||
l_json["embeds"] = l_array;
|
||||
|
||||
return QJsonDocument(l_json);
|
||||
}
|
||||
|
||||
QHttpMultiPart* Discord::constructLogMultipart(const QQueue<QString> &f_buffer) const
|
||||
{
|
||||
QHttpMultiPart* l_multipart = new QHttpMultiPart();
|
||||
|
Loading…
Reference in New Issue
Block a user