From 1dc61d2c44e6b574c1e6933e572dc7ea420fabc8 Mon Sep 17 00:00:00 2001 From: Brayden Banks Date: Wed, 13 Apr 2016 20:48:20 -0700 Subject: [PATCH] Final fixes. --- bindings.lua | 12 ++++---- bot.lua | 54 ++++++++++++++++++----------------- config.lua | 58 ++++++++++++++++++-------------------- launch.sh | 2 +- main.lua | 5 ++++ plugins/administration.lua | 8 +++--- plugins/echo.lua | 2 +- plugins/help.lua | 13 +++++---- plugins/luarun.lua | 2 +- plugins/moderation.lua | 10 +++---- plugins/nick.lua | 2 +- plugins/reactions.lua | 4 +-- plugins/shell.lua | 2 +- plugins/time.lua | 4 ++- plugins/whoami.lua | 2 +- utilities.lua | 17 ++++++----- 16 files changed, 105 insertions(+), 92 deletions(-) create mode 100644 main.lua diff --git a/bindings.lua b/bindings.lua index 9cf8974..2baf84b 100755 --- a/bindings.lua +++ b/bindings.lua @@ -75,7 +75,7 @@ function bindings:sendChatAction(chat_id, action) -- Support actions are typing, upload_photo, record_video, upload_video, record_audio, upload_audio, upload_document, find_location local url = self.BASE_URL .. '/sendChatAction?chat_id=' .. chat_id .. '&action=' .. action - return bindings.sendRequest(self, url) + return bindings.sendRequest(url) end @@ -94,7 +94,7 @@ function bindings:sendLocation(chat_id, latitude, longitude, reply_to_message_id url = url .. '&disable_notification=true' end - return bindings.sendRequest(self, url) + return bindings.sendRequest(url) end @@ -106,18 +106,18 @@ function bindings:forwardMessage(chat_id, from_chat_id, message_id, disable_noti url = url .. '&disable_notification=true' end - return bindings.sendRequest(self, url) + return bindings.sendRequest(url) end function bindings:kickChatMember(chat_id, user_id) local url = self.BASE_URL .. '/kickChatMember?chat_id=' .. chat_id .. '&user_id=' .. user_id - return bindings.sendRequest(self, url) + return bindings.sendRequest(url) end function bindings:unbanChatMember(chat_id, user_id) local url = self.BASE_URL .. '/unbanChatMember?chat_id=' .. chat_id .. '&user_id=' .. user_id - return bindings.sendRequest(self, url) + return bindings.sendRequest(url) end -- TODO: More of this. @@ -138,7 +138,7 @@ function bindings:sendPhotoID(chat_id, file_id, caption, reply_to_message_id, di url = url .. '&disable_notification=true' end - return bindings.sendRequest(self, url) + return bindings.sendRequest(url) end diff --git a/bot.lua b/bot.lua index 3e91658..25f10f6 100755 --- a/bot.lua +++ b/bot.lua @@ -1,7 +1,5 @@ local bot = {} -local instance = {} - local bindings = require('bindings') -- Load Telegram bindings. local utilities = require('utilities') -- Load miscellaneous and cross-plugin functions. @@ -27,7 +25,7 @@ function bot:init() -- The function run when the bot is started or reloaded. self.plugins = {} -- Load plugins. for _,v in ipairs(self.config.plugins) do - local p = require('plugins/'..v) + local p = require('plugins.'..v) table.insert(self.plugins, p) if p.init then p.init(self) end end @@ -77,7 +75,7 @@ function bot:on_msg_receive(msg) -- The fn run whenever a message is received. end) if not success then bindings.sendReply(self, msg, 'Sorry, an unexpected error occurred.') - bindings.handle_exception(self, result, msg.from.id .. ': ' .. msg.text) + utilities.handle_exception(self, result, msg.from.id .. ': ' .. msg.text) return end -- If the action returns a table, make that table msg. @@ -93,37 +91,41 @@ function bot:on_msg_receive(msg) -- The fn run whenever a message is received. end -bot.init(instance) -- Actually start the script. Run the bot_init function. +function bot:run() + bot.init(self) -- Actually start the script. Run the bot_init function. -while instance.is_started do -- Start a loop while the bot should be running. + while self.is_started do -- Start a loop while the bot should be running. - do - local res = bindings.getUpdates(instance, instance.last_update+1) -- Get the latest updates! - if res then - for _,v in ipairs(res.result) do -- Go through every new message. - instance.last_update = v.update_id - bot.on_msg_receive(instance, v.message) + do + local res = bindings.getUpdates(self, self.last_update+1) -- Get the latest updates! + if res then + for _,v in ipairs(res.result) do -- Go through every new message. + self.last_update = v.update_id + bot.on_msg_receive(self, v.message) + end + else + print(self.config.errors.connection) end - else - print(instance.config.errors.connection) end - end - if instance.last_cron ~= os.date('%M') then -- Run cron jobs every minute. - instance.last_cron = os.date('%M') - utilities.save_data(instance.info.username..'.db', instance.database) -- Save the database. - for i,v in ipairs(instance.plugins) do - if v.cron then -- Call each plugin's cron function, if it has one. - local res, err = pcall(function() v.cron(instance) end) - if not res then - utilities.handle_exception(instance, err, 'CRON: ' .. i) + if self.last_cron ~= os.date('%M') then -- Run cron jobs every minute. + self.last_cron = os.date('%M') + utilities.save_data(self.info.username..'.db', self.database) -- Save the database. + for i,v in ipairs(self.plugins) do + if v.cron then -- Call each plugin's cron function, if it has one. + local res, err = pcall(function() v.cron(self) end) + if not res then + utilities.handle_exception(self, err, 'CRON: ' .. i) + end end end end + end + -- Save the database before exiting. + utilities.save_data(self.info.username..'.db', self.database) + print('Halted.') end - -- Save the database before exiting. -utilities.save_data(instance.info.username..'.db', instance.database) -print('Halted.') +return bot diff --git a/config.lua b/config.lua index bac5ead..541a9f0 100755 --- a/config.lua +++ b/config.lua @@ -4,8 +4,6 @@ return { bot_api_key = '', -- Your Telegram ID. admin = 00000000, - -- Differences, in seconds, between your time and UTC. - time_offset = 0, -- Two-letter language code. lang = 'en', -- The channel, group, or user to send error reports to. @@ -51,35 +49,35 @@ Send /help to get started. }, plugins = { -- To enable a plugin, add its name to the list. - 'control.lua', - 'blacklist.lua', - 'about.lua', - 'ping.lua', - 'whoami.lua', - 'nick.lua', - 'echo.lua', - 'gSearch.lua', - 'gMaps.lua', - 'wikipedia.lua', - 'hackernews.lua', - 'imdb.lua', - 'calc.lua', - 'urbandictionary.lua', - 'time.lua', - 'eightball.lua', - 'dice.lua', - 'reddit.lua', - 'xkcd.lua', - 'slap.lua', - 'commit.lua', - 'pun.lua', - 'currency.lua', - 'cats.lua', - 'shout.lua', - 'patterns.lua', + 'control', + 'blacklist', + 'about', + 'ping', + 'whoami', + 'nick', + 'echo', + 'gSearch', + 'gMaps', + 'wikipedia', + 'hackernews', + 'imdb', + 'calc', + 'urbandictionary', + 'time', + 'eightball', + 'dice', + 'reddit', + 'xkcd', + 'slap', + 'commit', + 'pun', + 'currency', + 'cats', + 'shout', + 'patterns', -- Put new plugins above this line. - 'help.lua', - 'greetings.lua' + 'help', + 'greetings' } } diff --git a/launch.sh b/launch.sh index 6c20e59..be8b691 100755 --- a/launch.sh +++ b/launch.sh @@ -1,7 +1,7 @@ #!/bin/sh while true; do - lua bot.lua + lua main.lua echo 'otouto has stopped. ^C to exit.' sleep 5s done diff --git a/main.lua b/main.lua new file mode 100644 index 0000000..9158bcc --- /dev/null +++ b/main.lua @@ -0,0 +1,5 @@ +local bot = require('bot') + +local instance = {} + +return bot.run(instance) diff --git a/plugins/administration.lua b/plugins/administration.lua index 43ae4b7..d328433 100644 --- a/plugins/administration.lua +++ b/plugins/administration.lua @@ -324,7 +324,7 @@ function administration.init_command(self_) if self.admin_temp.flood[msg.chat.id_str][msg.from.id_str] > 99 then drua.kick_user(msg.chat.id, msg.from.id) local output = administration.flags[5].kicked:gsub('GROUPNAME', msg.chat.title) - bindings.sendMessage(msg.from.id, output) + bindings.sendMessage(self, msg.from.id, output) self.admin_temp.flood[msg.chat.id_str][msg.from.id_str] = nil return end @@ -372,7 +372,7 @@ function administration.init_command(self_) else group.name = msg.new_chat_title if group.grouptype == 'supergroup' then - administration.update_desc(msg.chat.id) + administration.update_desc(self, msg.chat.id) end end return @@ -799,7 +799,7 @@ function administration.init_command(self_) local status = group.flags[i] or false output = output .. '`[' .. i .. ']` *' .. v.name .. '*` = ' .. tostring(status) .. '`\n• ' .. v.desc .. '\n' end - bindings.sendMessage(msg.chat.id, output, true, nil, true) + bindings.sendMessage(self, msg.chat.id, output, true, nil, true) return end if group.flags[input] == true then @@ -987,7 +987,7 @@ function administration.init_command(self_) action = function(self, msg) if self.database.administration.groups[msg.chat.id_str] then - bindings.sendReply(msg, 'I am already administrating this group.') + bindings.sendReply(self, msg, 'I am already administrating this group.') return end self.database.administration.groups[msg.chat.id_str] = { diff --git a/plugins/echo.lua b/plugins/echo.lua index 26be359..9ec8bee 100755 --- a/plugins/echo.lua +++ b/plugins/echo.lua @@ -10,7 +10,7 @@ Repeats a string of text. ```]] function echo:init() - echo.triggers = utilities.triggers(self.info.username):t('echo').table + echo.triggers = utilities.triggers(self.info.username):t('echo', true).table end function echo:action(msg) diff --git a/plugins/help.lua b/plugins/help.lua index a84bf78..0e99280 100755 --- a/plugins/help.lua +++ b/plugins/help.lua @@ -6,24 +6,27 @@ local help = {} local bindings = require('bindings') local utilities = require('utilities') -local help_text = '*Available commands:*' +local help_text function help:init() + help_text = '*Available commands:*' + for _,plugin in ipairs(self.plugins) do if plugin.command then help_text = help_text .. '\n• /' .. plugin.command:gsub('%[', '\\[') end end - help.triggers = utilities.triggers(self.info.username):t('help', true):t('h', true).table -end - -help_text = help_text .. [[ + help_text = help_text .. [[ • /help Arguments: \[optional] ]] + help.triggers = utilities.triggers(self.info.username):t('help', true):t('h', true).table + +end + function help:action(msg) local input = utilities.input(msg.text_lower) diff --git a/plugins/luarun.lua b/plugins/luarun.lua index 9200246..34eda5c 100644 --- a/plugins/luarun.lua +++ b/plugins/luarun.lua @@ -19,7 +19,7 @@ function luarun:action(msg) return end - local output = loadstring(input)() + local output = loadstring('local utilities = require(\'utilities\'); return function (instance) '..input..' end')()(self) if output == nil then output = 'Done!' elseif type(output) == 'table' then diff --git a/plugins/moderation.lua b/plugins/moderation.lua index c8417f1..0e3eb18 100755 --- a/plugins/moderation.lua +++ b/plugins/moderation.lua @@ -268,12 +268,12 @@ function moderation:init() moderation.triggers = {} for trigger,_ in pairs(commands) do if trigger[-1] == '$' then - moderation.triggers:insert(trigger:sub(1, -2)..'@'..self.info.username..'$') + tables.insert(moderation.triggers, trigger:sub(1, -2)..'@'..self.info.username..'$') else - moderation.triggers:insert(trigger..'%s+[^%s]*') - moderation.triggers:insert(trigger..'@'..self.info.username..'%s+[^%s]*') - moderation.triggers:insert(trigger..'$') - moderation.triggers:insert(trigger..'@'..self.info.username..'$') + tables.insert(moderation.triggers, trigger..'%s+[^%s]*') + tables.insert(moderation.triggers, trigger..'@'..self.info.username..'%s+[^%s]*') + tables.insert(moderation.triggers, trigger..'$') + tables.insert(moderation.triggers, trigger..'@'..self.info.username..'$') end end end diff --git a/plugins/nick.lua b/plugins/nick.lua index 603a04e..d2a680b 100755 --- a/plugins/nick.lua +++ b/plugins/nick.lua @@ -10,7 +10,7 @@ Set your nickname. Use "/nick --" to delete it. ```]] function nick:init() - nick.triggers = utilities.triggers(self.info.nick):t('nick', true).table + nick.triggers = utilities.triggers(self.info.username):t('nick', true).table end function nick:action(msg) diff --git a/plugins/reactions.lua b/plugins/reactions.lua index 4ea2c55..0087378 100755 --- a/plugins/reactions.lua +++ b/plugins/reactions.lua @@ -28,8 +28,8 @@ function reactions:init() reactions.triggers = utilities.triggers(self.info.username):t('reactions').table for trigger,reaction in pairs(mapping) do help = help .. '• ' .. trigger:gsub('.%?', '') .. ': ' .. reaction .. '\n' - reactions.triggers:insert(utilities.INVOCATION_PATTERN..trigger) - reactions.triggers:insert(utilities.INVOCATION_PATTERN..trigger..'@'..self.username) + table.insert(reactions.triggers, utilities.INVOCATION_PATTERN..trigger) + table.insert(reactions.triggers, utilities.INVOCATION_PATTERN..trigger..'@'..self.username) end end diff --git a/plugins/shell.lua b/plugins/shell.lua index b1b90b0..1a1bd6f 100644 --- a/plugins/shell.lua +++ b/plugins/shell.lua @@ -4,7 +4,7 @@ local bindings = require('bindings') local utilities = require('utilities') function shell:init() - shell.triggers = utilities.bindings(self.info.username):t('run', true).table + shell.triggers = utilities.triggers(self.info.username):t('run', true).table end function shell:action(msg) diff --git a/plugins/time.lua b/plugins/time.lua index fe17cdb..418d679 100755 --- a/plugins/time.lua +++ b/plugins/time.lua @@ -43,7 +43,9 @@ function time:action(msg) local jdat = JSON.decode(jstr) - local timestamp = os.time() + jdat.rawOffset + jdat.dstOffset + self.config.time_offset + local now = os.time() + local time_offset = os.difftime(now, os.time(os.date("!*t", now))) + local timestamp = now + jdat.rawOffset + jdat.dstOffset + time_offset local utcoff = (jdat.rawOffset + jdat.dstOffset) / 3600 if utcoff == math.abs(utcoff) then utcoff = '+' .. utcoff diff --git a/plugins/whoami.lua b/plugins/whoami.lua index ed26d92..ddb1891 100755 --- a/plugins/whoami.lua +++ b/plugins/whoami.lua @@ -17,7 +17,7 @@ function whoami:action(msg) if msg.reply_to_message then msg = msg.reply_to_message - msg.from.name = utilities.build_name(self, msg.from.first_name, msg.from.last_name) + msg.from.name = utilities.build_name(msg.from.first_name, msg.from.last_name) end local chat_id = math.abs(msg.chat.id) diff --git a/utilities.lua b/utilities.lua index 99254e5..7ef571a 100755 --- a/utilities.lua +++ b/utilities.lua @@ -276,19 +276,20 @@ end utilities.INVOCATION_PATTERN = '/' -utilities.triggers_metatable = {} -function utilities.triggers_metatable:t(pattern, has_args) - self.table:insert('^'..utilities.INVOCATION_PATTERN..pattern..'$') - self.table:insert('^'..utilities.INVOCATION_PATTERN..pattern..'@'..self.username..'$') +utilities.triggers_meta = {} +utilities.triggers_meta.__index = utilities.triggers_meta +function utilities.triggers_meta:t(pattern, has_args) + table.insert(self.table, '^'..utilities.INVOCATION_PATTERN..pattern..'$') + table.insert(self.table, '^'..utilities.INVOCATION_PATTERN..pattern..'@'..self.username..'$') if has_args then - self.table:insert('^'..utilities.INVOCATION_PATTERN..pattern..'%s+[^%s]*') - self.table:insert('^'..utilities.INVOCATION_PATTERN..pattern..'@'..self.username..'%s+[^%s]*') + table.insert(self.table, '^'..utilities.INVOCATION_PATTERN..pattern..'%s+[^%s]*') + table.insert(self.table, '^'..utilities.INVOCATION_PATTERN..pattern..'@'..self.username..'%s+[^%s]*') end return self end function utilities.triggers(username, trigger_table) - local self = setmetatable({}, utilities.triggers_metatable) + local self = setmetatable({}, utilities.triggers_meta) self.username = username self.table = trigger_table or {} return self @@ -331,3 +332,5 @@ function utilities.enrich_message(msg) end return msg end + +return utilities