Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improvement: enable custom duration and simplify expiration calculation #3083

Merged
merged 3 commits into from
Nov 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading