Skip to content

Commit

Permalink
Fix crash of invalid discord url and wrong player death event name ty…
Browse files Browse the repository at this point in the history
…po (#237)

Fixed player death event name typo (from "Player Death" to "PlayerDeath"), on login.lua is: player:registerEvent("PlayerDeath")

Fixed a bug that we have in the discord webhook in the project, and the bug happens when the server can't access the discord because of an invalid url or a lack of connection from the discord itself

Behaviour actual:
Server crash "closes unexpectedly" if invalid url is set in "config.lua" or "discord_webhook.lua"

Expected:
The server has to work normally regardless of whether the url is correct or not and whether discord receives it or not
  • Loading branch information
beats-dh authored Apr 17, 2022
1 parent 653c65a commit 5714e14
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 deletions.
2 changes: 1 addition & 1 deletion data/scripts/creaturescripts/player_death.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local playerDeath = CreatureEvent("Player Death")
local playerDeath = CreatureEvent("PlayerDeath")

local deathListEnabled = true
local maxDeathRecords = 5
Expand Down
12 changes: 8 additions & 4 deletions data/scripts/custom/discord_webhook.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@
-- Leave empty if you wish to disable.

announcementChannels = {
["serverAnnouncements"] = "https://discord.com/api/webhooks/917947825910849556/hRf4tCzjNiFFG_hlza96fQ6wgM797Mt0aHW1o8Gw-wEw39GyJsVM4WRS4HM84sTUSzSG", -- Used for an announcement channel on your discord
["raids"] = "https://discord.com/api/webhooks/917948005691310100/z02hFUonVSWBuRH2V1zJEUELHOKTBrMqfThJxUMrk5CovIzresLAwr_oEo65f4WQyaVv", -- Used to isolate raids on your discord
["player-kills"] = "https://discord.com/api/webhooks/917948118585204737/67_SpZJM6JA5SZt4kc4FNVjLSFD4ebIyGkf3v8aYnnmb74FXlKUDQRAa-L4nSYf35_IF", -- Self-explaining
["serverAnnouncements"] = "", -- Used for an announcement channel on your discord
["raids"] = "", -- Used to isolate raids on your discord
["player-kills"] = "", -- Self-explaining
}

--[[
Example of notification (After you do the config):
This is going to send a message into your server announcements channel
Webhook.specialSend("Server save", message, WEBHOOK_COLOR_WARNING,
local message = blablabla
local title = test
Webhook.send(title, message, WEBHOOK_COLOR_WARNING,
announcementChannels["serverAnnouncements"])
Dev Comment: This lib can be used to add special webhook channels
where you are going to send your messages. Webhook.specialSend was designed
to be used with countless possibilities.
Expand Down
9 changes: 0 additions & 9 deletions src/server/network/webhook/webhook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,6 @@ static std::string get_payload(std::string title, std::string message, int color
return out.str();
}

// Can't labmda this for some reason
static size_t write_data(void *ptr, size_t size, size_t nmemb, void *userdata) {
(*static_cast<std::string *>(userdata)).append(static_cast<char *>(ptr), size * nmemb);
return size * nmemb;
}

static int webhook_send_message_(const char *url, const char *payload, std::string *response_body) {
CURL *curl = curl_easy_init();
if (!curl) {
Expand All @@ -125,10 +119,7 @@ static int webhook_send_message_(const char *url, const char *payload, std::stri
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, payload);

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, reinterpret_cast<void *>(&response_body));

curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "canary (https://github.com/Hydractify/canary)");

Expand Down

0 comments on commit 5714e14

Please sign in to comment.