otouto 3.13

good lord
This commit is contained in:
topkecleon
2016-08-13 22:26:44 -04:00
parent e19d2e1e84
commit 43a6b53c90
52 changed files with 1315 additions and 1617 deletions

View File

@ -1,18 +1,17 @@
local bot = {}
local bindings -- Bot API bindings.
local utilities -- Miscellaneous and shared plugins.
-- Requires are moved to init to allow for reloads.
local bindings -- Load Telegram bindings.
local utilities -- Load miscellaneous and cross-plugin functions.
bot.version = '3.13'
bot.version = '3.12'
function bot:init(config) -- The function run when the bot is started or reloaded.
-- Function to be run on start and reload.
function bot:init(config)
bindings = require('otouto.bindings')
utilities = require('otouto.utilities')
assert(
config.bot_api_key ~= '',
config.bot_api_key,
'You did not set your bot token in the config!'
)
self.BASE_URL = 'https://api.telegram.org/bot' .. config.bot_api_key .. '/'
@ -25,45 +24,76 @@ function bot:init(config) -- The function run when the bot is started or reloade
self.info = self.info.result
-- Load the "database"! ;)
self.database_name = config.database_name or self.info.username .. '.db'
if not self.database then
self.database = utilities.load_data(self.info.username..'.db')
self.database = utilities.load_data(self.database_name)
end
-- Migration code 1.12 -> 1.13
-- Back to administration global ban list; copy over current blacklist.
if self.database.version ~= '3.13' then
if self.database.administration then
self.database.administration.globalbans = self.database.administration.globalbans or self.database.blacklist or {}
utilities.save_data(self.database_name, self.database)
self.database = utilities.load_data(self.database_name)
end
end
-- End migration code.
-- Table to cache user info (usernames, IDs, etc).
self.database.users = self.database.users or {}
-- Table to store userdata (nicknames, lastfm usernames, etc).
self.database.userdata = self.database.userdata or {}
-- Table to store the IDs of blacklisted users.
self.database.blacklist = self.database.blacklist or {}
-- Save the bot's version in the database to make migration simpler.
self.database.version = bot.version
-- Add updated bot info to the user info cache.
self.database.users[tostring(self.info.id)] = self.info
self.plugins = {} -- Load plugins.
for _,v in ipairs(config.plugins) do
local p = require('otouto.plugins.'..v)
table.insert(self.plugins, p)
if p.init then p.init(self, config) end
if p.doc then p.doc = '```\n'..p.doc..'\n```' end
-- All plugins go into self.plugins. Plugins which accept forwarded messages
-- and messages from blacklisted users also go into self.panoptic_plugins.
self.plugins = {}
self.panoptic_plugins = {}
local t = {} -- Petty pseudo-optimization.
for _, pname in ipairs(config.plugins) do
local plugin = require('otouto.plugins.'..pname)
table.insert(self.plugins, plugin)
if plugin.init then plugin.init(self, config) end
if plugin.panoptic then table.insert(self.panoptic_plugins, plugin) end
if plugin.doc then plugin.doc = '```\n'..plugin.doc..'\n```' end
if not plugin.triggers then plugin.triggers = t end
end
print('@' .. self.info.username .. ', AKA ' .. self.info.first_name ..' ('..self.info.id..')')
self.last_update = self.last_update or 0 -- Set loop variables: Update offset,
self.last_cron = self.last_cron or os.date('%M') -- the time of the last cron job,
self.last_database_save = self.last_database_save or os.date('%H') -- the time of the last database save,
self.is_started = true -- and whether or not the bot should be running.
-- Set loop variables.
self.last_update = self.last_update or 0 -- Update offset.
self.last_cron = self.last_cron or os.date('%M') -- Last cron job.
self.last_database_save = self.last_database_save or os.date('%H') -- Last db save.
self.is_started = true
end
function bot:on_msg_receive(msg, config) -- The fn run whenever a message is received.
-- Function to be run on each new message.
function bot:on_msg_receive(msg, config)
if msg.date < os.time() - 5 then return end -- Do not process old messages.
-- Do not process old messages.
if msg.date < os.time() - 5 then return end
-- plugint is the array of plugins we'll check the message against.
-- If the message is forwarded or from a blacklisted user, the bot will only
-- check against panoptic plugins.
local plugint = self.plugins
local from_id_str = tostring(msg.from.id)
-- Cache user info for those involved.
self.database.users[tostring(msg.from.id)] = msg.from
self.database.users[from_id_str] = msg.from
if msg.reply_to_message then
self.database.users[tostring(msg.reply_to_message.from.id)] = msg.reply_to_message.from
elseif msg.forward_from then
-- Forwards only go to panoptic plugins.
plugint = self.panoptic_plugins
self.database.users[tostring(msg.forward_from.id)] = msg.forward_from
elseif msg.new_chat_member then
self.database.users[tostring(msg.new_chat_member.id)] = msg.new_chat_member
@ -71,9 +101,14 @@ function bot:on_msg_receive(msg, config) -- The fn run whenever a message is rec
self.database.users[tostring(msg.left_chat_member.id)] = msg.left_chat_member
end
-- Messages from blacklisted users only go to panoptic plugins.
if self.database.blacklist[from_id_str] then
plugint = self.panoptic_plugins
end
-- If no text, use captions.
msg.text = msg.text or msg.caption or ''
msg.text_lower = msg.text:lower()
if msg.reply_to_message then
msg.reply_to_message.text = msg.reply_to_message.text or msg.reply_to_message.caption or ''
end
@ -84,8 +119,11 @@ function bot:on_msg_receive(msg, config) -- The fn run whenever a message is rec
msg.text_lower = msg.text:lower()
end
for _, plugin in ipairs(self.plugins) do
for _, trigger in ipairs(plugin.triggers or {}) do
-- If the message is forwarded or comes from a blacklisted yser,
-- Do the thing.
for _, plugin in ipairs(plugint) do
for _, trigger in ipairs(plugin.triggers) do
if string.match(msg.text_lower, trigger) then
local success, result = pcall(function()
return plugin.action(self, msg, config)
@ -100,29 +138,29 @@ function bot:on_msg_receive(msg, config) -- The fn run whenever a message is rec
utilities.send_reply(self, msg, config.errors.generic)
end
utilities.handle_exception(self, result, msg.from.id .. ': ' .. msg.text, config)
msg = nil
return
end
-- If the action returns a table, make that table the new msg.
if type(result) == 'table' then
msg = result
-- If the action returns true, continue.
-- Continue if the return value is true.
elseif result ~= true then
msg = nil
return
end
end
end
end
msg = nil
end
-- main
function bot:run(config)
bot.init(self, config) -- Actually start the script.
while self.is_started do -- Start a loop while the bot should be running.
local res = bindings.getUpdates(self, { timeout=20, offset = self.last_update+1 } )
bot.init(self, config)
while self.is_started do
-- Update loop.
local res = bindings.getUpdates(self, { timeout = 20, offset = self.last_update + 1 } )
if res then
for _,v in ipairs(res.result) do -- Go through every new message.
-- Iterate over every new message.
for _,v in ipairs(res.result) do
self.last_update = v.update_id
if v.message then
bot.on_msg_receive(self, v.message, config)
@ -132,7 +170,8 @@ function bot:run(config)
print('Connection error while fetching updates.')
end
if self.last_cron ~= os.date('%M') then -- Run cron jobs every minute.
-- Run cron jobs every minute.
if self.last_cron ~= os.date('%M') then
self.last_cron = os.date('%M')
for i,v in ipairs(self.plugins) do
if v.cron then -- Call each plugin's cron function, if it has one.
@ -144,15 +183,14 @@ function bot:run(config)
end
end
-- Save the "database" every hour.
if self.last_database_save ~= os.date('%H') then
utilities.save_data(self.info.username..'.db', self.database) -- Save the database.
self.last_database_save = os.date('%H')
utilities.save_data(self.database_name, self.database)
end
end
-- Save the database before exiting.
utilities.save_data(self.info.username..'.db', self.database)
utilities.save_data(self.database_name, self.database)
print('Halted.')
end