Banning

I’ve been working on a big project and I seem to hit a wall. I made a simple ban command and it work’s just as intended except when you turn the server off then on it erases the ban. I have exec banned_user.cfg and exec banned_ip.cfg in my server.cfg, so this leads me to wonder if their is just a simple thing i’m missing. Any help will be greatly appreciated. Thanks guys!

BanCommand:
[LUA]function kickban(ply, cmd, args)
local id = findPlayer(args[1])
local time = tonumber(args[2])
local reason = table.concat( args, " “, 3 ) or “Banned”
if ply:EntIndex() == 0 then
name = “Console”
else
name = ply:Nick()
end
if ply:EntIndex() == 0 or ply:IsAdmin() then
if time > 0 and time < 1441 then
for _,v in pairs(player.GetAll()) do
v:ChatPrint(name …” has banned “…id:Nick()…” ("…time…") for "…reason)
end
game.ConsoleCommand(“banid " … time …” “… id:SteamID() …”
“)
game.ConsoleCommand(“kickid " … id:SteamID() …” “… reason …”
“)
game.ConsoleCommand( “writeid
" )
elseif time == 0 and ply:IsSuperAdmin() then
for _,v in pairs(player.GetAll()) do
v:ChatPrint(name …” has banned “…id:Nick()…” (”…time…”) for "…reason)
end
game.ConsoleCommand("banid 0 “… id:SteamID() …”
")
game.ConsoleCommand(“kickid " … id:SteamID() …” “… reason …”
")
game.ConsoleCommand( "writeid
" )
end
end
end
concommand.Add(“bantest”, kickban)[/LUA]

Why don’t you just use ply:Ban() and then ply:Kick() rather than running game.concommands?

Just personal preference really

try using ply:Ban, in my experience it automatically writes to banned_user.cfg

Simpler way of doing it:
[lua]
function MessageAllPlayers(str)
for k,v in ipairs(player.GetAll()) do
v:ChatPrint(str)
end
end

function kickban(ply, cmd, args)
if (!ply:EntIndex() == 0) or !ply:IsAdmin() then return end

local time = tonumber(args[2])
if (time == 0) and (!ply:IsSuperAdmin()) then 
	ply:ChatPrint("You don't have the specified rank to ban for the time of "..time..".")
	return
end

local name = "Console"
if ply:EntIndex() != 0 then
	name = ply:Nick()
end

local id = findPlayer(args[1])
local reason = "Banned"
if args[3] then
	reason = table.concat(args, " ", 3)
end

MessageAllPlayers(name.." has banned "..id:Nick().." ("..time..") for "..reason)
id:Ban(time, reason)
id:Kick("")

end
concommand.Add(“bantest”, kickban)
[/lua]

Go get some anger management classes RetTurtl3.

Don’t take the dumb ratings to heart. Most cases the giver deserves the label

I don’t, I know how people are on here. I just know that RetTurl3 likes to spam rate dumb.

I rewrote it with Ban() and Kick() instead of the game.ConsoleCommands and when I typed bantest loki 0 Bye restarted the server it worked! Then I decided to test it some more and I found a little error when I type bantest loki 10 Bye and restarted the server it didn’t save so I tried different amount of time still didn’t save. I do want to thank you guys for telling me to use Ban() and Kick() though.

server.cfg:
[LUA]exec banned_user.cfg
exec banned_ip.cfg[/LUA]

NewCode:
[LUA]function kickban(ply, cmd, args)
local id = findPlayer(args[1])
local time = tonumber(args[2])
local reason = table.concat( args, " “, 3 ) or “Banned”
if ply:EntIndex() == 0 then
name = “Console”
else
name = ply:Nick()
end
if time == 0 and !ply:IsOwner() then return end
if ply:EntIndex() == 0 or ply:IsOwner() or ply:IsLSAdmin() or ply:IsLAdmin() then
lokiall(name …” has banned “…id:Nick()…” ("…time…") for “…reason) // prints to all player’s chat.
print(name …” has banned “…id:Nick()…” ("…time…") for "…reason)
id:Ban(time, reason)
id:Kick(reason)
end
end
concommand.Add(“bantest”, kickban)[/LUA]

Did you create your own admin mod?

You could use glon to encode bans.

I did make my own admin mod, And would you say I would make it use glon to encode and decode a .txt with the ban data in it?

Yes, use file.Write to encode it and file.Read to decode it.

Okay i’ll try, I’ve never used glon before but here’s my quick guess I would add file.write(bans.txt, glon.encode(baninfohere)) to my ban command.

Yep pretty much like that. Read this http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index1fe0.html as well.

Don’t use glon, I don’t even think GMod comes with glon support anymore. Use JSON, it’s the modern equivalent.

Oh I see so it would be like this:
[LUA]function kickban(ply, cmd, args)
local id = findPlayer(args[1])
local time = tonumber(args[2])
local reason = table.concat( args, " “, 3 ) or “Banned”
if ply:EntIndex() == 0 then
name = “Console”
else
name = ply:Nick()
end
if time == 0 and !ply:IsOwner() then return end
if ply:EntIndex() == 0 or ply:IsOwner() or ply:IsLSAdmin() or ply:IsLAdmin() then
lokiall(name …” has banned “…id:Nick()…” ("…time…") for “…reason)
print(name …” has banned “…id:Nick()…” ("…time…") for "…reason)
local baninfo = {}
baninfo[“name”] = id:Nick()
baninfo[“time”] = time
baninfo[“reason”] = reason
banning = glon.encode(baninfo)
file.Write(“cfg/banned_user.cfg”, banning)
id:Ban(time, reason)
id:Kick(reason)
end
end
concommand.Add(“bantest”, kickban)[/LUA]

Come to think about JSON would be best.

So i’m going on a limb here and saying this is what ti would be with Json:
[LUA]function kickban(ply, cmd, args)
local id = findPlayer(args[1])
local time = tonumber(args[2])
local reason = table.concat( args, " “, 3 ) or “Banned”
if ply:EntIndex() == 0 then
name = “Console”
else
name = ply:Nick()
end
if ply:EntIndex() == 0 or ply:IsOwner() or ply:IsLSAdmin() or ply:IsLAdmin() then
if time > 1440 or time == 0 and !ply:IsOwner() or not ply:EntIndex() == 0 then return end
lokiall(Lokitag … name …” has banned “…id:Nick()…” ("…time…") for “…reason)
print(Lokitag … name …” has banned “…id:Nick()…” ("…time…") for "…reason)
local baninfo = {}
baninfo[“name”] = id:Nick()
baninfo[“time”] = time
baninfo[“reason”] = reason
banning = Json.Encode(baninfo)
file.Write(“cfg/banned_user.cfg”, banning)
id:Ban(time, reason)
id:Kick(reason)
end
end
concommand.Add(“bantest”, kickban)[/LUA]

or is Json something else?

I made my own admin mod so if you see IsOwner() and Lokitag their all defined and working fine.
After a while I ended up with this (thanks to all you):
[LUA]function kickban(ply, cmd, args)
require(“json”)
local id = findPlayer(args[1])
local time = tonumber(args[2])
local reason = table.concat( args, " “, 3 ) or “Banned”
if ply:EntIndex() == 0 then
name = “Console”
else
name = ply:Nick()
end
if ply:EntIndex() == 0 or ply:IsOwner() or ply:IsLSAdmin() or ply:IsLAdmin() then
if time == 0 and !ply:IsOwner() then return end
lokiall(Lokitag … name …” has banned “…id:Nick()…” ("…time…") for “…reason)
print(Lokitag … name …” has banned “…id:Nick()…” ("…time…") for "…reason)
local baninfo = {}
baninfo[“name”] = id:Nick()
baninfo[“time”] = time
baninfo[“reason”] = reason
banning = Json.Encode(baninfo)
file.Write(“server/bans.txt”, banning)
id:Ban(time, reason)
id:Kick(reason)
end
end
concommand.Add(“bantest”, kickban)[/LUA]
It write’s to the location I set, but I still don’t understand how can I use this information to make the temp bans save. I’m probably over thinking it but if someone could just lead me into the right direction. Thank you.

I’m sorry for bringing up this topic again, but I still don’t understand this. I now have it write to a file so I can see the bans, but I still need some help on how to make this save bans over a server restart. At the time of making this my guess was to use os.time to set a time for them to be unbanned. Thank you in advance for any help. PS: I don’t have much experience with Json.
Here’s what I have now:
[LUA]
admin = {}
require(“json”)
function admin:addban(name, time, psteam, reason)
local tab = {
[“name”] = name,
[“time”] = time,
[“steamid”] = psteam,
[“reason”] = reason
}
local info = Json.Encode(tab)
file.Write(“bans.txt”, info)
end
function ban(ply, cmd, args)
local target = findPlayer(args[1])
local time = tonumber(args[2])
local reason = table.concat( args, " “, 3 )
if not ply:IsOwner() and time < 1 or time > 1440 then return end
length = (time * 60) + (os.time())
loki:addban(ply:Nick(), length, target:SteamID(), reason)
chatall(ply:Nick()…” has banned “…target:Nick()…” ("…time…") for ("…reason…")")
print(ply:Nick()…" has banned “…target:Nick()…” “…time…” minute(s) for ("…reason…")")
target:Ban(time, “”)
game.ConsoleCommand("kickid “…target:SteamID()…” “…reason…”
")
end
concommand.Add(“ban4”, ban)
[/LUA]