forked from minetest-mods/irc
Add in-channel commands
This commit is contained in:
parent
c3ddf1df70
commit
d81f80155f
|
@ -1,7 +1,39 @@
|
|||
|
||||
mt_irc.bot_commands = {}
|
||||
|
||||
function mt_irc:check_botcmd(user, target, message)
|
||||
local prefix = mt_irc.config.command_prefix
|
||||
local nick = mt_irc.conn.nick
|
||||
|
||||
-- First check for a nick prefix
|
||||
if message:sub(1, #nick + 2) == nick..": " or
|
||||
message:sub(1, #nick + 2) == nick..", " then
|
||||
self:bot_command(user, message:sub(#nick + 3))
|
||||
return true
|
||||
-- Then check for the configured prefix
|
||||
elseif prefix and message:sub(1, #prefix) == prefix then
|
||||
self:bot_command(user, message:sub(#prefix + 1))
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
|
||||
function mt_irc:bot_command(user, message)
|
||||
if message:sub(1, 1) == "@" then
|
||||
local found, _, player_to, message = message:find("^.([^%s]+)%s(.+)$")
|
||||
if not mt_irc.joined_players[player_to] then
|
||||
mt_irc:reply("User '"..player_to.."' has parted.")
|
||||
return
|
||||
elseif not minetest.get_player_by_name(player_to) then
|
||||
mt_irc:reply("User '"..player_to.."' is not in the game.")
|
||||
return
|
||||
end
|
||||
minetest.chat_send_player(player_to,
|
||||
"PM from "..user.nick.."@IRC: "..message, false)
|
||||
mt_irc:reply("Message sent!")
|
||||
return
|
||||
end
|
||||
local pos = message:find(" ", 1, true)
|
||||
local cmd, args
|
||||
if pos then
|
||||
|
@ -13,7 +45,7 @@ function mt_irc:bot_command(user, message)
|
|||
end
|
||||
|
||||
if not self.bot_commands[cmd] then
|
||||
self:say(user.nick, "Unknown command '"..cmd.."'. Try `!help'."
|
||||
self:reply("Unknown command '"..cmd.."'. Try 'list'."
|
||||
.." Or use @playername <message> to send a private message")
|
||||
return
|
||||
end
|
||||
|
@ -25,6 +57,8 @@ end
|
|||
function mt_irc:register_bot_command(name, def)
|
||||
if (not def.func) or (type(def.func) ~= "function") then
|
||||
error("Erroneous bot command definition. def.func missing.", 2)
|
||||
elseif name:sub(1, 1) == "@" then
|
||||
error("Erroneous bot command name. Command name begins with '@'.", 2)
|
||||
end
|
||||
self.bot_commands[name] = def
|
||||
end
|
||||
|
@ -35,22 +69,21 @@ mt_irc:register_bot_command("help", {
|
|||
description = "Get help about a command",
|
||||
func = function(user, args)
|
||||
if args == "" then
|
||||
mt_irc:say(user.nick, "No command name specified. Use 'list' for a list of cammands")
|
||||
mt_irc:reply("No command name specified. Use 'list' for a list of commands")
|
||||
return
|
||||
end
|
||||
|
||||
local cmd = mt_irc.bot_commands[args]
|
||||
if not cmd then
|
||||
mt_irc:say(user.nick, "Unknown command '"..cmdname.."'.")
|
||||
mt_irc:reply("Unknown command '"..cmdname.."'.")
|
||||
return
|
||||
end
|
||||
|
||||
local usage = ("Usage: %c%s %s -- %s"):format(
|
||||
mt_irc:reply(("Usage: %c%s %s -- %s"):format(
|
||||
mt_irc.config.command_prefix,
|
||||
args,
|
||||
cmd.params or "<no parameters>",
|
||||
cmd.description or "<no description>")
|
||||
mt_irc:say(user.nick, usage)
|
||||
cmd.description or "<no description>"))
|
||||
end
|
||||
})
|
||||
|
||||
|
@ -63,8 +96,8 @@ mt_irc:register_bot_command("list", {
|
|||
for name, cmd in pairs(mt_irc.bot_commands) do
|
||||
cmdlist = cmdlist..name..", "
|
||||
end
|
||||
mt_irc:say(user.nick, cmdlist
|
||||
.." -- Use 'help <command name>' to get help about a specific command.")
|
||||
mt_irc:reply(cmdlist.." -- Use 'help <command name>' to get"
|
||||
.." help about a specific command.")
|
||||
end
|
||||
})
|
||||
|
||||
|
@ -81,10 +114,10 @@ mt_irc:register_bot_command("whereis", {
|
|||
if player then
|
||||
local fmt = "Player %s is at (%.2f,%.2f,%.2f)"
|
||||
local pos = player:getpos()
|
||||
mt_irc:say(user.nick, fmt:format(args, pos.x, pos.y, pos.z))
|
||||
mt_irc:reply(fmt:format(args, pos.x, pos.y, pos.z))
|
||||
return
|
||||
end
|
||||
mt_irc:say(user.nick, "There is No player named '"..args.."'")
|
||||
mt_irc:reply("There is no player named '"..args.."'")
|
||||
end
|
||||
})
|
||||
|
||||
|
@ -96,7 +129,7 @@ mt_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"
|
||||
mt_irc:say(user.nick, fmt:format(
|
||||
mt_irc:reply(fmt:format(
|
||||
math.floor(diff / 60 / 60),
|
||||
math.mod(math.floor(diff / 60), 60),
|
||||
math.mod(math.floor(diff), 60)
|
||||
|
@ -113,7 +146,7 @@ mt_irc:register_bot_command("players", {
|
|||
for _, player in pairs(players) do
|
||||
table.insert(names, player:get_player_name())
|
||||
end
|
||||
mt_irc:say(user.nick, "Connected players: "
|
||||
mt_irc:reply("Connected players: "
|
||||
..table.concat(names, ", "))
|
||||
end
|
||||
})
|
||||
|
|
|
@ -50,9 +50,8 @@ config.interval = tonumber(minetest.setting_get("irc.interval")) or 2.0
|
|||
-- Underlying socket timeout in seconds (number, default 60.0).
|
||||
config.timeout = tonumber(minetest.setting_get("irc.timeout")) or 60.0
|
||||
|
||||
-- Prefix to use for bot commands (char, default '!')
|
||||
config.command_prefix = minetest.setting_get("irc.command_prefix") or '!'
|
||||
config.command_prefix = config.command_prefix:sub(1, 1)
|
||||
-- Prefix to use for bot commands (string)
|
||||
config.command_prefix = minetest.setting_get("irc.command_prefix")
|
||||
|
||||
-- Enable debug output (boolean, default false)
|
||||
config.debug = minetest.setting_getbool("irc.debug")
|
||||
|
|
|
@ -62,8 +62,10 @@ function mt_irc.hooks.chat(user, channel, message)
|
|||
end
|
||||
|
||||
if channel == mt_irc.conn.nick then
|
||||
mt_irc.last_from = user.nick
|
||||
mt_irc.conn:invoke("PrivateMessage", user, message)
|
||||
else
|
||||
mt_irc.last_from = channel
|
||||
mt_irc.conn:invoke("OnChannelChat", user, channel, message)
|
||||
end
|
||||
end
|
||||
|
@ -96,16 +98,18 @@ function mt_irc.hooks.channelChat(user, channel, message)
|
|||
-- "<server@IRC> <player> message" into "<player@server> message"
|
||||
-- "<server@IRC> *** player joined/left the game" into "*** player@server joined/left the game"
|
||||
-- and "<server@IRC> * player orders a pizza" into "* player@server orders a pizza"
|
||||
local foundchat, _, chatnick, chatmessage
|
||||
= message:find("^<([^>]+)> (.*)$")
|
||||
local foundjoin, _, joinnick
|
||||
= message:find("^%*%*%* ([^%s]+) joined the game$")
|
||||
local foundleave, _, leavenick
|
||||
= message:find("^%*%*%* ([^%s]+) left the game$")
|
||||
local foundaction, _, actionnick, actionmessage
|
||||
= message:find("^%* ([^%s]+) (.*)$")
|
||||
local foundchat, _, chatnick, chatmessage =
|
||||
message:find("^<([^>]+)> (.*)$")
|
||||
local foundjoin, _, joinnick =
|
||||
message:find("^%*%*%* ([^%s]+) joined the game$")
|
||||
local foundleave, _, leavenick =
|
||||
message:find("^%*%*%* ([^%s]+) left the game$")
|
||||
local foundaction, _, actionnick, actionmessage =
|
||||
message:find("^%* ([^%s]+) (.*)$")
|
||||
|
||||
if foundchat then
|
||||
if mt_irc:check_botcmd(user, channel, message) then
|
||||
return
|
||||
elseif foundchat then
|
||||
mt_irc:sendLocal(("<%s@%s> %s")
|
||||
:format(chatnick, user.nick, chatmessage))
|
||||
elseif foundjoin then
|
||||
|
@ -124,29 +128,12 @@ end
|
|||
|
||||
|
||||
function mt_irc.hooks.pm(user, message)
|
||||
local player_to
|
||||
local msg
|
||||
if message:sub(1, 1) == "@" then
|
||||
local found, _, player_to, message = message:find("^.([^%s]+)%s(.+)$")
|
||||
if not mt_irc.joined_players[player_to] then
|
||||
mt_irc:say(user.nick, "User '"..player_to.."' has parted.")
|
||||
return
|
||||
elseif not minetest.get_player_by_name(player_to) then
|
||||
mt_irc:say(user.nick, "User '"..player_to.."' is not in the game.")
|
||||
return
|
||||
end
|
||||
minetest.chat_send_player(player_to,
|
||||
"PM from "..user.nick.."@IRC: "..message, false)
|
||||
mt_irc:say(user.nick, "Message sent!")
|
||||
elseif message:sub(1, 1) == "!" then
|
||||
mt_irc:bot_command(user, message:sub(2))
|
||||
return
|
||||
else
|
||||
mt_irc:say(user.nick, "Invalid command. Use '"
|
||||
..mt_irc.config.command_prefix
|
||||
.."list' to see possible commands.")
|
||||
return
|
||||
-- Trim prefix if it is found
|
||||
local prefix = mt_irc.config.command_prefix
|
||||
if prefix and message:sub(1, #prefix) == prefix then
|
||||
message = message:sub(#prefix + 1)
|
||||
end
|
||||
mt_irc:bot_command(user, message)
|
||||
end
|
||||
|
||||
|
||||
|
|
|
@ -134,6 +134,13 @@ function mt_irc:say(to, message)
|
|||
end
|
||||
|
||||
|
||||
function mt_irc:reply(message)
|
||||
if not self.last_from then
|
||||
return
|
||||
end
|
||||
self:say(self.last_from, message)
|
||||
end
|
||||
|
||||
function mt_irc:send(line)
|
||||
self.conn:send(line)
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue
Block a user