forked from group-butler/GroupButler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
polling.lua
executable file
·67 lines (59 loc) · 1.88 KB
/
polling.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env lua
package.path=package.path .. ';./lua/?.lua'
io.stdout:setvbuf "no" -- switch off buffering for stdout
local api = require 'methods'
local clr = require 'term.colors'
local plugins = require 'plugins'
local main = require 'main'
bot = api.getMe().result
local last_update, last_cron, current
function bot.init(on_reload) -- The function run when the bot is started or reloaded
if on_reload then
package.loaded.config = nil
package.loaded.languages = nil
package.loaded.utilities = nil
end
print('\n'..clr.blue..'BOT RUNNING:'..clr.reset,
clr.red..'[@'..bot.username .. '] [' .. bot.first_name ..'] ['..bot.id..']'..clr.reset..'\n')
last_update = last_update or -2 -- skip pending updates
last_cron = last_cron or os.time() -- the time of the last cron job
if on_reload then
return #plugins
else
api.sendAdmin('Bot started!\n'..os.date('On %A, %d %B %Y\nAt %X')..'\n'..#plugins..' plugins loaded', true)
bot.start_timestamp = os.time()
current = {h = 0}
bot.last = {h = 0}
end
end
bot.init()
api.firstUpdate()
while true do -- Start a loop while the bot should be running.
local res = api.getUpdates(last_update+1) -- Get the latest updates
if res then
-- clocktime_last_update = os.clock()
for i=1, #res.result do -- Go through every new message.
last_update = res.result[i].update_id
--print(last_update)
current.h = current.h + 1
main.parseMessageFunction(res.result[i])
end
else
print('Connection error')
end
if last_cron ~= os.date('%H') then -- Run cron jobs every hour.
last_cron = os.date('%H')
bot.last.h = current.h
current.h = 0
print(clr.yellow..'Cron...'..clr.reset)
for i=1, #plugins do
if plugins[i].cron then -- Call each plugin's cron function, if it has one.
local res2, err = pcall(plugins[i].cron)
if not res2 then
api.sendLog('An #error occurred (cron).\n'..err)
return
end
end
end
end
end