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 de7ae21
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 49 deletions.
71 changes: 62 additions & 9 deletions 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 All @@ -11,7 +11,7 @@ function ban.onSay(player, words, param)
return true
end

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

Expand All @@ -20,7 +20,7 @@ function ban.onSay(player, words, param)
return true
end

local accountId = Game.getPlayerAccountId(name)
local accountId = Game.getPlayerAccountId(targetName)
if accountId == 0 then
return true
end
Expand All @@ -35,19 +35,72 @@ function ban.onSay(player, words, param)
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)
if target then
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, string.format("%s has been banned for %d days.", target:getName(), banDuration))
target:remove()
local targetPlayer = Player(targetName)
if targetPlayer then
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, string.format("%s has been banned for %d days.", targetPlayer:getName(), banDuration))
targetPlayer:remove()
else
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, string.format("%s has been banned for %d days.", name, banDuration))
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, string.format("%s has been banned for %d days.", targetName, banDuration))
end
return true
end

ban:separator(" ")
ban: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 ipBanReason = 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 currentTime = os.time()
local expirationTime = currentTime + (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(ipBanReason), currentTime, 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()

local unban = TalkAction("/unban")

function unban.onSay(player, words, param)
Expand All @@ -72,7 +125,7 @@ function unban.onSay(player, words, param)
db.asyncQuery("DELETE FROM `account_bans` WHERE `account_id` = " .. db.escapeString(tostring(accountId)))
db.asyncQuery("DELETE FROM `ip_bans` WHERE `ip` = " .. db.escapeString(lastIp))

player:sendTextMessage(MESSAGE_EVENT_ADVANCE, param .. " has been unbanned.")
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, string.format("%s has been unbanned.", param))
return true
end

Expand Down
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 de7ae21

Please sign in to comment.