command.register("remind", "Set a reminder", function(ctx) if #ctx.args < 2 then ctx:reply("Usage: /remind <seconds> <message>") return false end local seconds = tonumber(ctx.args[1]) if not seconds then ctx:reply("`4Error: ``Invalid time") return false end local message = table.concat(ctx.args, " ", 2) scheduler.schedule(seconds * 1000, function() local log = LogPacket.new() log.msg = "`2[Reminder] ``" .. message send.to_client(log) end) ctx:reply("`2Reminder set for {} seconds", seconds) return trueend)
local tracking = falselocal track_task_id = nilcommand.register("track", "Toggle position tracking", function(ctx) if tracking then scheduler.cancel(track_task_id) tracking = false ctx:reply("`2Position tracking stopped") else track_task_id = scheduler.schedule_periodic(1000, function() local player = world:get_local_player() if player then logger.info("Position: ({}, {})", player.position.x, player.position.y) end return true end) tracking = true ctx:reply("`2Position tracking started") end return trueend)
command.register("afk", "Auto-disconnect after time", function(ctx) if #ctx.args < 1 then ctx:reply("Usage: /afk <minutes>") return false end local minutes = tonumber(ctx.args[1]) if not minutes or minutes <= 0 then ctx:reply("`4Error: ``Invalid time") return false end local seconds = minutes * 60 scheduler.schedule(seconds * 1000, function() packet.send_text("action|quit", true) logger.info("Auto-disconnected after {} minutes", minutes) end) ctx:reply("`2Will disconnect in {} minutes", minutes) return trueend)
command.register("countdown", "Start a countdown", function(ctx) if #ctx.args < 1 then ctx:reply("Usage: /countdown <seconds>") return false end local seconds = tonumber(ctx.args[1]) if not seconds or seconds <= 0 then ctx:reply("`4Error: ``Invalid time") return false end local remaining = seconds local task_id = scheduler.schedule_periodic(1000, function() ctx:reply("`2{} seconds remaining", remaining) remaining = remaining - 1 if remaining < 0 then ctx:reply("`2Countdown finished!") return false end return true end) return trueend)
local tasks = {}local function schedule(key, delay, fn) tasks[key] = scheduler.schedule(delay, fn) return tasks[key]endlocal function schedule_periodic(key, interval, fn, initial_delay) if initial_delay then tasks[key] = scheduler.schedule_periodic(interval, fn, initial_delay) else tasks[key] = scheduler.schedule_periodic(interval, fn) end return tasks[key]endcommand.register("tasks", "Show pending tasks", function(ctx) local count = scheduler.pending_count() ctx:reply("`2Pending tasks: ``{}", count) for key, task_id in pairs(tasks) do if scheduler.is_pending(task_id) then ctx:reply("`2 - ``{} (ID: {})", key, task_id) end end return trueend)
local active_tasks = {}event.on("server:Disconnect", function(ctx) logger.info("Cancelling {} active tasks", #active_tasks) for _, task_id in ipairs(active_tasks) do scheduler.cancel(task_id) end active_tasks = {}end)event.on("server:Connect", function(ctx) local task_id = scheduler.schedule_periodic(5000, function() logger.info("Heartbeat") return true end) table.insert(active_tasks, task_id)end)