]> git.lizzy.rs Git - luairc.git/blob - test/luabot.lua
6c9b43a8881db9cd9cc59b322e002a60d92fa4f5
[luairc.git] / test / luabot.lua
1 #!/usr/bin/env lua
2
3 local irc = require 'irc'
4 irc.DEBUG = true
5
6 local nick = "luabot"
7 local pre_code = [[
8 io = nil
9 os = nil
10 loadfile = nil
11 dofile = nil
12 package = nil
13 require = nil
14 module = nil
15 debug = nil
16 ]]
17
18 irc.register_callback("connect", function()
19     irc.join("#doytest")
20 end)
21
22 irc.register_callback("channel_msg", function(channel, from, message)
23     local for_me, code = message:match("^(" .. nick .. ". )(.*)")
24     if for_me then
25         code = code:gsub("^=", "return ")
26         local fn, err = loadstring(pre_code .. code)
27         if not fn then
28             irc.say(channel.name, from .. ": Error loading code: " .. err)
29             return
30         else
31             local result = {pcall(fn)}
32             local success = table.remove(result, 1)
33             if not success then
34                 irc.say(channel.name, from .. ": Error running code: " .. result[1])
35             else
36                 irc.say(channel.name, from .. ": " .. table.concat(result, ", "))
37             end
38         end
39     end
40 end)
41
42 irc.connect{network = "irc.freenode.net", nick = nick}