Running MySQL Query After 'x' Minutes

I have a function in my addon that bans users from specific actions. I’m trying to add MySQL support so that the bans can have effect across all of an owner’s servers, and I was wondering how I could run a query to delete a user from the banned table after ‘x’ number of minutes. I thought about using a timer but I’m pretty sure they won’t exist after server map changes or restarts.

Ban Command (sorry about the syntax, FP didn’t c&p correctly)



local Database, error = tmysql.initialize( RAS.MySQL.Host, RAS.MySQL.User, RAS.MySQL.Pass, RAS.MySQL.Database, RAS.MySQL.Port, nil, nil )

functionsTable["banchat"] = function( ply, args )
	if RAS.ValidSteamID( args[1] ) then
		if RAS.HasPerms( ply ) then
			if args[2] == "perm" || args[2] == nil then
                                Database:Connect()
				Database:Query( "INSERT INTO banned (steam_id_64) VALUES ('args[1]')" )
                                Database:Disconnect()
				RAS.ChatPrint( args[1].." has been banned from chatting permanently" )
				RAS.Log( "["..ply:SteamID().."]"..ply:Nick().." banned "..args[1].." from chatting permanently!" )
			elseif type( tonumber( args[1] ) ) == "number" && CheckNumberFunc( args[1] ) then
				local time = tonumber( args[2] ) * 60
                                Database:Connect()
				Database:Query( "INSERT INTO banned (steam_id_64) VALUES ('args[1]')" )
                                Database:Disconnect()
				RAS.ChatPrint( args[1].." has been banned from chatting for "..time.." minutes" )
				RAS.Log( "["..ply:SteamID().."]"..ply:Nick().." banned "..args[1].." from chatting for "..time.." minutes!" )
				timer.Simple( time, function()
                                        Database:Connect()
					Database:Query( "DELETE FROM banned WHERE steam_id_64 = 'args[1]'" )
                                        Database:Disconnect()
				end)
			end
		else
			RAS.ChatPrint( "You do not have permission to do this!", ply )
		end
	elseif RAS.ValidPly( args[1] ) then
		if RAS.HasPerms( ply ) then
			if args[2] == "perm" || args[2] == nil then
                                Database:Connect()
				Database:Query( "INSERT INTO banned (steam_id_64) VALUES ('RAS.GetSteamID( args[1] )')" )
                                Database:Disconnect()
				RAS.ChatPrint( args[1].." has been banned from chatting permanently" )
				RAS.Log( "["..ply:SteamID().."]"..ply:Nick().." banned "..args[1].." from chatting permanently!" )
			elseif type( tonumber( args[1] ) ) == "number" && CheckNumberFunc( args[1] ) then
				local time = tonumber( args[2] ) * 60
                                Database:Connect()
				Database:Query( "INSERT INTO banned (steam_id_64) VALUES ('RAS.GetSteamID( args[1] )')" )
                                Database:Disconnect()
				RAS.ChatPrint( args[1].." has been banned from chatting for "..time.." minutes" )
				RAS.Log( "["..ply:SteamID().."]"..ply:Nick().." banned "..args[1].." from chatting for "..time.." minutes!" )
				timer.Simple( time, function()
                                        Database:Connect()
					Database:Query( "DELETE FROM banned WHERE steam_id_64 = 'RAS.GetSteamID( args[1] )'" )
                                        Database:Disconnect()
				end)
			end
		else
			RAS.ChatPrint( "You do not have permission to do this!", ply )
		end
	else
		RAS.ChatPrint( "Invalid Target!", ply )
	end
end


I think I mentioned this before. The better way to do this rather then delete the record is:

When adding the ban do:



-- if the ban is say 1 day
local expire_time = os.time() + 86400 -- the amount of time in seconds the ban is for.
"INSERT INTO banned (steam_id_64, expire_time) VALUES ( " .. steam_id_64 .. ", " .. expire_time )

Expire time should be a BIGINT in MySQL. That way, when checking if they are banned, you can just do…


"SELECT * FROM banned WHERE steam_id_64 = " .. steam_id_64 .. " AND expire_time > " .. os.time()

So it’ll only find their ban records where it’s their steam id and the ban expire time is greate then the current time ( meaning it hasn’t expired ).

This is very rough.

Best not delete data from the table though as then you loose your ban record.