2016-04-08 23:12:02 +02:00
|
|
|
local bot = {}
|
2016-08-14 04:26:44 +02:00
|
|
|
local bindings -- Bot API bindings.
|
|
|
|
local utilities -- Miscellaneous and shared plugins.
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-08-14 04:26:44 +02:00
|
|
|
bot.version = '3.13'
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-08-14 04:26:44 +02:00
|
|
|
-- Function to be run on start and reload.
|
|
|
|
function bot:init(config)
|
2016-04-08 23:12:02 +02:00
|
|
|
|
2016-06-07 06:31:34 +02:00
|
|
|
bindings = require('otouto.bindings')
|
|
|
|
utilities = require('otouto.utilities')
|
2016-04-29 06:36:35 +02:00
|
|
|
|
2016-05-29 19:08:39 +02:00
|
|
|
assert(
|
2016-08-14 04:26:44 +02:00
|
|
|
config.bot_api_key,
|
2016-05-27 02:26:30 +02:00
|
|
|
'You did not set your bot token in the config!'
|
2016-05-29 19:08:39 +02:00
|
|
|
)
|
2016-05-27 02:26:30 +02:00
|
|
|
self.BASE_URL = 'https://api.telegram.org/bot' .. config.bot_api_key .. '/'
|
2015-07-10 09:52:22 +02:00
|
|
|
|
2016-01-12 11:22:28 +01:00
|
|
|
-- Fetch bot information. Try until it succeeds.
|
2016-05-21 02:47:13 +02:00
|
|
|
repeat
|
|
|
|
print('Fetching bot information...')
|
|
|
|
self.info = bindings.getMe(self)
|
|
|
|
until self.info
|
2016-04-08 23:12:02 +02:00
|
|
|
self.info = self.info.result
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-03-08 14:15:48 +01:00
|
|
|
-- Load the "database"! ;)
|
2016-08-14 04:26:44 +02:00
|
|
|
self.database_name = config.database_name or self.info.username .. '.db'
|
2016-04-08 23:12:02 +02:00
|
|
|
if not self.database then
|
2016-08-14 04:26:44 +02:00
|
|
|
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
|
2016-03-08 14:15:48 +01:00
|
|
|
end
|
2016-08-14 04:26:44 +02:00
|
|
|
-- End migration code.
|
2016-03-08 14:15:48 +01:00
|
|
|
|
otouto 3.11
"things occurred"
Added some utilities (id_from_username, id_from_message), removed some utilities (latcyr, others?).
Removed cycle-wasting "shortcuts" -- no more automatic id_str or name; text_lower remains.
Moved userdata (nicknames, lastfm, etc) to a different tree in the database (automatic migration will occur). /me now returns userdata.
Speaking of migration, database now stores the latest version run to make future automigration easy.
Database now saves hourly rather than minutely.
Changed readme and some plugins to reflect above changes.
Removed broken rockspec (Brayden, feel free to re-add once it's working).
Added option to automatically block people (via drua) when blacklisted.
Fixed about.lua trigger problems.
administration 1.11 - Removed /kickme and /broadcast. Users should leave manually, and announcements should be made via channel rather than spam. /setqotd now handles forwarded messages correctly. /kick, /ban, /hammer,
/mod, /admin now support multiple arguments. Added get_targets function. No migration is necessary.
2016-07-05 09:29:11 +02:00
|
|
|
-- 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 {}
|
2016-08-14 04:26:44 +02:00
|
|
|
-- Table to store the IDs of blacklisted users.
|
|
|
|
self.database.blacklist = self.database.blacklist or {}
|
otouto 3.11
"things occurred"
Added some utilities (id_from_username, id_from_message), removed some utilities (latcyr, others?).
Removed cycle-wasting "shortcuts" -- no more automatic id_str or name; text_lower remains.
Moved userdata (nicknames, lastfm, etc) to a different tree in the database (automatic migration will occur). /me now returns userdata.
Speaking of migration, database now stores the latest version run to make future automigration easy.
Database now saves hourly rather than minutely.
Changed readme and some plugins to reflect above changes.
Removed broken rockspec (Brayden, feel free to re-add once it's working).
Added option to automatically block people (via drua) when blacklisted.
Fixed about.lua trigger problems.
administration 1.11 - Removed /kickme and /broadcast. Users should leave manually, and announcements should be made via channel rather than spam. /setqotd now handles forwarded messages correctly. /kick, /ban, /hammer,
/mod, /admin now support multiple arguments. Added get_targets function. No migration is necessary.
2016-07-05 09:29:11 +02:00
|
|
|
-- 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.
|
2016-06-07 05:13:26 +02:00
|
|
|
self.database.users[tostring(self.info.id)] = self.info
|
|
|
|
|
2016-08-14 04:26:44 +02:00
|
|
|
-- 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
|
2015-11-25 03:22:04 +01:00
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-04-08 23:12:02 +02:00
|
|
|
print('@' .. self.info.username .. ', AKA ' .. self.info.first_name ..' ('..self.info.id..')')
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-08-14 04:26:44 +02:00
|
|
|
-- 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
|
2016-03-22 11:16:26 +01:00
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-08-14 04:26:44 +02:00
|
|
|
-- Function to be run on each new message.
|
|
|
|
function bot:on_msg_receive(msg, config)
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-08-14 04:26:44 +02:00
|
|
|
-- 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)
|
otouto 3.11
"things occurred"
Added some utilities (id_from_username, id_from_message), removed some utilities (latcyr, others?).
Removed cycle-wasting "shortcuts" -- no more automatic id_str or name; text_lower remains.
Moved userdata (nicknames, lastfm, etc) to a different tree in the database (automatic migration will occur). /me now returns userdata.
Speaking of migration, database now stores the latest version run to make future automigration easy.
Database now saves hourly rather than minutely.
Changed readme and some plugins to reflect above changes.
Removed broken rockspec (Brayden, feel free to re-add once it's working).
Added option to automatically block people (via drua) when blacklisted.
Fixed about.lua trigger problems.
administration 1.11 - Removed /kickme and /broadcast. Users should leave manually, and announcements should be made via channel rather than spam. /setqotd now handles forwarded messages correctly. /kick, /ban, /hammer,
/mod, /admin now support multiple arguments. Added get_targets function. No migration is necessary.
2016-07-05 09:29:11 +02:00
|
|
|
|
2016-05-15 14:22:31 +02:00
|
|
|
-- Cache user info for those involved.
|
2016-08-14 04:26:44 +02:00
|
|
|
self.database.users[from_id_str] = msg.from
|
otouto 3.11
"things occurred"
Added some utilities (id_from_username, id_from_message), removed some utilities (latcyr, others?).
Removed cycle-wasting "shortcuts" -- no more automatic id_str or name; text_lower remains.
Moved userdata (nicknames, lastfm, etc) to a different tree in the database (automatic migration will occur). /me now returns userdata.
Speaking of migration, database now stores the latest version run to make future automigration easy.
Database now saves hourly rather than minutely.
Changed readme and some plugins to reflect above changes.
Removed broken rockspec (Brayden, feel free to re-add once it's working).
Added option to automatically block people (via drua) when blacklisted.
Fixed about.lua trigger problems.
administration 1.11 - Removed /kickme and /broadcast. Users should leave manually, and announcements should be made via channel rather than spam. /setqotd now handles forwarded messages correctly. /kick, /ban, /hammer,
/mod, /admin now support multiple arguments. Added get_targets function. No migration is necessary.
2016-07-05 09:29:11 +02:00
|
|
|
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
|
2016-08-14 04:26:44 +02:00
|
|
|
-- Forwards only go to panoptic plugins.
|
|
|
|
plugint = self.panoptic_plugins
|
otouto 3.11
"things occurred"
Added some utilities (id_from_username, id_from_message), removed some utilities (latcyr, others?).
Removed cycle-wasting "shortcuts" -- no more automatic id_str or name; text_lower remains.
Moved userdata (nicknames, lastfm, etc) to a different tree in the database (automatic migration will occur). /me now returns userdata.
Speaking of migration, database now stores the latest version run to make future automigration easy.
Database now saves hourly rather than minutely.
Changed readme and some plugins to reflect above changes.
Removed broken rockspec (Brayden, feel free to re-add once it's working).
Added option to automatically block people (via drua) when blacklisted.
Fixed about.lua trigger problems.
administration 1.11 - Removed /kickme and /broadcast. Users should leave manually, and announcements should be made via channel rather than spam. /setqotd now handles forwarded messages correctly. /kick, /ban, /hammer,
/mod, /admin now support multiple arguments. Added get_targets function. No migration is necessary.
2016-07-05 09:29:11 +02:00
|
|
|
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
|
|
|
|
elseif msg.left_chat_member then
|
|
|
|
self.database.users[tostring(msg.left_chat_member.id)] = msg.left_chat_member
|
2016-03-08 14:15:48 +01:00
|
|
|
end
|
|
|
|
|
2016-08-14 04:26:44 +02:00
|
|
|
-- 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.
|
otouto 3.11
"things occurred"
Added some utilities (id_from_username, id_from_message), removed some utilities (latcyr, others?).
Removed cycle-wasting "shortcuts" -- no more automatic id_str or name; text_lower remains.
Moved userdata (nicknames, lastfm, etc) to a different tree in the database (automatic migration will occur). /me now returns userdata.
Speaking of migration, database now stores the latest version run to make future automigration easy.
Database now saves hourly rather than minutely.
Changed readme and some plugins to reflect above changes.
Removed broken rockspec (Brayden, feel free to re-add once it's working).
Added option to automatically block people (via drua) when blacklisted.
Fixed about.lua trigger problems.
administration 1.11 - Removed /kickme and /broadcast. Users should leave manually, and announcements should be made via channel rather than spam. /setqotd now handles forwarded messages correctly. /kick, /ban, /hammer,
/mod, /admin now support multiple arguments. Added get_targets function. No migration is necessary.
2016-07-05 09:29:11 +02:00
|
|
|
msg.text = msg.text or msg.caption or ''
|
|
|
|
msg.text_lower = msg.text:lower()
|
2016-07-15 21:24:20 +02:00
|
|
|
if msg.reply_to_message then
|
|
|
|
msg.reply_to_message.text = msg.reply_to_message.text or msg.reply_to_message.caption or ''
|
|
|
|
end
|
|
|
|
|
otouto 3.11
"things occurred"
Added some utilities (id_from_username, id_from_message), removed some utilities (latcyr, others?).
Removed cycle-wasting "shortcuts" -- no more automatic id_str or name; text_lower remains.
Moved userdata (nicknames, lastfm, etc) to a different tree in the database (automatic migration will occur). /me now returns userdata.
Speaking of migration, database now stores the latest version run to make future automigration easy.
Database now saves hourly rather than minutely.
Changed readme and some plugins to reflect above changes.
Removed broken rockspec (Brayden, feel free to re-add once it's working).
Added option to automatically block people (via drua) when blacklisted.
Fixed about.lua trigger problems.
administration 1.11 - Removed /kickme and /broadcast. Users should leave manually, and announcements should be made via channel rather than spam. /setqotd now handles forwarded messages correctly. /kick, /ban, /hammer,
/mod, /admin now support multiple arguments. Added get_targets function. No migration is necessary.
2016-07-05 09:29:11 +02:00
|
|
|
-- Support deep linking.
|
2016-05-27 05:28:44 +02:00
|
|
|
if msg.text:match('^'..config.cmd_pat..'start .+') then
|
|
|
|
msg.text = config.cmd_pat .. utilities.input(msg.text)
|
2016-04-12 11:24:56 +02:00
|
|
|
msg.text_lower = msg.text:lower()
|
2016-01-08 04:30:12 +01:00
|
|
|
end
|
|
|
|
|
2016-08-14 04:26:44 +02:00
|
|
|
-- 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
|
2016-06-14 23:57:36 +02:00
|
|
|
if string.match(msg.text_lower, trigger) then
|
2015-11-25 03:22:04 +01:00
|
|
|
local success, result = pcall(function()
|
2016-06-14 23:57:36 +02:00
|
|
|
return plugin.action(self, msg, config)
|
2015-11-25 03:22:04 +01:00
|
|
|
end)
|
|
|
|
if not success then
|
2016-06-14 23:57:36 +02:00
|
|
|
-- If the plugin has an error message, send it. If it does
|
|
|
|
-- not, use the generic one specified in config. If it's set
|
|
|
|
-- to false, do nothing.
|
|
|
|
if plugin.error then
|
|
|
|
utilities.send_reply(self, msg, plugin.error)
|
|
|
|
elseif plugin.error == nil then
|
|
|
|
utilities.send_reply(self, msg, config.errors.generic)
|
|
|
|
end
|
2016-05-27 02:26:30 +02:00
|
|
|
utilities.handle_exception(self, result, msg.from.id .. ': ' .. msg.text, config)
|
2016-08-14 04:26:44 +02:00
|
|
|
msg = nil
|
2015-11-25 03:22:04 +01:00
|
|
|
return
|
2016-08-14 04:26:44 +02:00
|
|
|
-- Continue if the return value is true.
|
2015-11-25 03:22:04 +01:00
|
|
|
elseif result ~= true then
|
2016-08-14 04:26:44 +02:00
|
|
|
msg = nil
|
2015-11-25 03:22:04 +01:00
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
|
|
|
end
|
2016-08-14 04:26:44 +02:00
|
|
|
msg = nil
|
2015-07-03 00:15:52 +02:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2016-08-14 04:26:44 +02:00
|
|
|
-- main
|
2016-05-27 05:28:44 +02:00
|
|
|
function bot:run(config)
|
2016-08-14 04:26:44 +02:00
|
|
|
bot.init(self, config)
|
|
|
|
while self.is_started do
|
|
|
|
-- Update loop.
|
|
|
|
local res = bindings.getUpdates(self, { timeout = 20, offset = self.last_update + 1 } )
|
2016-05-22 09:03:50 +02:00
|
|
|
if res then
|
2016-08-14 04:26:44 +02:00
|
|
|
-- Iterate over every new message.
|
|
|
|
for _,v in ipairs(res.result) do
|
2016-05-22 09:03:50 +02:00
|
|
|
self.last_update = v.update_id
|
|
|
|
if v.message then
|
2016-05-27 02:26:30 +02:00
|
|
|
bot.on_msg_receive(self, v.message, config)
|
2016-04-14 05:48:20 +02:00
|
|
|
end
|
2016-04-08 23:12:02 +02:00
|
|
|
end
|
2016-05-22 09:03:50 +02:00
|
|
|
else
|
2016-06-07 05:13:26 +02:00
|
|
|
print('Connection error while fetching updates.')
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
|
|
|
|
2016-08-14 04:26:44 +02:00
|
|
|
-- Run cron jobs every minute.
|
|
|
|
if self.last_cron ~= os.date('%M') then
|
2016-04-14 05:48:20 +02:00
|
|
|
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.
|
2016-05-27 05:28:44 +02:00
|
|
|
local result, err = pcall(function() v.cron(self, config) end)
|
2016-05-29 19:08:39 +02:00
|
|
|
if not result then
|
2016-05-27 02:26:30 +02:00
|
|
|
utilities.handle_exception(self, err, 'CRON: ' .. i, config)
|
2016-04-14 05:48:20 +02:00
|
|
|
end
|
2016-01-13 19:00:17 +01:00
|
|
|
end
|
2015-07-15 08:15:23 +02:00
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
2016-04-14 05:48:20 +02:00
|
|
|
|
2016-08-14 04:26:44 +02:00
|
|
|
-- Save the "database" every hour.
|
otouto 3.11
"things occurred"
Added some utilities (id_from_username, id_from_message), removed some utilities (latcyr, others?).
Removed cycle-wasting "shortcuts" -- no more automatic id_str or name; text_lower remains.
Moved userdata (nicknames, lastfm, etc) to a different tree in the database (automatic migration will occur). /me now returns userdata.
Speaking of migration, database now stores the latest version run to make future automigration easy.
Database now saves hourly rather than minutely.
Changed readme and some plugins to reflect above changes.
Removed broken rockspec (Brayden, feel free to re-add once it's working).
Added option to automatically block people (via drua) when blacklisted.
Fixed about.lua trigger problems.
administration 1.11 - Removed /kickme and /broadcast. Users should leave manually, and announcements should be made via channel rather than spam. /setqotd now handles forwarded messages correctly. /kick, /ban, /hammer,
/mod, /admin now support multiple arguments. Added get_targets function. No migration is necessary.
2016-07-05 09:29:11 +02:00
|
|
|
if self.last_database_save ~= os.date('%H') then
|
|
|
|
self.last_database_save = os.date('%H')
|
2016-08-14 04:26:44 +02:00
|
|
|
utilities.save_data(self.database_name, self.database)
|
otouto 3.11
"things occurred"
Added some utilities (id_from_username, id_from_message), removed some utilities (latcyr, others?).
Removed cycle-wasting "shortcuts" -- no more automatic id_str or name; text_lower remains.
Moved userdata (nicknames, lastfm, etc) to a different tree in the database (automatic migration will occur). /me now returns userdata.
Speaking of migration, database now stores the latest version run to make future automigration easy.
Database now saves hourly rather than minutely.
Changed readme and some plugins to reflect above changes.
Removed broken rockspec (Brayden, feel free to re-add once it's working).
Added option to automatically block people (via drua) when blacklisted.
Fixed about.lua trigger problems.
administration 1.11 - Removed /kickme and /broadcast. Users should leave manually, and announcements should be made via channel rather than spam. /setqotd now handles forwarded messages correctly. /kick, /ban, /hammer,
/mod, /admin now support multiple arguments. Added get_targets function. No migration is necessary.
2016-07-05 09:29:11 +02:00
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
2016-04-14 05:48:20 +02:00
|
|
|
-- Save the database before exiting.
|
2016-08-14 04:26:44 +02:00
|
|
|
utilities.save_data(self.database_name, self.database)
|
2016-04-14 05:48:20 +02:00
|
|
|
print('Halted.')
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
2015-08-29 08:15:01 +02:00
|
|
|
|
2016-04-14 05:48:20 +02:00
|
|
|
return bot
|