Skip to content

Commit

Permalink
refactor: ipban to use string formatting in messages
Browse files Browse the repository at this point in the history
  • Loading branch information
omarcopires committed Nov 11, 2024
1 parent 5a6a851 commit 4b2567c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 41 deletions.
55 changes: 54 additions & 1 deletion data/scripts/talkactions/ban.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local ban = TalkAction("/ban")

function ban.onSay(player, words, param)
if not player:getGroup():getAccess() or player:getAccountType() < ACCOUNT_TYPE_GAMEMASTER then
if not player:getGroup():getAccess() then
return true
end

Expand Down Expand Up @@ -78,3 +78,56 @@ end

unban:separator(" ")
unban:register()

local ipban = TalkAction("/ipban")

function ipban.onSay(player, words, param)
if not player:getGroup():getAccess() then
return true
end

local params = param:split(",")
if #params < 3 then
player:sendCancelMessage("Command requires 3 parameters: /ipban <player name>, <duration in days>, <reason>")
return true
end

local targetName = params[1]:trim()
local ipBanDuration = tonumber(params[2]:trim())
local banReason = params[3]:trim()

if not ipBanDuration or ipBanDuration <= 0 then
player:sendCancelMessage("Ban duration must be a positive number.")
return true
end

local resultId = db.storeQuery("SELECT `name`, `lastip` FROM `players` WHERE `name` = " .. db.escapeString(targetName))
if not resultId then
return true
end

local targetIp = result.getString(resultId, "lastip")
result.free(resultId)

if targetIp == "0" then
player:sendTextMessage(MESSAGE_STATUS_WARNING, string.format("Invalid IP for player %s.", targetName))
return true
end

local checkBanQuery = db.storeQuery("SELECT 1 FROM `ip_bans` WHERE `ip` = " .. db.escapeString(targetIp))
if checkBanQuery then
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, string.format("%s is already IP banned.", targetName))
result.free(checkBanQuery)
return true
end

local timeNow = os.time()
local expirationTime = timeNow + (ipBanDuration * 24 * 60 * 60)
db.query(string.format("INSERT INTO `ip_bans` (`ip`, `reason`, `banned_at`, `expires_at`, `banned_by`) VALUES (%s, %s, %d, %d, %d)", db.escapeString(targetIp), db.escapeString(banReason), timeNow, expirationTime, player:getGuid()))

player:sendTextMessage(MESSAGE_EVENT_ADVANCE, string.format("%s has been IP banned for %d days.", targetName, ipBanDuration))
return true
end

ipban:separator(" ")
ipban:register()
39 changes: 0 additions & 39 deletions data/talkactions/scripts/ip_ban.lua

This file was deleted.

1 change: 0 additions & 1 deletion data/talkactions/talkactions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<talkactions>
<!-- commands -->
<talkaction words="/attr" separator=" " script="attributes.lua" />
<talkaction words="/ipban" separator=" " script="ip_ban.lua" />
<talkaction words="/up" script="up.lua" />
<talkaction words="/down" script="down.lua" />
<talkaction words="/c" separator=" " script="teleport_creature_here.lua" />
Expand Down

0 comments on commit 4b2567c

Please sign in to comment.