diff --git a/config.lua.dist b/config.lua.dist index 7471d5cd768..55a211267e6 100644 --- a/config.lua.dist +++ b/config.lua.dist @@ -474,6 +474,7 @@ location = "South America" -- The URL layout is https://discord.com/api/webhooks/:id/:token -- Leave empty if you wish to disable. discordWebhookURL = "" +discordSendFooter = true -- Vip System (Get more info in: https://github.com/opentibiabr/canary/pull/1063) -- NOTE: set vipSystemEnabled to true to enable the vip system functionalities (this overrides premium checks) diff --git a/data-otservbr-global/scripts/creaturescripts/others/player_death.lua b/data-otservbr-global/scripts/creaturescripts/others/player_death.lua index ef79ffee7d4..9e8bfbeca34 100644 --- a/data-otservbr-global/scripts/creaturescripts/others/player_death.lua +++ b/data-otservbr-global/scripts/creaturescripts/others/player_death.lua @@ -68,16 +68,7 @@ function playerDeath.onDeath(player, corpse, killer, mostDamageKiller, unjustifi ) local resultId = db.storeQuery("SELECT `player_id` FROM `player_deaths` WHERE `player_id` = " .. playerGuid) -- Start Webhook Player Death - local playerName = player:getName() - local playerLevel = player:getLevel() - local killerLink = string.gsub(killerName, "%s+", "+") - local playerLink = string.gsub(playerName, "%s+", "+") - local serverURL = getConfigInfo("url") - if killer and killer:isPlayer() then - Webhook.sendMessage(playerName .. " just got killed!", "**[" .. playerName .. "](" .. serverURL .. "/?characters/" .. playerLink .. ")** got killed at level " .. playerLevel .. " by **[" .. killerName .. "](" .. serverURL .. "/?characters/" .. killerLink .. ")**", WEBHOOK_COLOR_OFFLINE, announcementChannels["player-kills"]) - else - Webhook.sendMessage(playerName .. " has just died!", "**[" .. playerName .. "](" .. serverURL .. "/?characters/" .. playerLink .. ")** died at level " .. playerLevel .. " by " .. killerName, WEBHOOK_COLOR_WARNING, announcementChannels["player-kills"]) - end + Webhook.sendMessage(":skull_crossbones: " .. player:getMarkdownLink() .. " has died. Killed at level _" .. player:getLevel() .. "_ by **" .. killerName .. "**.", announcementChannels["player-kills"]) -- End Webhook Player Death local deathRecords = 0 diff --git a/data-otservbr-global/scripts/globalevents/others/global_server_save.lua b/data-otservbr-global/scripts/globalevents/others/global_server_save.lua index c65255b592b..84303a356da 100644 --- a/data-otservbr-global/scripts/globalevents/others/global_server_save.lua +++ b/data-otservbr-global/scripts/globalevents/others/global_server_save.lua @@ -17,7 +17,7 @@ local function ServerSaveWarning(time) local remainingTime = tonumber(time) - 60000 if configManager.getBoolean(configKeys.GLOBAL_SERVER_SAVE_NOTIFY_MESSAGE) then local message = "Server is saving game in " .. (remainingTime / 60000) .. " minute(s). Please logout." - Webhook.sendMessage("Server save", message, WEBHOOK_COLOR_WARNING) + Webhook.sendMessage(":floppy_disk: " .. message, announcementChannels["serverAnnouncements"]) Game.broadcastMessage(message, MESSAGE_GAME_HIGHLIGHT) end -- if greater than one minute, schedule another warning @@ -36,7 +36,7 @@ function serverSaveEvent.onTime(interval) local remainingTime = configManager.getNumber(configKeys.GLOBAL_SERVER_SAVE_NOTIFY_DURATION) * 60000 if configManager.getBoolean(configKeys.GLOBAL_SERVER_SAVE_NOTIFY_MESSAGE) then local message = "Server is saving game in " .. (remainingTime / 60000) .. " minute(s). Please logout." - Webhook.sendMessage("Server save", message, WEBHOOK_COLOR_WARNING, announcementChannels["serverAnnouncements"]) + Webhook.sendMessage(":floppy_disk: " .. message, announcementChannels["serverAnnouncements"]) Game.broadcastMessage(message, MESSAGE_GAME_HIGHLIGHT) end addEvent(ServerSaveWarning, 60000, remainingTime) -- Schedule next event in 1 minute(60000) diff --git a/data-otservbr-global/scripts/globalevents/spawn/rashid.lua b/data-otservbr-global/scripts/globalevents/spawn/rashid.lua index 0e21385eb75..78fd80d874e 100644 --- a/data-otservbr-global/scripts/globalevents/spawn/rashid.lua +++ b/data-otservbr-global/scripts/globalevents/spawn/rashid.lua @@ -9,7 +9,7 @@ local positionByDay = { } local function rashidwebhook(message) -- New local function that runs on delay to send webhook message. - Webhook.sendMessage("[Rashid] ", message, WEBHOOK_COLOR_ONLINE) --Sends webhook message + Webhook.sendMessage(":man_wearing_turban: " .. message, announcementChannels["serverAnnouncements"]) end local rashid = GlobalEvent("rashid") diff --git a/data-otservbr-global/scripts/globalevents/worldchanges/fury_gates.lua b/data-otservbr-global/scripts/globalevents/worldchanges/fury_gates.lua index 44c5a4c78c6..a50cc253016 100644 --- a/data-otservbr-global/scripts/globalevents/worldchanges/fury_gates.lua +++ b/data-otservbr-global/scripts/globalevents/worldchanges/fury_gates.lua @@ -101,7 +101,7 @@ local gates = { } local function Furywebhook(message) -- New local function that runs on delay to send webhook message. - Webhook.sendMessage("[Fury Gates] ", message, WEBHOOK_COLOR_ONLINE) --Sends webhook message + Webhook.sendMessage(":fire: " .. message, announcementChannels["serverAnnouncements"]) end -- FURY GATES MAP LOAD diff --git a/data-otservbr-global/scripts/globalevents/worldchanges/nightmare_isle.lua b/data-otservbr-global/scripts/globalevents/worldchanges/nightmare_isle.lua index b367b490679..fb0d3b6f672 100644 --- a/data-otservbr-global/scripts/globalevents/worldchanges/nightmare_isle.lua +++ b/data-otservbr-global/scripts/globalevents/worldchanges/nightmare_isle.lua @@ -17,7 +17,7 @@ local config = { } local function Nightmarewebhook(message) -- New local function that runs on delay to send webhook message. - Webhook.sendMessage("[Nightmare Isle] ", message, WEBHOOK_COLOR_ONLINE) --Sends webhook message + Webhook.sendMessage(":thought_balloon: " .. message, announcementChannels["serverAnnouncements"]) end local NightmareIsle = GlobalEvent("NightmareIsle") diff --git a/data-otservbr-global/scripts/globalevents/worldchanges/yasir.lua b/data-otservbr-global/scripts/globalevents/worldchanges/yasir.lua index a0bd427f568..5807c44387b 100644 --- a/data-otservbr-global/scripts/globalevents/worldchanges/yasir.lua +++ b/data-otservbr-global/scripts/globalevents/worldchanges/yasir.lua @@ -46,7 +46,7 @@ local config = { } local function yasirwebhook(message) -- New local function that runs on delay to send webhook message. - Webhook.sendMessage("[Yasir] ", message, WEBHOOK_COLOR_ONLINE) --Sends webhook message + Webhook.sendMessage(":man_wearing_turban_tone4: " .. message, announcementChannels["serverAnnouncements"]) end local yasirEnabled = true diff --git a/data/events/scripts/player.lua b/data/events/scripts/player.lua index c134e784993..d327b06015e 100644 --- a/data/events/scripts/player.lua +++ b/data/events/scripts/player.lua @@ -662,3 +662,24 @@ function Player:onChangeZone(zone) end function Player:onInventoryUpdate(item, slot, equip) end + +function Player:getURL() + local playerLink = string.gsub(self:getName(), "%s+", "+") + local serverURL = configManager.getString(configKeys.URL) + return serverURL .. "/characters/" .. playerLink +end + +function Player:getMarkdownLink() + local vocation = self:vocationAbbrev() + local emoji = ":school_satchel:" + if self:isKnight() then + emoji = ":crossed_swords:" + elseif self:isPaladin() then + emoji = ":bow_and_arrow:" + elseif self:isDruid() then + emoji = ":herb:" + elseif self:isSorcerer() then + emoji = ":crystal_ball:" + end + return "**[" .. self:getName() .. "](" .. self:getURL() .. ")** " .. emoji .. " [_" .. vocation .. "_]" +end diff --git a/data/scripts/discord_webhook/discord_webhook.lua b/data/scripts/discord_webhook/discord_webhook.lua index 756078c9f61..0aed824b278 100644 --- a/data/scripts/discord_webhook/discord_webhook.lua +++ b/data/scripts/discord_webhook/discord_webhook.lua @@ -14,7 +14,7 @@ announcementChannels = { local message = blablabla local title = test - Webhook.sendMessage(title, message, WEBHOOK_COLOR_WARNING, + Webhook.sendMessage(title, message, WEBHOOK_COLOR_YELLOW, announcementChannels["serverAnnouncements"]) Dev Comment: This lib can be used to add special webhook channels diff --git a/data/scripts/talkactions/gm/ban.lua b/data/scripts/talkactions/gm/ban.lua index 8a45a6c7d1b..8b46394a3e2 100644 --- a/data/scripts/talkactions/gm/ban.lua +++ b/data/scripts/talkactions/gm/ban.lua @@ -38,7 +38,7 @@ function ban.onSay(player, words, param) if target then local text = target:getName() .. " has been banned" player:sendTextMessage(MESSAGE_ADMINISTRADOR, text) - Webhook.sendMessage("Player Banned", text .. " reason: " .. reason .. ". (by: " .. player:getName() .. ")", WEBHOOK_COLOR_WARNING, announcementChannels["serverAnnouncements"]) + Webhook.sendMessage("Player Banned", text .. " reason: " .. reason .. ". (by: " .. player:getName() .. ")", WEBHOOK_COLOR_YELLOW, announcementChannels["serverAnnouncements"]) target:remove() else player:sendTextMessage(MESSAGE_ADMINISTRADOR, name .. " has been banned.") diff --git a/data/scripts/talkactions/gm/kick.lua b/data/scripts/talkactions/gm/kick.lua index fe7581b40e3..a7c67c5c941 100644 --- a/data/scripts/talkactions/gm/kick.lua +++ b/data/scripts/talkactions/gm/kick.lua @@ -15,7 +15,7 @@ function kick.onSay(player, words, param) return true end - Webhook.sendMessage("Player Kicked", target:getName() .. " has been kicked by " .. player:getName(), WEBHOOK_COLOR_WARNING, announcementChannels["serverAnnouncements"]) + Webhook.sendMessage("Player Kicked", target:getName() .. " has been kicked by " .. player:getName(), WEBHOOK_COLOR_YELLOW, announcementChannels["serverAnnouncements"]) target:remove() return true end diff --git a/data/scripts/talkactions/gm/push_town.lua b/data/scripts/talkactions/gm/push_town.lua index 7c2060839c3..cf32d148d20 100644 --- a/data/scripts/talkactions/gm/push_town.lua +++ b/data/scripts/talkactions/gm/push_town.lua @@ -18,7 +18,7 @@ function pushTown.onSay(player, words, param) targetPlayer:getPosition():sendMagicEffect(CONST_ME_HOLYAREA) local text = "Player " .. targetPlayer:getName() .. " has been teleported to temple by " .. player:getName() .. "." logger.info("[pushTown.onSay] - {}", text) - Webhook.sendMessage("Player Teleported", text, WEBHOOK_COLOR_WARNING, announcementChannels["serverAnnouncements"]) + Webhook.sendMessage("Player Teleported", text, WEBHOOK_COLOR_YELLOW, announcementChannels["serverAnnouncements"]) end return true end diff --git a/data/scripts/talkactions/gm/unban.lua b/data/scripts/talkactions/gm/unban.lua index d791d85b4e7..264335eeec7 100644 --- a/data/scripts/talkactions/gm/unban.lua +++ b/data/scripts/talkactions/gm/unban.lua @@ -19,7 +19,7 @@ function unban.onSay(player, words, param) Result.free(resultId) local text = param .. " has been unbanned." player:sendTextMessage(MESSAGE_ADMINISTRADOR, text) - Webhook.sendMessage("Player Unbanned", text .. " (by: " .. player:getName() .. ")", WEBHOOK_COLOR_WARNING, announcementChannels["serverAnnouncements"]) + Webhook.sendMessage("Player Unbanned", text .. " (by: " .. player:getName() .. ")", WEBHOOK_COLOR_YELLOW, announcementChannels["serverAnnouncements"]) return true end diff --git a/data/scripts/talkactions/god/close_server.lua b/data/scripts/talkactions/god/close_server.lua index a807b4f72a1..930cde5be79 100644 --- a/data/scripts/talkactions/god/close_server.lua +++ b/data/scripts/talkactions/god/close_server.lua @@ -6,7 +6,7 @@ function closeServer.onSay(player, words, param) if param == "shutdown" then Game.setGameState(GAME_STATE_SHUTDOWN) - Webhook.sendMessage("Server Shutdown", "Server was shutdown by: " .. player:getName(), WEBHOOK_COLOR_WARNING, announcementChannels["serverAnnouncements"]) + Webhook.sendMessage(":red_circle: Server was shutdown by: **" .. player:getName() .. "**", announcementChannels["serverAnnouncements"]) elseif param == "save" then if configManager.getBoolean(configKeys.GLOBAL_SERVER_SAVE_CLEAN_MAP) then cleanMap() @@ -17,15 +17,13 @@ function closeServer.onSay(player, words, param) if configManager.getBoolean(configKeys.GLOBAL_SERVER_SAVE_SHUTDOWN) then Game.setGameState(GAME_STATE_SHUTDOWN, true) end - -- Updating daily reward next server save. - UpdateDailyRewardGlobalStorage(DailyReward.storages.lastServerSave, os.time()) elseif param == "maintainance" then Game.setGameState(GAME_STATE_MAINTAIN) player:sendTextMessage(MESSAGE_ADMINISTRADOR, "Server is set to maintenance mode.") else Game.setGameState(GAME_STATE_CLOSED) player:sendTextMessage(MESSAGE_ADMINISTRADOR, "Server is now closed.") - Webhook.sendMessage("Server Closed", "Server was closed by: " .. player:getName(), WEBHOOK_COLOR_WARNING, announcementChannels["serverAnnouncements"]) + Webhook.sendMessage(":yellow_square: Server was closed by: **" .. player:getName() .. "**", announcementChannels["serverAnnouncements"]) end return true end diff --git a/data/scripts/talkactions/god/open_server.lua b/data/scripts/talkactions/god/open_server.lua index f098d05bb99..84f0b4afe49 100644 --- a/data/scripts/talkactions/god/open_server.lua +++ b/data/scripts/talkactions/god/open_server.lua @@ -6,7 +6,7 @@ function openServer.onSay(player, words, param) Game.setGameState(GAME_STATE_NORMAL) player:sendTextMessage(MESSAGE_ADMINISTRADOR, "Server is now open.") - Webhook.sendMessage("Server Open", "Server was opened by: " .. player:getName(), WEBHOOK_COLOR_WARNING, announcementChannels["serverAnnouncements"]) + Webhook.sendMessage(":green_circle: Server was opened by: **" .. player:getName() .. "**", announcementChannels["serverAnnouncements"]) return true end diff --git a/src/canary_server.cpp b/src/canary_server.cpp index f60e0ab42f5..a3a27bfdef4 100644 --- a/src/canary_server.cpp +++ b/src/canary_server.cpp @@ -102,10 +102,10 @@ int CanaryServer::run() { if (g_configManager().getBoolean(TOGGLE_MAINTAIN_MODE, __FUNCTION__)) { g_game().setGameState(GAME_STATE_CLOSED); g_logger().warn("Initialized in maintain mode!"); - g_webhook().sendMessage("Server is now online", "The server is now online. Access is currently restricted to administrators only.", WEBHOOK_COLOR_ONLINE); + g_webhook().sendMessage(":yellow_square: Server is now **online** _(access restricted to staff)_"); } else { g_game().setGameState(GAME_STATE_NORMAL); - g_webhook().sendMessage("Server is now online", "Server has successfully started.", WEBHOOK_COLOR_ONLINE); + g_webhook().sendMessage(":green_circle: Server is now **online**"); } loaderStatus = LoaderStatus::LOADED; diff --git a/src/config/config_definitions.hpp b/src/config/config_definitions.hpp index 920494fea9c..31aa4659e06 100644 --- a/src/config/config_definitions.hpp +++ b/src/config/config_definitions.hpp @@ -51,6 +51,7 @@ enum ConfigKey_t : uint16_t { DEPOT_BOXES, DISABLE_LEGACY_RAIDS, DISABLE_MONSTER_ARMOR, + DISCORD_SEND_FOOTER, DISCORD_WEBHOOK_DELAY_MS, DISCORD_WEBHOOK_URL, EMOTE_SPELLS, diff --git a/src/config/configmanager.cpp b/src/config/configmanager.cpp index 090fcaf73e8..1afbc0295ae 100644 --- a/src/config/configmanager.cpp +++ b/src/config/configmanager.cpp @@ -152,6 +152,7 @@ bool ConfigManager::load() { loadStringConfig(L, WORLD_TYPE, "worldType", "pvp"); loadStringConfig(L, STORE_IMAGES_URL, "coinImagesURL", ""); loadStringConfig(L, DISCORD_WEBHOOK_URL, "discordWebhookURL", ""); + loadBoolConfig(L, DISCORD_SEND_FOOTER, "discordSendFooter", true); loadStringConfig(L, SAVE_INTERVAL_TYPE, "saveIntervalType", ""); loadStringConfig(L, GLOBAL_SERVER_SAVE_TIME, "globalServerSaveTime", "06:00"); loadStringConfig(L, DATA_DIRECTORY, "dataPackDirectory", "data-otservbr-global"); diff --git a/src/creatures/players/player.cpp b/src/creatures/players/player.cpp index 2aebb8179c6..0b5da33dc41 100644 --- a/src/creatures/players/player.cpp +++ b/src/creatures/players/player.cpp @@ -2112,8 +2112,8 @@ void Player::onThink(uint32_t interval) { triggerMomentum(); auto playerTile = getTile(); const bool vipStaysOnline = isVip() && g_configManager().getBoolean(VIP_STAY_ONLINE, __FUNCTION__); + idleTime += interval; if (playerTile && !playerTile->hasFlag(TILESTATE_NOLOGOUT) && !isAccessPlayer() && !isExerciseTraining() && !vipStaysOnline) { - idleTime += interval; const int32_t kickAfterMinutes = g_configManager().getNumber(KICK_AFTER_MINUTES, __FUNCTION__); if (idleTime > (kickAfterMinutes * 60000) + 60000) { removePlayer(true); diff --git a/src/game/game.cpp b/src/game/game.cpp index 1cca6b5f388..4a573d31f94 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -7590,7 +7590,7 @@ void Game::dieSafely(std::string errorMsg /* = "" */) { } void Game::shutdown() { - g_webhook().sendMessage("Server is shutting down", "Shutting down...", WEBHOOK_COLOR_OFFLINE); + g_webhook().sendMessage(":red_circle: Server is shutting down..."); g_logger().info("Shutting down..."); map.spawnsMonster.clear(); @@ -9182,6 +9182,11 @@ void Game::playerSetMonsterPodium(uint32_t playerId, uint32_t monsterRaceId, con return; } + if (player->isUIExhausted()) { + player->sendCancelMessage(RETURNVALUE_YOUAREEXHAUSTED); + return; + } + if (g_configManager().getBoolean(ONLY_INVITED_CAN_MOVE_HOUSE_ITEMS, __FUNCTION__) && !InternalGame::playerCanUseItemOnHouseTile(player, item)) { player->sendCancelMessage(RETURNVALUE_NOTPOSSIBLE); return; @@ -9195,8 +9200,8 @@ void Game::playerSetMonsterPodium(uint32_t playerId, uint32_t monsterRaceId, con const auto mType = g_monsters().getMonsterTypeByRaceId(static_cast(monsterRaceId), itemId == ITEM_PODIUM_OF_VIGOUR); if (!mType) { - player->sendCancelMessage(RETURNVALUE_CONTACTADMINISTRATOR); - g_logger().error("[{}] player {} is trying to add invalid monster to podium {}", __FUNCTION__, player->getName(), item->getName()); + player->sendCancelMessage(RETURNVALUE_NOTPOSSIBLE); + g_logger().debug("[{}] player {} is trying to add invalid monster to podium {}", __FUNCTION__, player->getName(), item->getName()); return; } @@ -9242,6 +9247,8 @@ void Game::playerSetMonsterPodium(uint32_t playerId, uint32_t monsterRaceId, con for (const auto &spectator : Spectators().find(pos, true)) { spectator->getPlayer()->sendUpdateTileItem(tile, pos, item); } + + player->updateUIExhausted(); } void Game::playerRotatePodium(uint32_t playerId, const Position &pos, uint8_t stackPos, const uint16_t itemId) { diff --git a/src/game/game_definitions.hpp b/src/game/game_definitions.hpp index 5abf8f4610a..17bc69dd514 100644 --- a/src/game/game_definitions.hpp +++ b/src/game/game_definitions.hpp @@ -104,10 +104,10 @@ enum HighscoreType_t : uint8_t { }; enum Webhook_Colors_t : uint32_t { - WEBHOOK_COLOR_ONLINE = 0x00FF00, - WEBHOOK_COLOR_OFFLINE = 0xFF0000, - WEBHOOK_COLOR_WARNING = 0xFFFF00, - WEBHOOK_COLOR_RAID = 0x0000FF + WEBHOOK_COLOR_GREEN = 0x00FF00, + WEBHOOK_COLOR_RED = 0xFF0000, + WEBHOOK_COLOR_YELLOW = 0xFFFF00, + WEBHOOK_COLOR_BLUE = 0x0000FF }; // Structs diff --git a/src/lua/creature/raids.cpp b/src/lua/creature/raids.cpp index aad2fd08350..f30155578b3 100644 --- a/src/lua/creature/raids.cpp +++ b/src/lua/creature/raids.cpp @@ -319,7 +319,7 @@ bool AnnounceEvent::configureRaidEvent(const pugi::xml_node &eventNode) { bool AnnounceEvent::executeEvent() { g_game().broadcastMessage(message, messageType); - g_webhook().sendMessage("Incoming raid!", message, WEBHOOK_COLOR_RAID); + g_webhook().sendMessage(fmt::format(":space_invader: {}", message)); return true; } diff --git a/src/lua/functions/core/game/lua_enums.cpp b/src/lua/functions/core/game/lua_enums.cpp index fd2f26699f4..6391cdffb2e 100644 --- a/src/lua/functions/core/game/lua_enums.cpp +++ b/src/lua/functions/core/game/lua_enums.cpp @@ -1239,10 +1239,10 @@ void LuaEnums::initForgeEnums(lua_State* L) { // Webhook default colors void LuaEnums::initWebhookEnums(lua_State* L) { - registerEnum(L, WEBHOOK_COLOR_ONLINE); - registerEnum(L, WEBHOOK_COLOR_OFFLINE); - registerEnum(L, WEBHOOK_COLOR_WARNING); - registerEnum(L, WEBHOOK_COLOR_RAID); + registerEnum(L, WEBHOOK_COLOR_GREEN); + registerEnum(L, WEBHOOK_COLOR_RED); + registerEnum(L, WEBHOOK_COLOR_YELLOW); + registerEnum(L, WEBHOOK_COLOR_BLUE); } void LuaEnums::initBosstiaryEnums(lua_State* L) { diff --git a/src/lua/functions/core/network/webhook_functions.cpp b/src/lua/functions/core/network/webhook_functions.cpp index 7c649ce4369..43e1935d903 100644 --- a/src/lua/functions/core/network/webhook_functions.cpp +++ b/src/lua/functions/core/network/webhook_functions.cpp @@ -13,13 +13,19 @@ #include "server/network/webhook/webhook.hpp" int WebhookFunctions::luaWebhookSendMessage(lua_State* L) { - // Webhook.sendMessage(title, message, color, url = "WEBHOOK_DISCORD_URL") + // Webhook.sendMessage(title, message, color, url = "WEBHOOK_DISCORD_URL") | + // Webhook.sendMessage(message, url = "WEBHOOK_DISCORD_URL") std::string title = getString(L, 1); std::string message = getString(L, 2); uint32_t color = getNumber(L, 3, 0); - std::string url = getString(L, 4); - - g_webhook().sendMessage(title, message, color, url); + std::string url = getString(L, -1); + if (url == title) { + g_webhook().sendMessage(title); + } else if (url == message) { + g_webhook().sendMessage(title, url); + } else { + g_webhook().sendMessage(title, message, color, url); + } lua_pushnil(L); return 1; diff --git a/src/server/network/webhook/webhook.cpp b/src/server/network/webhook/webhook.cpp index b761210ccdb..a157a3e0c7b 100644 --- a/src/server/network/webhook/webhook.cpp +++ b/src/server/network/webhook/webhook.cpp @@ -43,12 +43,12 @@ void Webhook::run() { ); } -void Webhook::sendMessage(const std::string payload, std::string url) { +void Webhook::sendPayload(const std::string &payload, std::string url) { std::scoped_lock lock { taskLock }; webhooks.push_back(std::make_shared(payload, url)); } -void Webhook::sendMessage(const std::string title, const std::string message, int color, std::string url) { +void Webhook::sendMessage(const std::string &title, const std::string &message, int color, std::string url, bool embed) { if (url.empty()) { url = g_configManager().getString(DISCORD_WEBHOOK_URL, __FUNCTION__); } @@ -57,7 +57,19 @@ void Webhook::sendMessage(const std::string title, const std::string message, in return; } - sendMessage(getPayload(title, message, color), url); + sendPayload(getPayload(title, message, color, embed), url); +} + +void Webhook::sendMessage(const std::string &message, std::string url) { + if (url.empty()) { + url = g_configManager().getString(DISCORD_WEBHOOK_URL, __FUNCTION__); + } + + if (url.empty() || message.empty()) { + return; + } + + sendPayload(getPayload("", message, -1, false), url); } int Webhook::sendRequest(const char* url, const char* payload, std::string* response_body) const { @@ -100,7 +112,7 @@ size_t Webhook::writeCallback(void* contents, size_t size, size_t nmemb, void* u return real_size; } -std::string Webhook::getPayload(const std::string title, const std::string message, int color) const { +std::string Webhook::getPayload(const std::string &title, const std::string &message, int color, bool embed) const { std::time_t now = getTimeNow(); std::string time_buf = formatDate(now); @@ -110,14 +122,22 @@ std::string Webhook::getPayload(const std::string title, const std::string messa << time_buf; std::stringstream payload; - payload << "{ \"embeds\": [{ "; - payload << "\"title\": \"" << title << "\", "; - payload << "\"description\": \"" << message << "\", "; - payload << "\"footer\": { \"text\": \"" << footer_text.str() << "\" }, "; - if (color >= 0) { - payload << "\"color\": " << color; + if (embed) { + payload << "{ \"embeds\": [{ "; + payload << "\"title\": \"" << title << "\", "; + if (!message.empty()) { + payload << "\"description\": \"" << message << "\", "; + } + if (g_configManager().getBoolean(DISCORD_SEND_FOOTER, __FUNCTION__)) { + payload << "\"footer\": { \"text\": \"" << footer_text.str() << "\" }, "; + } + if (color >= 0) { + payload << "\"color\": " << color; + } + payload << " }] }"; + } else { + payload << "{ \"content\": \"" << (!message.empty() ? message : title) << "\" }"; } - payload << " }] }"; return payload.str(); } diff --git a/src/server/network/webhook/webhook.hpp b/src/server/network/webhook/webhook.hpp index e41e63c186f..df414f24e1d 100644 --- a/src/server/network/webhook/webhook.hpp +++ b/src/server/network/webhook/webhook.hpp @@ -30,8 +30,9 @@ class Webhook { void run(); - void sendMessage(const std::string payload, std::string url); - void sendMessage(const std::string title, const std::string message, int color, std::string url = ""); + void sendPayload(const std::string &payload, std::string url); + void sendMessage(const std::string &title, const std::string &message, int color, std::string url = "", bool embed = true); + void sendMessage(const std::string &message, std::string url = ""); private: std::mutex taskLock; @@ -43,7 +44,7 @@ class Webhook { int sendRequest(const char* url, const char* payload, std::string* response_body) const; static size_t writeCallback(void* contents, size_t size, size_t nmemb, void* userp); - std::string getPayload(const std::string title, const std::string message, int color) const; + std::string getPayload(const std::string &title, const std::string &message, int color, bool embed = true) const; }; constexpr auto g_webhook = Webhook::getInstance;