2015-07-03 00:15:52 +02:00
|
|
|
HTTP = require('socket.http')
|
2015-08-29 08:15:01 +02:00
|
|
|
HTTPS= require('ssl.https')
|
|
|
|
URL = require('socket.url')
|
2015-07-10 09:52:22 +02:00
|
|
|
JSON = require('dkjson')
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2015-09-07 17:19:36 +02:00
|
|
|
VERSION = '2.10'
|
2015-07-03 00:15:52 +02:00
|
|
|
|
|
|
|
function on_msg_receive(msg)
|
|
|
|
|
2015-08-29 08:15:01 +02:00
|
|
|
if blacklist[tostring(msg.from.id)] then return end
|
2015-09-07 17:19:36 +02:00
|
|
|
if floodcontrol[-msg.chat.id] then -- This stuff is useful for the moderation plugin to not be completely unusable when floodcontrol is activated.
|
|
|
|
msg.flood = msg.chat.id
|
|
|
|
msg.chat.id = msg.from.id
|
|
|
|
end
|
2015-08-04 22:11:55 +02:00
|
|
|
|
2015-07-10 09:52:22 +02:00
|
|
|
msg = process_msg(msg)
|
|
|
|
|
2015-09-22 01:34:02 +02:00
|
|
|
if msg.date < os.time() - 10 then return end -- don't react to old messages
|
2015-07-03 00:15:52 +02:00
|
|
|
if not msg.text then return end -- don't react to media messages
|
|
|
|
if msg.forward_from then return end -- don't react to forwarded messages
|
2015-08-04 22:11:55 +02:00
|
|
|
|
2015-07-03 00:15:52 +02:00
|
|
|
local lower = string.lower(msg.text)
|
|
|
|
for i,v in pairs(plugins) do
|
|
|
|
for j,w in pairs(v.triggers) do
|
|
|
|
if string.match(lower, w) then
|
2015-08-18 11:55:25 +02:00
|
|
|
if v.typing then
|
2015-09-22 01:34:02 +02:00
|
|
|
send_chat_action(msg.chat.id, 'typing')
|
2015-07-19 22:46:06 +02:00
|
|
|
end
|
2015-08-09 02:53:46 +02:00
|
|
|
local a,b = pcall(function() -- Janky error handling
|
2015-08-04 22:11:55 +02:00
|
|
|
v.action(msg)
|
|
|
|
end)
|
|
|
|
if not a then
|
2015-09-23 20:48:54 +02:00
|
|
|
print('',msg.text,'\n',b) -- debugging
|
2015-08-04 22:11:55 +02:00
|
|
|
send_msg(msg, b)
|
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function bot_init()
|
|
|
|
|
2015-08-29 08:15:01 +02:00
|
|
|
print('Loading configuration...')
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2015-07-15 08:15:23 +02:00
|
|
|
config = dofile('config.lua')
|
2015-07-03 00:15:52 +02:00
|
|
|
require('bindings')
|
2015-08-29 08:15:01 +02:00
|
|
|
require('utilities')
|
|
|
|
blacklist = load_data('blacklist.json')
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2015-08-29 08:15:01 +02:00
|
|
|
print('Fetching bot information...')
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2015-09-07 17:19:36 +02:00
|
|
|
bot = get_me()
|
|
|
|
while bot == false do
|
|
|
|
print('Failure fetching bot information. Trying again...')
|
|
|
|
bot = get_me()
|
2015-07-19 22:46:06 +02:00
|
|
|
end
|
2015-09-07 17:19:36 +02:00
|
|
|
bot = bot.result
|
2015-07-03 00:15:52 +02:00
|
|
|
|
|
|
|
print('Loading plugins...')
|
|
|
|
|
|
|
|
plugins = {}
|
|
|
|
for i,v in ipairs(config.plugins) do
|
2015-07-15 08:15:23 +02:00
|
|
|
local p = dofile('plugins/'..v)
|
2015-07-03 00:15:52 +02:00
|
|
|
table.insert(plugins, p)
|
|
|
|
end
|
|
|
|
|
2015-08-29 08:15:01 +02:00
|
|
|
print('Plugins loaded: ' .. #plugins .. '. Generating help message...')
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2015-07-04 16:54:41 +02:00
|
|
|
help_message = ''
|
2015-07-03 00:15:52 +02:00
|
|
|
for i,v in ipairs(plugins) do
|
|
|
|
if v.doc then
|
|
|
|
local a = string.sub(v.doc, 1, string.find(v.doc, '\n')-1)
|
|
|
|
help_message = help_message .. ' - ' .. a .. '\n'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-29 08:15:01 +02:00
|
|
|
print('@'.. bot.username ..', AKA '.. bot.first_name ..' ('.. bot.id ..')')
|
2015-07-11 05:43:30 +02:00
|
|
|
|
2015-07-03 00:15:52 +02:00
|
|
|
is_started = true
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-07-10 09:52:22 +02:00
|
|
|
function process_msg(msg)
|
|
|
|
|
2015-09-07 17:19:36 +02:00
|
|
|
if msg.new_chat_participant then
|
|
|
|
if msg.new_chat_participant.id ~= bot.id then
|
|
|
|
if msg.from.id == 100547061 then return msg end
|
|
|
|
msg.text = 'hi '..bot.first_name
|
|
|
|
msg.from = msg.new_chat_participant
|
|
|
|
else
|
|
|
|
msg.text = '/about'
|
|
|
|
end
|
2015-07-10 09:52:22 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
if msg.left_chat_participant and msg.left_chat_participant.id ~= bot.id then
|
2015-08-29 08:15:01 +02:00
|
|
|
if msg.from.id == 100547061 then return msg end
|
2015-07-10 09:52:22 +02:00
|
|
|
msg.text = 'bye '..bot.first_name
|
|
|
|
msg.from = msg.left_chat_participant
|
|
|
|
end
|
|
|
|
|
|
|
|
return msg
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-07-03 00:15:52 +02:00
|
|
|
bot_init()
|
|
|
|
last_update = 0
|
2015-09-23 20:48:54 +02:00
|
|
|
last_cron = os.time()
|
2015-08-29 08:15:01 +02:00
|
|
|
|
2015-08-04 22:11:55 +02:00
|
|
|
while is_started do
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2015-08-04 22:11:55 +02:00
|
|
|
local res = get_updates(last_update+1)
|
2015-07-19 22:46:06 +02:00
|
|
|
if not res then
|
2015-07-15 08:15:23 +02:00
|
|
|
print('Error getting updates.')
|
|
|
|
else
|
2015-07-19 22:46:06 +02:00
|
|
|
for i,v in ipairs(res.result) do
|
2015-07-15 08:15:23 +02:00
|
|
|
if v.update_id > last_update then
|
|
|
|
last_update = v.update_id
|
|
|
|
on_msg_receive(v.message)
|
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-29 08:15:01 +02:00
|
|
|
-- cron-like thing
|
|
|
|
-- run PLUGIN.cron() every five seconds
|
2015-09-23 20:48:54 +02:00
|
|
|
if last_cron < os.time() - 5 then
|
2015-08-29 08:15:01 +02:00
|
|
|
for k,v in pairs(plugins) do
|
|
|
|
if v.cron then
|
2015-09-07 17:19:36 +02:00
|
|
|
a,b = pcall(function() v.cron() end)
|
|
|
|
if not a then print(b) end
|
2015-07-15 08:15:23 +02:00
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
2015-09-23 20:48:54 +02:00
|
|
|
last_cron = os.time()
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
2015-08-29 08:15:01 +02:00
|
|
|
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
2015-08-29 08:15:01 +02:00
|
|
|
|
2015-07-19 22:46:06 +02:00
|
|
|
print('Halted.')
|