diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/LICENSE b/LICENSE old mode 100644 new mode 100755 diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/bindings.lua b/bindings.lua old mode 100644 new mode 100755 index e24646a..8b7e02f --- a/bindings.lua +++ b/bindings.lua @@ -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 diff --git a/bot.lua b/bot.lua old mode 100644 new mode 100755 index d604e70..a3666d4 --- a/bot.lua +++ b/bot.lua @@ -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.') diff --git a/config.lua.default b/config.lua.default old mode 100644 new mode 100755 index f95b135..6191b83 --- a/config.lua.default +++ b/config.lua.default @@ -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', } diff --git a/dkjson.lua b/dkjson.lua old mode 100644 new mode 100755 diff --git a/example-plugin.lua b/example-plugin.lua new file mode 100755 index 0000000..90c357b --- /dev/null +++ b/example-plugin.lua @@ -0,0 +1,47 @@ +local doc = [[ + /example [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. + +]]-- diff --git a/loc/en.lua b/loc/en.lua old mode 100644 new mode 100755 diff --git a/plugins/8ball.lua b/plugins/8ball.lua old mode 100644 new mode 100755 diff --git a/plugins/about.lua b/plugins/about.lua old mode 100644 new mode 100755 diff --git a/plugins/admin.lua b/plugins/admin.lua old mode 100644 new mode 100755 diff --git a/plugins/bandersnatch.lua b/plugins/bandersnatch.lua old mode 100644 new mode 100755 diff --git a/plugins/bible.lua b/plugins/bible.lua old mode 100644 new mode 100755 diff --git a/plugins/blacklist.lua b/plugins/blacklist.lua old mode 100644 new mode 100755 index 5a4c017..be983ed --- a/plugins/blacklist.lua +++ b/plugins/blacklist.lua @@ -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 diff --git a/plugins/btc.lua b/plugins/btc.lua old mode 100644 new mode 100755 diff --git a/plugins/calc.lua b/plugins/calc.lua old mode 100644 new mode 100755 diff --git a/plugins/cats.lua b/plugins/cats.lua old mode 100644 new mode 100755 diff --git a/plugins/chatter.lua b/plugins/chatter.lua old mode 100644 new mode 100755 diff --git a/plugins/commit.lua b/plugins/commit.lua old mode 100644 new mode 100755 diff --git a/plugins/currency.lua b/plugins/currency.lua old mode 100644 new mode 100755 diff --git a/plugins/dice.lua b/plugins/dice.lua old mode 100644 new mode 100755 diff --git a/plugins/dogify.lua b/plugins/dogify.lua old mode 100644 new mode 100755 diff --git a/plugins/echo.lua b/plugins/echo.lua old mode 100644 new mode 100755 diff --git a/plugins/floodcontrol.lua b/plugins/floodcontrol.lua new file mode 100755 index 0000000..d9246cb --- /dev/null +++ b/plugins/floodcontrol.lua @@ -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 +} diff --git a/plugins/fortune.lua b/plugins/fortune.lua old mode 100644 new mode 100755 diff --git a/plugins/gImages.lua b/plugins/gImages.lua old mode 100644 new mode 100755 diff --git a/plugins/gMaps.lua b/plugins/gMaps.lua old mode 100644 new mode 100755 diff --git a/plugins/gSearch.lua b/plugins/gSearch.lua old mode 100644 new mode 100755 diff --git a/plugins/giphy.lua b/plugins/giphy.lua old mode 100644 new mode 100755 index 413afab..9c910d3 --- a/plugins/giphy.lua +++ b/plugins/giphy.lua @@ -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 diff --git a/plugins/hackernews.lua b/plugins/hackernews.lua old mode 100644 new mode 100755 diff --git a/plugins/help.lua b/plugins/help.lua old mode 100644 new mode 100755 diff --git a/plugins/hex.lua b/plugins/hex.lua old mode 100644 new mode 100755 diff --git a/plugins/imdb.lua b/plugins/imdb.lua old mode 100644 new mode 100755 diff --git a/plugins/interactions.lua b/plugins/interactions.lua old mode 100644 new mode 100755 diff --git a/plugins/kickass.lua b/plugins/kickass.lua new file mode 100755 index 0000000..c0a1452 --- /dev/null +++ b/plugins/kickass.lua @@ -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 + 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('&', '&') + + send_msg(msg, message) + +end + +return { + doc = doc, + triggers = triggers, + action = action, + typing = true +} diff --git a/plugins/lastfm.lua b/plugins/lastfm.lua old mode 100644 new mode 100755 index b126dda..452f842 --- a/plugins/lastfm.lua +++ b/plugins/lastfm.lua @@ -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) diff --git a/plugins/lmgtfy.lua b/plugins/lmgtfy.lua old mode 100644 new mode 100755 diff --git a/plugins/moderation.lua b/plugins/moderation.lua old mode 100644 new mode 100755 index 203d78f..692923a --- a/plugins/moderation.lua +++ b/plugins/moderation.lua @@ -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.') diff --git a/plugins/nick.lua b/plugins/nick.lua old mode 100644 new mode 100755 index 86a100c..c863d75 --- a/plugins/nick.lua +++ b/plugins/nick.lua @@ -1,6 +1,7 @@ local doc = [[ /nick 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 .. '.') diff --git a/plugins/pokedex.lua b/plugins/pokedex.lua old mode 100644 new mode 100755 diff --git a/plugins/pun.lua b/plugins/pun.lua old mode 100644 new mode 100755 diff --git a/plugins/reaction.lua b/plugins/reaction.lua old mode 100644 new mode 100755 diff --git a/plugins/reddit.lua b/plugins/reddit.lua old mode 100644 new mode 100755 diff --git a/plugins/remind.lua b/plugins/remind.lua old mode 100644 new mode 100755 index b342672..271bedf --- a/plugins/remind.lua +++ b/plugins/remind.lua @@ -1,20 +1,20 @@ -local PLUGIN = {} +reminders = load_data('reminders.json') -PLUGIN.doc = [[ +local doc = [[ /remind 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 +} diff --git a/plugins/slap.lua b/plugins/slap.lua old mode 100644 new mode 100755 diff --git a/plugins/time.lua b/plugins/time.lua old mode 100644 new mode 100755 diff --git a/plugins/translate.lua b/plugins/translate.lua old mode 100644 new mode 100755 diff --git a/plugins/urbandictionary.lua b/plugins/urbandictionary.lua old mode 100644 new mode 100755 diff --git a/plugins/weather.lua b/plugins/weather.lua old mode 100644 new mode 100755 diff --git a/plugins/whoami.lua b/plugins/whoami.lua old mode 100644 new mode 100755 index 69b23c5..8589379 --- a/plugins/whoami.lua +++ b/plugins/whoami.lua @@ -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) diff --git a/plugins/wikipedia.lua b/plugins/wikipedia.lua old mode 100644 new mode 100755 diff --git a/plugins/xkcd.lua b/plugins/xkcd.lua old mode 100644 new mode 100755 index 9b509fd..505e4f6 --- a/plugins/xkcd.lua +++ b/plugins/xkcd.lua @@ -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 diff --git a/utilities.lua b/utilities.lua old mode 100644 new mode 100755