From 73c7457731f82e45022324ac1e379edf40a23473 Mon Sep 17 00:00:00 2001 From: Marco Date: Wed, 13 Nov 2024 00:30:11 -0300 Subject: [PATCH] improvement: enable custom duration and simplify expiration calculation (#3083) Allows setting a custom ban duration and simplifies the expiration time calculation. --- data/scripts/talkactions/gm/ban.lua | 40 ++++++++++++++--------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/data/scripts/talkactions/gm/ban.lua b/data/scripts/talkactions/gm/ban.lua index 0ab07040333..2796444a70b 100644 --- a/data/scripts/talkactions/gm/ban.lua +++ b/data/scripts/talkactions/gm/ban.lua @@ -1,48 +1,48 @@ -local banDays = 7 - local ban = TalkAction("/ban") function ban.onSay(player, words, param) -- create log logCommand(player, words, param) - if param == "" then - player:sendCancelMessage("Command param required.") + local params = param:split(",") + if #params < 3 then + player:sendCancelMessage("Command requires 3 parameters: /ban , , ") return true end - local name = param - local reason = "" + local playerName = params[1]:trim() + local banDuration = tonumber(params[2]:trim()) + local banReason = params[3]:trim() - local separatorPos = param:find(",") - if separatorPos then - name = param:sub(0, separatorPos - 1) - reason = string.trim(param:sub(separatorPos + 1)) + if not banDuration or banDuration <= 0 then + player:sendCancelMessage("Ban duration must be a positive number.") + return true end - local accountId = getAccountNumberByPlayerName(name) + local accountId = getAccountNumberByPlayerName(playerName) if accountId == 0 then return true end local resultId = db.storeQuery("SELECT 1 FROM `account_bans` WHERE `account_id` = " .. accountId) - if resultId ~= false then - Result.free(resultId) + if resultId then + result.free(resultId) return true end - local timeNow = os.time() - db.query("INSERT INTO `account_bans` (`account_id`, `reason`, `banned_at`, `expires_at`, `banned_by`) VALUES (" .. accountId .. ", " .. db.escapeString(reason) .. ", " .. timeNow .. ", " .. timeNow + (banDays * 86400) .. ", " .. player:getGuid() .. ")") + local currentTime = os.time() + local expirationTime = currentTime + (banDuration * 24 * 60 * 60) + db.query(string.format("INSERT INTO `account_bans` (`account_id`, `reason`, `banned_at`, `expires_at`, `banned_by`) VALUES (%d, %s, %d, %d, %d)", accountId, db.escapeString(banReason), currentTime, expirationTime, player:getGuid())) - local target = Player(name) + local target = Player(playerName) if target then - local text = target:getName() .. " has been banned" - player:sendTextMessage(MESSAGE_ADMINISTRATOR, text) - Webhook.sendMessage("Player Banned", text .. " reason: " .. reason .. ". (by: " .. player:getName() .. ")", WEBHOOK_COLOR_YELLOW, announcementChannels["serverAnnouncements"]) + player:sendTextMessage(MESSAGE_ADMINISTRATOR, string.format("%s has been banned for %d days.", target:getName(), banDuration)) target:remove() + Webhook.sendMessage("Player Banned", string.format("%s has been banned for %d days. Reason: %s (by: %s)", target:getName(), banDuration, banReason, player:getName()), WEBHOOK_COLOR_YELLOW, announcementChannels["serverAnnouncements"]) else - player:sendTextMessage(MESSAGE_ADMINISTRATOR, name .. " has been banned.") + player:sendTextMessage(MESSAGE_ADMINISTRATOR, string.format("%s has been banned for %d days.", playerName, banDuration)) end + return true end ban:separator(" ")