1 -- Minetest: builtin/chatcommands.lua
7 minetest.chatcommands = {}
8 function minetest.register_chatcommand(cmd, def)
10 def.params = def.params or ""
11 def.description = def.description or ""
12 def.privs = def.privs or {}
13 minetest.chatcommands[cmd] = def
16 -- Register the help command
17 minetest.register_chatcommand("help", {
19 params = "(nothing)/all/privs/<cmd>",
20 description = "Get help for commands or list privileges",
21 func = function(name, param)
22 local format_help_line = function(cmd, def)
24 if def.params and def.params ~= "" then msg = msg .. " " .. def.params end
25 if def.description and def.description ~= "" then msg = msg .. ": " .. def.description end
31 for cmd, def in pairs(minetest.chatcommands) do
32 if minetest.check_player_privs(name, def.privs) then
33 table.insert(cmds, cmd)
36 minetest.chat_send_player(name, "Available commands: "..table.concat(cmds, " "))
37 minetest.chat_send_player(name, "Use '/help <cmd>' to get more information, or '/help all' to list everything.")
38 elseif param == "all" then
39 minetest.chat_send_player(name, "Available commands:")
40 for cmd, def in pairs(minetest.chatcommands) do
41 if minetest.check_player_privs(name, def.privs) then
42 minetest.chat_send_player(name, format_help_line(cmd, def))
45 elseif param == "privs" then
46 minetest.chat_send_player(name, "Available privileges:")
47 for priv, def in pairs(minetest.registered_privileges) do
48 minetest.chat_send_player(name, priv..": "..def.description)
52 def = minetest.chatcommands[cmd]
54 minetest.chat_send_player(name, "Command not available: "..cmd)
56 minetest.chat_send_player(name, format_help_line(cmd, def))
62 -- Register C++ commands without functions
63 minetest.register_chatcommand("me", {params = nil, description = "chat action (eg. /me orders a pizza)"})
64 minetest.register_chatcommand("status", {description = "print server status line"})
65 minetest.register_chatcommand("shutdown", {params = "", description = "shutdown server", privs = {server=true}})
66 minetest.register_chatcommand("setting", {params = "<name> = <value>", description = "set line in configuration file", privs = {server=true}})
67 minetest.register_chatcommand("clearobjects", {params = "", description = "clear all objects in world", privs = {server=true}})
68 minetest.register_chatcommand("time", {params = "<0...24000>", description = "set time of day", privs = {settime=true}})
69 minetest.register_chatcommand("ban", {params = "<name>", description = "ban IP of player", privs = {ban=true}})
70 minetest.register_chatcommand("unban", {params = "<name/ip>", description = "remove IP ban", privs = {ban=true}})
72 -- Register some other commands
73 minetest.register_chatcommand("privs", {
75 description = "print out privileges of player",
76 func = function(name, param)
80 if not minetest.check_player_privs(name, {privs=true}) then
81 minetest.chat_send_player(name, "Privileges of "..param.." are hidden from you.")
84 minetest.chat_send_player(name, "Privileges of "..param..": "..minetest.privs_to_string(minetest.get_player_privs(param), ' '))
87 minetest.register_chatcommand("grant", {
88 params = "<name> <privilege>|all",
89 description = "Give privilege to player",
91 func = function(name, param)
92 local grantname, grantprivstr = string.match(param, "([^ ]+) (.+)")
93 if not grantname or not grantprivstr then
94 minetest.chat_send_player(name, "Invalid parameters (see /help grant)")
97 local grantprivs = minetest.string_to_privs(grantprivstr)
98 if grantprivstr == "all" then
99 grantprivs = minetest.registered_privileges
101 local privs = minetest.get_player_privs(grantname)
102 for priv, _ in pairs(grantprivs) do
105 minetest.set_player_privs(grantname, privs)
106 minetest.chat_send_player(name, "Privileges of "..grantname..": "..minetest.privs_to_string(minetest.get_player_privs(grantname), ' '))
107 if grantname ~= name then
108 minetest.chat_send_player(grantname, name.." granted you privileges: "..minetest.privs_to_string(grantprivs, ' '))
112 minetest.register_chatcommand("revoke", {
113 params = "<name> <privilege>|all",
114 description = "Remove privilege from player",
115 privs = {privs=true},
116 func = function(name, param)
117 local revokename, revokeprivstr = string.match(param, "([^ ]+) (.+)")
118 if not revokename or not revokeprivstr then
119 minetest.chat_send_player(name, "Invalid parameters (see /help revoke)")
122 local revokeprivs = minetest.string_to_privs(revokeprivstr)
123 local privs = minetest.get_player_privs(revokename)
124 if revokeprivstr == "all" then
127 for priv, _ in pairs(revokeprivs) do
131 minetest.set_player_privs(revokename, privs)
132 minetest.chat_send_player(name, "Privileges of "..revokename..": "..minetest.privs_to_string(minetest.get_player_privs(revokename), ' '))
133 if revokename ~= name then
134 minetest.chat_send_player(revokename, name.." revoked privileges from you: "..minetest.privs_to_string(revokeprivs, ' '))
138 minetest.register_chatcommand("setpassword", {
139 params = "<name> <password>",
140 description = "set given password",
141 privs = {password=true},
142 func = function(name, param)
144 minetest.chat_send_player(name, "Password field required")
147 minetest.set_player_password(name, param)
148 minetest.chat_send_player(name, "Password set")
151 minetest.register_chatcommand("clearpassword", {
153 description = "set empty password",
154 privs = {password=true},
155 func = function(name, param)
156 minetest.set_player_password(name, '')
157 minetest.chat_send_player(name, "Password cleared")
161 minetest.register_chatcommand("auth_reload", {
163 description = "reload authentication data",
164 privs = {server=true},
165 func = function(name, param)
166 local done = minetest.auth_reload()
168 minetest.chat_send_player(name, "Done.")
170 minetest.chat_send_player(name, "Failed.")
175 minetest.register_chatcommand("teleport", {
176 params = "<X>,<Y>,<Z> | <to_name> | <name> <X>,<Y>,<Z> | <name> <to_name>",
177 description = "teleport to given position",
178 privs = {teleport=true},
179 func = function(name, param)
180 -- Returns (pos, true) if found, otherwise (pos, false)
181 local function find_free_position_near(pos)
188 for _, d in ipairs(tries) do
189 local p = {x = pos.x+d.x, y = pos.y+d.y, z = pos.z+d.z}
190 local n = minetest.env:get_node(p)
191 if not minetest.registered_nodes[n.name].walkable then
198 local teleportee = nil
200 p.x, p.y, p.z = string.match(param, "^([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+)$")
201 teleportee = minetest.env:get_player_by_name(name)
202 if teleportee and p.x and p.y and p.z then
203 minetest.chat_send_player(name, "Teleporting to ("..p.x..", "..p.y..", "..p.z..")")
208 local teleportee = nil
210 local target_name = nil
211 target_name = string.match(param, "^([^ ]+)$")
212 teleportee = minetest.env:get_player_by_name(name)
214 local target = minetest.env:get_player_by_name(target_name)
219 if teleportee and p then
220 p = find_free_position_near(p)
221 minetest.chat_send_player(name, "Teleporting to "..target_name.." at ("..p.x..", "..p.y..", "..p.z..")")
226 if minetest.check_player_privs(name, {bring=true}) then
227 local teleportee = nil
229 local teleportee_name = nil
230 teleportee_name, p.x, p.y, p.z = string.match(param, "^([^ ]+) +([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+)$")
231 if teleportee_name then
232 teleportee = minetest.env:get_player_by_name(teleportee_name)
234 if teleportee and p.x and p.y and p.z then
235 minetest.chat_send_player(name, "Teleporting "..teleportee_name.." to ("..p.x..", "..p.y..", "..p.z..")")
240 local teleportee = nil
242 local teleportee_name = nil
243 local target_name = nil
244 teleportee_name, target_name = string.match(param, "^([^ ]+) +([^ ]+)$")
245 if teleportee_name then
246 teleportee = minetest.env:get_player_by_name(teleportee_name)
249 local target = minetest.env:get_player_by_name(target_name)
254 if teleportee and p then
255 p = find_free_position_near(p)
256 minetest.chat_send_player(name, "Teleporting "..teleportee_name.." to "..target_name.." at ("..p.x..", "..p.y..", "..p.z..")")
262 minetest.chat_send_player(name, "Invalid parameters (\""..param.."\") or player not found (see /help teleport)")
268 -- Builtin chat handler
271 minetest.register_on_chat_message(function(name, message)
272 local cmd, param = string.match(message, "/([^ ]+) *(.*)")
276 local cmd_def = minetest.chatcommands[cmd]
278 if not cmd_def.func then
279 -- This is a C++ command
282 local has_privs, missing_privs = minetest.check_player_privs(name, cmd_def.privs)
284 cmd_def.func(name, param)
286 minetest.chat_send_player(name, "You don't have permission to run this command (missing privileges: "..table.concat(missing_privs, ", ")..")")
288 return true -- handled chat message