Skip to content

Commit

Permalink
improvement: enable custom duration and simplify expiration calculati…
Browse files Browse the repository at this point in the history
…on (#3083)

Allows setting a custom ban duration and simplifies the expiration time
calculation.
  • Loading branch information
omarcopires authored Nov 13, 2024
1 parent 6239786 commit 73c7457
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions data/scripts/talkactions/gm/ban.lua
Original file line number Diff line number Diff line change
@@ -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 <player name>, <duration in days>, <reason>")
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(" ")
Expand Down

0 comments on commit 73c7457

Please sign in to comment.