mirror of
https://github.com/minetest-mods/irc.git
synced 2024-12-26 17:00:17 +01:00
Add success and message return values to bot commands
This commit is contained in:
parent
8e6a9a287f
commit
5655f75946
37
API.md
37
API.md
@ -25,25 +25,26 @@ irc:register_bot_command(name, cmdDef)
|
||||
When an user sends a private message to the bot with the command name, the
|
||||
command's function is called.
|
||||
Here's the format of a command definition (<cmdDef>):
|
||||
cmdDef = {
|
||||
params = "<param1> ...", -- A description of the command's parameters
|
||||
description = "My command", -- A description of what the command does. (one-liner)
|
||||
func = function(user, param)
|
||||
-- This function gets called when the command is invoked.
|
||||
-- <user> is a user table for the user that ran the command.
|
||||
-- (See the LuaIRC documentation for details.)
|
||||
-- It contains fields such as 'nick' and 'ident'
|
||||
-- <param> is a string of parameters to the command (may be "")
|
||||
end,
|
||||
};
|
||||
cmdDef = {
|
||||
params = "<param1> ...", -- A description of the command's parameters
|
||||
description = "My command", -- A description of what the command does. (one-liner)
|
||||
func = function(user, args)
|
||||
-- This function gets called when the command is invoked.
|
||||
-- <user> is a user table for the user that ran the command.
|
||||
-- (See the LuaIRC documentation for details.)
|
||||
-- It contains fields such as 'nick' and 'ident'
|
||||
-- <args> is a string of arguments to the command (may be "")
|
||||
-- This function should return boolean success and a message.
|
||||
end,
|
||||
};
|
||||
Example:
|
||||
irc:register_bot_command("hello", {
|
||||
params = "",
|
||||
description = "Greet user",
|
||||
func = function(user, param)
|
||||
irc:say(user.nick, "Hello!")
|
||||
end,
|
||||
});
|
||||
irc:register_bot_command("hello", {
|
||||
params = "",
|
||||
description = "Greet user",
|
||||
func = function(user, param)
|
||||
return true, "Hello!"
|
||||
end,
|
||||
});
|
||||
|
||||
irc.joined_players[name]
|
||||
This table holds the players who are currently on the channel (may be less
|
||||
|
47
botcmds.lua
47
botcmds.lua
@ -52,7 +52,10 @@ function irc:bot_command(msg, text)
|
||||
return
|
||||
end
|
||||
|
||||
self.bot_commands[cmd].func(msg.user, args)
|
||||
local success, message = self.bot_commands[cmd].func(msg.user, args)
|
||||
if message then
|
||||
self:reply(message)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@ -71,21 +74,19 @@ irc:register_bot_command("help", {
|
||||
description = "Get help about a command",
|
||||
func = function(user, args)
|
||||
if args == "" then
|
||||
irc:reply("No command name specified. Use 'list' for a list of commands")
|
||||
return
|
||||
return false, "No command name specified. Use 'list' for a list of commands."
|
||||
end
|
||||
|
||||
local cmd = irc.bot_commands[args]
|
||||
if not cmd then
|
||||
irc:reply("Unknown command '"..cmdname.."'.")
|
||||
return
|
||||
return false, "Unknown command '"..cmdname.."'."
|
||||
end
|
||||
|
||||
irc:reply(("Usage: %c%s %s -- %s"):format(
|
||||
return true, ("Usage: %c%s %s -- %s"):format(
|
||||
irc.config.command_prefix,
|
||||
args,
|
||||
cmd.params or "<no parameters>",
|
||||
cmd.description or "<no description>"))
|
||||
cmd.description or "<no description>")
|
||||
end
|
||||
})
|
||||
|
||||
@ -98,8 +99,8 @@ irc:register_bot_command("list", {
|
||||
for name, cmd in pairs(irc.bot_commands) do
|
||||
cmdlist = cmdlist..name..", "
|
||||
end
|
||||
irc:reply(cmdlist.." -- Use 'help <command name>' to get"
|
||||
.." help about a specific command.")
|
||||
return true, cmdlist.." -- Use 'help <command name>' to get"
|
||||
.." help about a specific command."
|
||||
end
|
||||
})
|
||||
|
||||
@ -109,17 +110,15 @@ irc:register_bot_command("whereis", {
|
||||
description = "Tell the location of <player>",
|
||||
func = function(user, args)
|
||||
if args == "" then
|
||||
irc:bot_help(user, "whereis")
|
||||
return
|
||||
return false, "Player name required."
|
||||
end
|
||||
local player = minetest.env:get_player_by_name(args)
|
||||
if player then
|
||||
local fmt = "Player %s is at (%.2f,%.2f,%.2f)"
|
||||
local pos = player:getpos()
|
||||
irc:reply(fmt:format(args, pos.x, pos.y, pos.z))
|
||||
return
|
||||
local player = minetest.get_player_by_name(args)
|
||||
if not player then
|
||||
return false, "There is no player named '"..args.."'"
|
||||
end
|
||||
irc:reply("There is no player named '"..args.."'")
|
||||
local fmt = "Player %s is at (%.2f,%.2f,%.2f)"
|
||||
local pos = player:getpos()
|
||||
return true, fmt:format(args, pos.x, pos.y, pos.z)
|
||||
end
|
||||
})
|
||||
|
||||
@ -131,11 +130,11 @@ irc:register_bot_command("uptime", {
|
||||
local cur_time = os.time()
|
||||
local diff = os.difftime(cur_time, starttime)
|
||||
local fmt = "Server has been running for %d:%02d:%02d"
|
||||
irc:reply(fmt:format(
|
||||
return true, fmt:format(
|
||||
math.floor(diff / 60 / 60),
|
||||
math.mod(math.floor(diff / 60), 60),
|
||||
math.mod(math.floor(diff), 60)
|
||||
))
|
||||
math.floor(diff / 60) % 60,
|
||||
math.floor(diff) % 60
|
||||
)
|
||||
end
|
||||
})
|
||||
|
||||
@ -148,8 +147,8 @@ irc:register_bot_command("players", {
|
||||
for _, player in pairs(players) do
|
||||
table.insert(names, player:get_player_name())
|
||||
end
|
||||
irc:reply("Connected players: "
|
||||
..table.concat(names, ", "))
|
||||
return true, "Connected players: "
|
||||
..table.concat(names, ", ")
|
||||
end
|
||||
})
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user