added cron jobs for plugins

added example plugin with documentation
added liberbot-compliant flood control
	see Liberbot Support for details on getting compliant
added Kickass Torrent plugin
various bugfixes
all files seem to have been marked changed due to a shift in platform
	I will do a clean clone and testing to ensure there is no issue.
This commit is contained in:
topkecleon 2015-08-28 23:15:01 -07:00
parent 2e01ceec71
commit 60570e90f3
54 changed files with 281 additions and 66 deletions

0
.gitignore vendored Normal file → Executable file
View File

0
LICENSE Normal file → Executable file
View File

0
README.md Normal file → Executable file
View File

2
bindings.lua Normal file → Executable file
View File

@ -11,7 +11,7 @@ local function send_request(url)
local tab = JSON.decode(dat)
if res ~= 200 then
print('Connection error.')
print('','Connection error.\n',url)
return false
end

60
bot.lua Normal file → Executable file
View File

@ -1,16 +1,14 @@
-- bot.lua
-- Run this!
HTTP = require('socket.http')
HTTPS = require('ssl.https')
URL = require('socket.url')
HTTPS= require('ssl.https')
URL = require('socket.url')
JSON = require('dkjson')
VERSION = 2.8
VERSION = 2.9
function on_msg_receive(msg)
if config.blacklist[tostring(msg.from.id)] then return end
if blacklist[tostring(msg.from.id)] then return end
if floodcontrol[-msg.chat.id] then return end
msg = process_msg(msg)
@ -29,7 +27,7 @@ function on_msg_receive(msg)
v.action(msg)
end)
if not a then
print(b)
print('',msg.text,'\n',b) -- For debugging purposes.
send_msg(msg, b)
end
end
@ -38,53 +36,40 @@ function on_msg_receive(msg)
end
function bot_init()
require('utilities')
print('\nLoading configuration...')
print('Loading configuration...')
config = dofile('config.lua')
print(#config.plugins .. ' plugins enabled.')
require('bindings')
require('utilities')
blacklist = load_data('blacklist.json')
print('\nFetching bot information...')
print('Fetching bot information...')
bot = get_me().result
if not bot then
error('Failure fetching bot information.')
end
for k,v in pairs(bot) do
print('',k,v)
end
print('Bot information retrieved!\n')
print('Loading plugins...')
plugins = {}
for i,v in ipairs(config.plugins) do
print('',v)
local p = dofile('plugins/'..v)
table.insert(plugins, p)
end
print('Done! Plugins loaded: ' .. #plugins .. '\n')
print('Generating help message...')
print('Plugins loaded: ' .. #plugins .. '. Generating help message...')
help_message = ''
for i,v in ipairs(plugins) do
if v.doc then
local a = string.sub(v.doc, 1, string.find(v.doc, '\n')-1)
print(a)
help_message = help_message .. ' - ' .. a .. '\n'
end
end
print('Help message generated!\n')
print('username: @'..bot.username)
print('name: '..bot.first_name)
print('ID: '..bot.id)
print('@'.. bot.username ..', AKA '.. bot.first_name ..' ('.. bot.id ..')')
is_started = true
@ -92,12 +77,18 @@ end
function process_msg(msg)
if msg.text and msg.reply_to_message and string.sub(msg.text, 1, 1) ~= '/' and msg.reply_to_message.from.id == bot.id then
msg.text = bot.first_name .. ', ' .. msg.text
end
if msg.new_chat_participant and msg.new_chat_participant.id ~= bot.id then
if msg.from.id == 100547061 then return msg end
msg.text = 'hi '..bot.first_name
msg.from = msg.new_chat_participant
end
if msg.left_chat_participant and msg.left_chat_participant.id ~= bot.id then
if msg.from.id == 100547061 then return msg end
msg.text = 'bye '..bot.first_name
msg.from = msg.left_chat_participant
end
@ -111,8 +102,8 @@ function process_msg(msg)
end
bot_init()
reminders = {}
last_update = 0
while is_started do
local res = get_updates(last_update+1)
@ -127,13 +118,16 @@ while is_started do
end
end
for i,v in pairs(reminders) do
if os.time() > v.alarm then
local a = send_message(v.chat_id, 'Reminder: '..v.text)
if a then
table.remove(reminders, i)
-- cron-like thing
-- run PLUGIN.cron() every five seconds
if os.date('%S', os.time()) % 5 == 0 then -- Only check every five seconds.
for k,v in pairs(plugins) do
if v.cron then
pcall(function() v.cron() end)
end
end
end
end
print('Halted.')

5
config.lua.default Normal file → Executable file
View File

@ -23,6 +23,7 @@ return {
'wikipedia.lua',
'imdb.lua',
'urbandictionary.lua',
'kickass.lua',
'hackernews.lua',
'cats.lua',
'time.lua',
@ -48,12 +49,12 @@ return {
'translate.lua',
'currency.lua',
'blacklist.lua',
'nick.lua'
'nick.lua',
'floodcontrol.lua'
},
moderation = {
realm = -000,
realmname = 'Realm name or ident',
data = 'moderation.json',
admins = {
['000'] = 'nickname',
}

0
dkjson.lua Normal file → Executable file
View File

47
example-plugin.lua Executable file
View File

@ -0,0 +1,47 @@
local doc = [[
/example <required> [optional]
Info about the plugin goes here.
]]
local triggers = {
'^/example',
'^/e '
}
local action = function(msg) do
-- do stuff
end
local cron = function() do
-- do stuff
end
return {
doc = doc,
triggers = triggers,
action = action,
cron = cron,
typing = true
}
--[[
Here's an example plugin.
"doc" is a string. It contains info on the plugin's usage.
The first line should be only the command and its expected arguments. Arguments should be encased in <> if they are required, and [] if they are optional.
The entire thing is sent as a message when "/help example" is used.
"triggers" is a table of triggers. A trigger is a string that should pattern-match the desired input.
"action" is the main function. It's what the plugin does. It takes a single argument, msg, which is a table of the contents of a message.
"cron" is another function. It is run every five seconds without regard to triggers or messages.
"typing" is a boolean. Set it to true if you want the bot to send a typing notification when the plugin is triggered.
]]--

0
loc/en.lua Normal file → Executable file
View File

0
plugins/8ball.lua Normal file → Executable file
View File

0
plugins/about.lua Normal file → Executable file
View File

0
plugins/admin.lua Normal file → Executable file
View File

0
plugins/bandersnatch.lua Normal file → Executable file
View File

0
plugins/bible.lua Normal file → Executable file
View File

15
plugins/blacklist.lua Normal file → Executable file
View File

@ -11,26 +11,29 @@ local action = function(msg)
return send_msg(msg, 'Permission denied.')
end
local name
local input = get_input(msg.text)
if not input then
if msg.reply_to_message then
input = msg.reply_to_message.from.id
name = msg.reply_to_message.from.first_name
else
return send_msg(msg, 'Must be used via reply or by specifying a user\'s ID.')
end
end
local id = tostring(input)
if not name then name = id end
if config.blacklist[id] then
config.blacklist[id] = nil
send_message(msg.chat.id, 'User has been removed from the blacklist.')
if blacklist[id] then
blacklist[id] = nil
send_message(msg.chat.id, name .. ' has been removed from the blacklist.')
else
config.blacklist[id] = true
send_message(msg.chat.id, 'User has been blacklisted.')
blacklist[id] = true
send_message(msg.chat.id, name .. ' has been blacklisted.')
end
save_data('blacklist.json', config.blacklist)
save_data('blacklist.json', blacklist)
end

0
plugins/btc.lua Normal file → Executable file
View File

0
plugins/calc.lua Normal file → Executable file
View File

0
plugins/cats.lua Normal file → Executable file
View File

0
plugins/chatter.lua Normal file → Executable file
View File

0
plugins/commit.lua Normal file → Executable file
View File

0
plugins/currency.lua Normal file → Executable file
View File

0
plugins/dice.lua Normal file → Executable file
View File

0
plugins/dogify.lua Normal file → Executable file
View File

0
plugins/echo.lua Normal file → Executable file
View File

46
plugins/floodcontrol.lua Executable file
View File

@ -0,0 +1,46 @@
floodcontrol = {}
local triggers = {
'/floodcontrol'
}
local action = function(msg)
local input, output
if msg.from.id ~= 100547061 then -- Only acknowledge Liberbot.
if not config.admins[msg.from.id] then -- or an admin. :)
return
end
end
input = get_input(msg.text) -- Remove the first word from the input.
input = JSON.decode(input) -- Parse the JSON into a table.
if not input.groupid then return end -- If no group is specified, end.
if not input.duration then -- If no duration is specified, set it to 5min.
input.duration = 600
end
floodcontrol[input.groupid] = os.time() + input.duration
local s = input.groupid .. ' silenced for ' .. input.duration .. ' seconds.'
send_message(-34496439, s)
end
local cron = function()
for k,v in pairs(floodcontrol) do
if os.time() > v then
floodcontrol[k] = nil
end
end
end
return {
triggers = triggers,
action = action,
cron = cron
}

0
plugins/fortune.lua Normal file → Executable file
View File

0
plugins/gImages.lua Normal file → Executable file
View File

0
plugins/gMaps.lua Normal file → Executable file
View File

0
plugins/gSearch.lua Normal file → Executable file
View File

3
plugins/giphy.lua Normal file → Executable file
View File

@ -43,6 +43,9 @@ function PLUGIN.action(msg)
return send_msg(msg, config.locale.errors.connection)
end
local jdat = JSON.decode(jstr)
if #jdat.data == 0 then
return send_msg(msg, config.locale.errors.results)
end
result_url = jdat.data[math.random(#jdat.data)].images.original.url
end

0
plugins/hackernews.lua Normal file → Executable file
View File

0
plugins/help.lua Normal file → Executable file
View File

0
plugins/hex.lua Normal file → Executable file
View File

0
plugins/imdb.lua Normal file → Executable file
View File

0
plugins/interactions.lua Normal file → Executable file
View File

69
plugins/kickass.lua Executable file
View File

@ -0,0 +1,69 @@
-- Kickass Torrents
-- Based on @Imandaneshi's torrent.lua
-- https://github.com/Imandaneshi/Jack/blob/master/plugins/torrent.lua
local doc = [[
/torrent <query>
Search Kickass Torrents. Results may be NSFW.
]]
local triggers = {
'^/torrent',
'^/kickass'
}
local action = function(msg)
local url = 'http://kat.cr/json.php?q='
local input = get_input(msg.text)
if not input then
return send_msg(msg, doc)
end
local jstr, res = HTTP.request(url..URL.escape(input))
if res ~= 200 then
return send_msg(msg, config.locale.errors.connection)
end
local jdat = JSON.decode(jstr)
if #jdat.total_results == 0 then
return send_msg(msg, config.locale.errors.results)
end
local limit = 4 -- If the request is made in a PM, send 8 results instead of 4.
if msg.chat.id == msg.from.id then
limit = 8
end
if #jdat.total_results < limit then -- If there are not that many results, do as many as possible.
limit = #jdat.total_results
end
for i,v in ipairs(jdat.list) do -- Remove any entries that have zero seeds.
if v.seeds == 0 then
table.remove(jdat.list, i)
end
end
if #jdat.list == 0 then
return send_msg(msg, config.locale.errors.results)
end
local message = ''
for i = 1, limit do
local torrenturl = jdat.list[i].torrentLink:sub(1, jdat.list[i].torrentLink:find('?')-1) -- Clean up the torrent link.
message = message .. jdat.list[i].title .. '\n' .. jdat.list[i].category .. ' | ' .. string.format('%.3f', jdat.list[i].size/1000000) .. 'MB | ' .. jdat.list[i].seeds .. 'S/' .. jdat.list[i].peers .. 'L\n' .. torrenturl .. '\n\n'
end
message = message:gsub('&amp;', '&')
send_msg(msg, message)
end
return {
doc = doc,
triggers = triggers,
action = action,
typing = true
}

3
plugins/lastfm.lua Normal file → Executable file
View File

@ -72,6 +72,7 @@ function PLUGIN.action(msg)
message = '🎵 ' .. msg.from.first_name .. ' is listening to:\n'
end
local name = jdat.name or 'Unknown'
local artist
if jdat.artist then
artist = jdat.artist['#text']
@ -79,7 +80,7 @@ function PLUGIN.action(msg)
artist = 'Unknown'
end
local message = message .. jdat.name .. ' - ' .. jdat.artist['#text']
local message = message .. name .. ' - ' .. artist
send_message(msg.chat.id, message)

0
plugins/lmgtfy.lua Normal file → Executable file
View File

26
plugins/moderation.lua Normal file → Executable file
View File

@ -15,7 +15,7 @@ help.trigger = '^/modhelp'
help.action = function(msg)
local data = load_data(config.moderation.data)
local data = load_data('moderation.json')
local do_send = false
if data[tostring(msg.chat.id)] and data[tostring(msg.chat.id)][tostring(msg.from.id)] then do_send = true end
@ -47,7 +47,7 @@ ban.trigger = '^/modban'
ban.action = function(msg)
local data = load_data(config.moderation.data)
local data = load_data('moderation.json')
if not data[tostring(msg.chat.id)] then return end
if not data[tostring(msg.chat.id)][tostring(msg.from.id)] then
@ -84,7 +84,7 @@ kick.trigger = '^/modkick'
kick.action = function(msg)
local data = load_data(config.moderation.data)
local data = load_data('moderation.json')
if not data[tostring(msg.chat.id)] then return end
if not data[tostring(msg.chat.id)][tostring(msg.from.id)] then
@ -121,7 +121,7 @@ add.trigger = '^/[mod]*add$'
add.action = function(msg)
local data = load_data(config.moderation.data)
local data = load_data('moderation.json')
if not config.moderation.admins[tostring(msg.from.id)] then return end
@ -130,7 +130,7 @@ add.action = function(msg)
end
data[tostring(msg.chat.id)] = {}
save_data(config.moderation.data, data)
save_data('moderation.json', data)
send_message(msg.chat.id, 'Group has been added.')
@ -143,7 +143,7 @@ rem.trigger = '^/[mod]*rem[ove]*$'
rem.action = function(msg)
local data = load_data(config.moderation.data)
local data = load_data('moderation.json')
if not config.moderation.admins[tostring(msg.from.id)] then return end
@ -152,7 +152,7 @@ rem.action = function(msg)
end
data[tostring(msg.chat.id)] = nil
save_data(config.moderation.data, data)
save_data('moderation.json', data)
send_message(msg.chat.id, 'Group has been removed.')
@ -165,7 +165,7 @@ promote.trigger = '^/[mod]*prom[ote]*$'
promote.action = function(msg)
local data = load_data(config.moderation.data)
local data = load_data('moderation.json')
local chatid = tostring(msg.chat.id)
if not config.moderation.admins[tostring(msg.from.id)] then return end
@ -193,7 +193,7 @@ promote.action = function(msg)
end
data[chatid][targid] = msg.reply_to_message.from.first_name
save_data(config.moderation.data, data)
save_data('moderation.json', data)
send_message(msg.chat.id, msg.reply_to_message.from.first_name..' has been promoted.')
@ -206,7 +206,7 @@ demote.trigger = '^/[mod]*dem[ote]*'
demote.action = function(msg)
local data = load_data(config.moderation.data)
local data = load_data('moderation.json')
if not config.moderation.admins[tostring(msg.from.id)] then return end
@ -228,7 +228,7 @@ demote.action = function(msg)
end
data[tostring(msg.chat.id)][tostring(input)] = nil
save_data(config.moderation.data, data)
save_data('moderation.json', data)
send_message(msg.chat.id, input..' has been demoted.')
@ -241,7 +241,7 @@ broadcast.trigger = '^/modcast'
broadcast.action = function(msg)
local data = load_data(config.moderation.data)
local data = load_data('moderation.json')
if not config.moderation.admins[tostring(msg.from.id)] then return end
@ -268,7 +268,7 @@ modlist.trigger = '^/modlist'
modlist.action = function(msg)
local data = load_data(config.moderation.data)
local data = load_data('moderation.json')
if not data[tostring(msg.chat.id)] then
return send_message(msg.chat.id, 'Group is not added.')

15
plugins/nick.lua Normal file → Executable file
View File

@ -1,6 +1,7 @@
local doc = [[
/nick <nickname>
Set your nickname for the bot to call you.
Use -- to clear your nickname.
]]
local triggers = {
@ -13,9 +14,21 @@ local action = function(msg)
local id = tostring(msg.from.id)
local input = get_input(msg.text)
if not input then
return send_msg(msg, doc)
local message = ''
if data[id] then
message = '\nYour nickname is currently ' .. data[id] .. '.'
end
return send_msg(msg, doc..message)
end
if input == '--' then
data[id] = nil
save_data('nicknames.json', data)
send_msg(msg, 'Your nickname has been deleted.')
return
end
input = input:sub(1,64):gsub('\n',' ')
data[id] = input
save_data('nicknames.json', data)
send_msg(msg, 'Your nickname has been set to ' .. input .. '.')

0
plugins/pokedex.lua Normal file → Executable file
View File

0
plugins/pun.lua Normal file → Executable file
View File

0
plugins/reaction.lua Normal file → Executable file
View File

0
plugins/reddit.lua Normal file → Executable file
View File

39
plugins/remind.lua Normal file → Executable file
View File

@ -1,20 +1,20 @@
local PLUGIN = {}
reminders = load_data('reminders.json')
PLUGIN.doc = [[
local doc = [[
/remind <delay> <message>
Set a reminder for yourself. First argument is the number of minutes until you wish to be reminded.
]]
PLUGIN.triggers = {
local triggers = {
'^/remind$',
'^/remind '
}
function PLUGIN.action(msg)
local action = function(msg)
local input = get_input(msg.text)
if not input then
return send_msg(msg, PLUGIN.doc)
return send_msg(msg, doc)
end
local delay = first_word(input)
@ -39,6 +39,7 @@ function PLUGIN.action(msg)
}
table.insert(reminders, reminder)
save_data('reminders.json', reminders)
if delay <= 1 then
delay = (delay * 60) .. ' seconds'
@ -52,4 +53,30 @@ function PLUGIN.action(msg)
end
return PLUGIN
local cron = function()
reminders = load_data('reminders.json')
for k,v in pairs(reminders) do
if os.time() > v.alarm then
local a = send_message(v.chat_id, 'Reminder: '..v.text)
if a then
reminders[k] = nil
save_data('reminders.json', reminders)
end
end
-- If the bot is no longer in the chat, he won't be able to send reminders.
-- To prevent abuse, check for old reminders and remove them.
if v.alarm < os.time() + 3600 then
reminders[k] = nil
save_data('reminders.json', reminders)
end
end
end
return {
doc = doc,
triggers = triggers,
action = action,
cron = cron
}

0
plugins/slap.lua Normal file → Executable file
View File

0
plugins/time.lua Normal file → Executable file
View File

0
plugins/translate.lua Normal file → Executable file
View File

0
plugins/urbandictionary.lua Normal file → Executable file
View File

0
plugins/weather.lua Normal file → Executable file
View File

8
plugins/whoami.lua Normal file → Executable file
View File

@ -23,6 +23,12 @@ function PLUGIN.action(msg)
msg = msg.reply_to_message
end
local nicknames = load_data('nicknames.json')
local message = ''
if nicknames[tostring(msg.from.id)] then
message = 'Hi, ' .. nicknames[tostring(msg.from.id)] .. '!\n'
end
local from_name = msg.from.first_name
if msg.from.last_name then
from_name = from_name .. ' ' .. msg.from.last_name
@ -32,7 +38,7 @@ function PLUGIN.action(msg)
end
from_name = from_name .. ' (' .. msg.from.id .. ')'
local message = 'You are ' .. from_name .. ' and you are messaging ' .. to_name .. '.'
local message = message .. 'You are ' .. from_name .. ' and you are messaging ' .. to_name .. '.'
send_msg(msg, message)

0
plugins/wikipedia.lua Normal file → Executable file
View File

9
plugins/xkcd.lua Normal file → Executable file
View File

@ -18,6 +18,7 @@ function PLUGIN.action(msg)
return send_msg(msg, config.locale.errors.connection)
end
local latest = JSON.decode(jstr).num
local jdat
if input then
url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&safe=active&q=site%3axkcd%2ecom%20' .. URL.escape(input)
@ -26,7 +27,11 @@ function PLUGIN.action(msg)
print('here')
return send_msg(msg, config.locale.errors.connection)
end
url = JSON.decode(jstr).responseData.results[1].url .. 'info.0.json'
jdat = JSON.decode(jstr)
if #jdat.responseData.results == 0 then
return send_msg(msg, config.locale.errors.results)
end
url = jdat.responseData.results[1].url .. 'info.0.json'
else
math.randomseed(os.time())
url = 'http://xkcd.com/' .. math.random(latest) .. '/info.0.json'
@ -36,7 +41,7 @@ function PLUGIN.action(msg)
if res ~= 200 then
return send_msg(msg, config.locale.errors.connection)
end
local jdat = JSON.decode(jstr)
jdat = JSON.decode(jstr)
local message = '[' .. jdat.num .. '] ' .. jdat.alt .. '\n' .. jdat.img

0
utilities.lua Normal file → Executable file
View File