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
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

54
bot.lua
View File

@ -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

View File

@ -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'
}
}

View File

@ -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

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
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] = {

View File

@ -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)

View File

@ -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 <command>
Arguments: <required> \[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)

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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)

View File

@ -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