Final fixes.

This commit is contained in:
Brayden Banks 2016-04-13 20:48:20 -07:00
parent 80930f389c
commit 1dc61d2c44
16 changed files with 105 additions and 92 deletions

View File

@ -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 -- 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 local url = self.BASE_URL .. '/sendChatAction?chat_id=' .. chat_id .. '&action=' .. action
return bindings.sendRequest(self, url) return bindings.sendRequest(url)
end end
@ -94,7 +94,7 @@ function bindings:sendLocation(chat_id, latitude, longitude, reply_to_message_id
url = url .. '&disable_notification=true' url = url .. '&disable_notification=true'
end end
return bindings.sendRequest(self, url) return bindings.sendRequest(url)
end end
@ -106,18 +106,18 @@ function bindings:forwardMessage(chat_id, from_chat_id, message_id, disable_noti
url = url .. '&disable_notification=true' url = url .. '&disable_notification=true'
end end
return bindings.sendRequest(self, url) return bindings.sendRequest(url)
end end
function bindings:kickChatMember(chat_id, user_id) function bindings:kickChatMember(chat_id, user_id)
local url = self.BASE_URL .. '/kickChatMember?chat_id=' .. chat_id .. '&user_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 end
function bindings:unbanChatMember(chat_id, user_id) function bindings:unbanChatMember(chat_id, user_id)
local url = self.BASE_URL .. '/unbanChatMember?chat_id=' .. chat_id .. '&user_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 end
-- TODO: More of this. -- 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' url = url .. '&disable_notification=true'
end end
return bindings.sendRequest(self, url) return bindings.sendRequest(url)
end end

54
bot.lua
View File

@ -1,7 +1,5 @@
local bot = {} local bot = {}
local instance = {}
local bindings = require('bindings') -- Load Telegram bindings. local bindings = require('bindings') -- Load Telegram bindings.
local utilities = require('utilities') -- Load miscellaneous and cross-plugin functions. 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. self.plugins = {} -- Load plugins.
for _,v in ipairs(self.config.plugins) do for _,v in ipairs(self.config.plugins) do
local p = require('plugins/'..v) local p = require('plugins.'..v)
table.insert(self.plugins, p) table.insert(self.plugins, p)
if p.init then p.init(self) end if p.init then p.init(self) end
end end
@ -77,7 +75,7 @@ function bot:on_msg_receive(msg) -- The fn run whenever a message is received.
end) end)
if not success then if not success then
bindings.sendReply(self, msg, 'Sorry, an unexpected error occurred.') 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 return
end end
-- If the action returns a table, make that table msg. -- 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 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 do
local res = bindings.getUpdates(instance, instance.last_update+1) -- Get the latest updates! local res = bindings.getUpdates(self, self.last_update+1) -- Get the latest updates!
if res then if res then
for _,v in ipairs(res.result) do -- Go through every new message. for _,v in ipairs(res.result) do -- Go through every new message.
instance.last_update = v.update_id self.last_update = v.update_id
bot.on_msg_receive(instance, v.message) bot.on_msg_receive(self, v.message)
end
else
print(self.config.errors.connection)
end end
else
print(instance.config.errors.connection)
end end
end
if instance.last_cron ~= os.date('%M') then -- Run cron jobs every minute. if self.last_cron ~= os.date('%M') then -- Run cron jobs every minute.
instance.last_cron = os.date('%M') self.last_cron = os.date('%M')
utilities.save_data(instance.info.username..'.db', instance.database) -- Save the database. utilities.save_data(self.info.username..'.db', self.database) -- Save the database.
for i,v in ipairs(instance.plugins) do for i,v in ipairs(self.plugins) do
if v.cron then -- Call each plugin's cron function, if it has one. if v.cron then -- Call each plugin's cron function, if it has one.
local res, err = pcall(function() v.cron(instance) end) local res, err = pcall(function() v.cron(self) end)
if not res then if not res then
utilities.handle_exception(instance, err, 'CRON: ' .. i) utilities.handle_exception(self, err, 'CRON: ' .. i)
end
end end
end end
end end
end end
-- Save the database before exiting.
utilities.save_data(self.info.username..'.db', self.database)
print('Halted.')
end end
-- Save the database before exiting. return bot
utilities.save_data(instance.info.username..'.db', instance.database)
print('Halted.')

View File

@ -4,8 +4,6 @@ return {
bot_api_key = '', bot_api_key = '',
-- Your Telegram ID. -- Your Telegram ID.
admin = 00000000, admin = 00000000,
-- Differences, in seconds, between your time and UTC.
time_offset = 0,
-- Two-letter language code. -- Two-letter language code.
lang = 'en', lang = 'en',
-- The channel, group, or user to send error reports to. -- 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. plugins = { -- To enable a plugin, add its name to the list.
'control.lua', 'control',
'blacklist.lua', 'blacklist',
'about.lua', 'about',
'ping.lua', 'ping',
'whoami.lua', 'whoami',
'nick.lua', 'nick',
'echo.lua', 'echo',
'gSearch.lua', 'gSearch',
'gMaps.lua', 'gMaps',
'wikipedia.lua', 'wikipedia',
'hackernews.lua', 'hackernews',
'imdb.lua', 'imdb',
'calc.lua', 'calc',
'urbandictionary.lua', 'urbandictionary',
'time.lua', 'time',
'eightball.lua', 'eightball',
'dice.lua', 'dice',
'reddit.lua', 'reddit',
'xkcd.lua', 'xkcd',
'slap.lua', 'slap',
'commit.lua', 'commit',
'pun.lua', 'pun',
'currency.lua', 'currency',
'cats.lua', 'cats',
'shout.lua', 'shout',
'patterns.lua', 'patterns',
-- Put new plugins above this line. -- Put new plugins above this line.
'help.lua', 'help',
'greetings.lua' 'greetings'
} }
} }

View File

@ -1,7 +1,7 @@
#!/bin/sh #!/bin/sh
while true; do while true; do
lua bot.lua lua main.lua
echo 'otouto has stopped. ^C to exit.' echo 'otouto has stopped. ^C to exit.'
sleep 5s sleep 5s
done done

5
main.lua Normal file
View File

@ -0,0 +1,5 @@
local bot = require('bot')
local instance = {}
return bot.run(instance)

View File

@ -324,7 +324,7 @@ function administration.init_command(self_)
if self.admin_temp.flood[msg.chat.id_str][msg.from.id_str] > 99 then if self.admin_temp.flood[msg.chat.id_str][msg.from.id_str] > 99 then
drua.kick_user(msg.chat.id, msg.from.id) drua.kick_user(msg.chat.id, msg.from.id)
local output = administration.flags[5].kicked:gsub('GROUPNAME', msg.chat.title) 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 self.admin_temp.flood[msg.chat.id_str][msg.from.id_str] = nil
return return
end end
@ -372,7 +372,7 @@ function administration.init_command(self_)
else else
group.name = msg.new_chat_title group.name = msg.new_chat_title
if group.grouptype == 'supergroup' then if group.grouptype == 'supergroup' then
administration.update_desc(msg.chat.id) administration.update_desc(self, msg.chat.id)
end end
end end
return return
@ -799,7 +799,7 @@ function administration.init_command(self_)
local status = group.flags[i] or false local status = group.flags[i] or false
output = output .. '`[' .. i .. ']` *' .. v.name .. '*` = ' .. tostring(status) .. '`\n' .. v.desc .. '\n' output = output .. '`[' .. i .. ']` *' .. v.name .. '*` = ' .. tostring(status) .. '`\n' .. v.desc .. '\n'
end end
bindings.sendMessage(msg.chat.id, output, true, nil, true) bindings.sendMessage(self, msg.chat.id, output, true, nil, true)
return return
end end
if group.flags[input] == true then if group.flags[input] == true then
@ -987,7 +987,7 @@ function administration.init_command(self_)
action = function(self, msg) action = function(self, msg)
if self.database.administration.groups[msg.chat.id_str] then 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 return
end end
self.database.administration.groups[msg.chat.id_str] = { self.database.administration.groups[msg.chat.id_str] = {

View File

@ -10,7 +10,7 @@ Repeats a string of text.
```]] ```]]
function echo:init() 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 end
function echo:action(msg) function echo:action(msg)

View File

@ -6,24 +6,27 @@ local help = {}
local bindings = require('bindings') local bindings = require('bindings')
local utilities = require('utilities') local utilities = require('utilities')
local help_text = '*Available commands:*' local help_text
function help:init() function help:init()
help_text = '*Available commands:*'
for _,plugin in ipairs(self.plugins) do for _,plugin in ipairs(self.plugins) do
if plugin.command then if plugin.command then
help_text = help_text .. '\n• /' .. plugin.command:gsub('%[', '\\[') help_text = help_text .. '\n• /' .. plugin.command:gsub('%[', '\\[')
end end
end end
help.triggers = utilities.triggers(self.info.username):t('help', true):t('h', true).table help_text = help_text .. [[
end
help_text = help_text .. [[
/help <command> /help <command>
Arguments: <required> \[optional] Arguments: <required> \[optional]
]] ]]
help.triggers = utilities.triggers(self.info.username):t('help', true):t('h', true).table
end
function help:action(msg) function help:action(msg)
local input = utilities.input(msg.text_lower) local input = utilities.input(msg.text_lower)

View File

@ -19,7 +19,7 @@ function luarun:action(msg)
return return
end end
local output = loadstring(input)() local output = loadstring('local utilities = require(\'utilities\'); return function (instance) '..input..' end')()(self)
if output == nil then if output == nil then
output = 'Done!' output = 'Done!'
elseif type(output) == 'table' then elseif type(output) == 'table' then

View File

@ -268,12 +268,12 @@ function moderation:init()
moderation.triggers = {} moderation.triggers = {}
for trigger,_ in pairs(commands) do for trigger,_ in pairs(commands) do
if trigger[-1] == '$' then 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 else
moderation.triggers:insert(trigger..'%s+[^%s]*') tables.insert(moderation.triggers, trigger..'%s+[^%s]*')
moderation.triggers:insert(trigger..'@'..self.info.username..'%s+[^%s]*') tables.insert(moderation.triggers, trigger..'@'..self.info.username..'%s+[^%s]*')
moderation.triggers:insert(trigger..'$') tables.insert(moderation.triggers, trigger..'$')
moderation.triggers:insert(trigger..'@'..self.info.username..'$') tables.insert(moderation.triggers, trigger..'@'..self.info.username..'$')
end end
end end
end end

View File

@ -10,7 +10,7 @@ Set your nickname. Use "/nick --" to delete it.
```]] ```]]
function nick:init() 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 end
function nick:action(msg) function nick:action(msg)

View File

@ -28,8 +28,8 @@ function reactions:init()
reactions.triggers = utilities.triggers(self.info.username):t('reactions').table reactions.triggers = utilities.triggers(self.info.username):t('reactions').table
for trigger,reaction in pairs(mapping) do for trigger,reaction in pairs(mapping) do
help = help .. '' .. trigger:gsub('.%?', '') .. ': ' .. reaction .. '\n' help = help .. '' .. trigger:gsub('.%?', '') .. ': ' .. reaction .. '\n'
reactions.triggers:insert(utilities.INVOCATION_PATTERN..trigger) table.insert(reactions.triggers, utilities.INVOCATION_PATTERN..trigger)
reactions.triggers:insert(utilities.INVOCATION_PATTERN..trigger..'@'..self.username) table.insert(reactions.triggers, utilities.INVOCATION_PATTERN..trigger..'@'..self.username)
end end
end end

View File

@ -4,7 +4,7 @@ local bindings = require('bindings')
local utilities = require('utilities') local utilities = require('utilities')
function shell:init() 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 end
function shell:action(msg) function shell:action(msg)

View File

@ -43,7 +43,9 @@ function time:action(msg)
local jdat = JSON.decode(jstr) 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 local utcoff = (jdat.rawOffset + jdat.dstOffset) / 3600
if utcoff == math.abs(utcoff) then if utcoff == math.abs(utcoff) then
utcoff = '+' .. utcoff utcoff = '+' .. utcoff

View File

@ -17,7 +17,7 @@ function whoami:action(msg)
if msg.reply_to_message then if msg.reply_to_message then
msg = msg.reply_to_message 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 end
local chat_id = math.abs(msg.chat.id) local chat_id = math.abs(msg.chat.id)

View File

@ -276,19 +276,20 @@ end
utilities.INVOCATION_PATTERN = '/' utilities.INVOCATION_PATTERN = '/'
utilities.triggers_metatable = {} utilities.triggers_meta = {}
function utilities.triggers_metatable:t(pattern, has_args) utilities.triggers_meta.__index = utilities.triggers_meta
self.table:insert('^'..utilities.INVOCATION_PATTERN..pattern..'$') function utilities.triggers_meta:t(pattern, has_args)
self.table:insert('^'..utilities.INVOCATION_PATTERN..pattern..'@'..self.username..'$') table.insert(self.table, '^'..utilities.INVOCATION_PATTERN..pattern..'$')
table.insert(self.table, '^'..utilities.INVOCATION_PATTERN..pattern..'@'..self.username..'$')
if has_args then if has_args then
self.table:insert('^'..utilities.INVOCATION_PATTERN..pattern..'%s+[^%s]*') table.insert(self.table, '^'..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..'@'..self.username..'%s+[^%s]*')
end end
return self return self
end end
function utilities.triggers(username, trigger_table) function utilities.triggers(username, trigger_table)
local self = setmetatable({}, utilities.triggers_metatable) local self = setmetatable({}, utilities.triggers_meta)
self.username = username self.username = username
self.table = trigger_table or {} self.table = trigger_table or {}
return self return self
@ -331,3 +332,5 @@ function utilities.enrich_message(msg)
end end
return msg return msg
end end
return utilities