2013-09-22 11:54:15 +00:00
|
|
|
-- All bot-like behaviour (response to commands, etc)
|
|
|
|
|
|
|
|
bot = {}
|
|
|
|
|
|
|
|
function bot:Initialize(IRC, Prefix)
|
|
|
|
self._irc = IRC
|
|
|
|
self._prefix = Prefix
|
2018-09-14 18:34:10 +00:00
|
|
|
self._meme = "alexa, "
|
2013-09-22 11:54:15 +00:00
|
|
|
self._commands = {}
|
|
|
|
|
|
|
|
hook.Add('irc.Message', 'bot.OnChannelMessage', function(Username, Channel, Message)
|
|
|
|
local Success, Error = pcall(function()
|
|
|
|
self:OnChannelMessage(Username, Channel, Message)
|
|
|
|
end)
|
|
|
|
if not Success then
|
|
|
|
Channel:Say("Whoops! Error when executing OnChannelMessage: " .. Error)
|
|
|
|
end
|
|
|
|
end)
|
2013-09-22 19:28:48 +00:00
|
|
|
self:AddCommand('eval-core', -1, function(Username, Channel, String)
|
|
|
|
local Function, Message = loadstring(String)
|
|
|
|
if not Function then
|
|
|
|
Channel:Say("Parse error: " .. Message)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local Status, Message = pcall(Function)
|
|
|
|
if not Status then
|
|
|
|
Channel:Say("Error -> " .. Message)
|
|
|
|
else
|
|
|
|
Channel:Say("OK -> " .. tostring(Message))
|
|
|
|
end
|
|
|
|
end, "Runs a Lua command in the bot context.", 100)
|
2013-09-22 11:54:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function bot:OnChannelMessage(Username, Channel, Message)
|
2018-09-14 18:34:10 +00:00
|
|
|
local String
|
2013-09-22 11:54:15 +00:00
|
|
|
if Message:sub(1,#self._prefix) == self._prefix then
|
2018-09-14 18:34:10 +00:00
|
|
|
String = Message:sub(#self._prefix + 1)
|
|
|
|
elseif Message:lower():sub(1, #self._meme) == self._meme then
|
|
|
|
String = Message:sub(#self._meme + 1)
|
|
|
|
else
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local Command
|
|
|
|
local Arguments = {}
|
|
|
|
for Part in String:gmatch("%S+") do
|
|
|
|
if Command == nil then
|
|
|
|
Command = Part
|
|
|
|
else
|
|
|
|
Arguments[#Arguments + 1] = Part
|
2013-09-22 11:54:15 +00:00
|
|
|
end
|
2018-09-14 18:34:10 +00:00
|
|
|
end
|
2013-09-22 11:54:15 +00:00
|
|
|
|
2018-09-14 18:34:10 +00:00
|
|
|
if Command == nil then
|
|
|
|
Channel:Say("No command given.")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if not self._commands[Command] then
|
|
|
|
local FellBack = hook.Call("bot.UnknownCommand", Username, Channel, Command, Arguments)
|
|
|
|
if not FellBack then
|
|
|
|
Channel:Say("Unknown command '" .. Command .. "'.")
|
|
|
|
end
|
|
|
|
else
|
|
|
|
local CommandData = self._commands[Command]
|
|
|
|
if #Arguments ~= CommandData.Arguments and CommandData.Arguments ~= -1 then
|
|
|
|
Channel:Say(string.format("Command '%s' expects '%i' arguments, got '%i'.",
|
|
|
|
Command, CommandData.Arguments, #Arguments))
|
2013-09-22 11:54:15 +00:00
|
|
|
else
|
2018-09-14 18:34:10 +00:00
|
|
|
-- -1 means we want a raw string
|
|
|
|
if CommandData.Arguments == -1 then
|
|
|
|
if #Arguments < 1 then
|
|
|
|
Channel:Say("Please provide an argument.")
|
2013-09-22 16:58:15 +00:00
|
|
|
return
|
|
|
|
end
|
2018-09-14 18:34:10 +00:00
|
|
|
Arguments = { table.concat(Arguments, ' ') }
|
|
|
|
end
|
|
|
|
local RequiredAccess = CommandData.Access
|
|
|
|
if RequiredAccess == 0 then
|
|
|
|
CommandData.Callback(Username, Channel, unpack(Arguments))
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local Account = hook.Call("auth.GetAccount", irc, Username)
|
|
|
|
if not Account then
|
|
|
|
Channel:Say("Please identify with NickServ.")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local UserAccess = hook.Call("auth.GetLevel", Channel, Account)
|
|
|
|
if not UserAccess then
|
|
|
|
Channel:Say("Could not run command because auth backend is missing.")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if UserAccess >= RequiredAccess then
|
|
|
|
CommandData.Callback(Username, Channel, unpack(Arguments))
|
|
|
|
else
|
|
|
|
Channel:Say(string.format("Insufficient access level (%i required).", RequiredAccess))
|
2013-09-22 11:54:15 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function bot:AddCommand(Name, Arguments, Callback, Help, Access)
|
|
|
|
local Command = {}
|
|
|
|
Command.Callback = Callback
|
|
|
|
Command.Access = Access or 0
|
|
|
|
Command.Help = Help or "No help available."
|
|
|
|
Command.Arguments = Arguments
|
|
|
|
self._commands[Name] = Command
|
|
|
|
end
|
|
|
|
|
2013-09-22 12:56:38 +00:00
|
|
|
function bot:RemoveCommand(Name)
|
|
|
|
self._commands[Name] = nil
|
|
|
|
end
|