added error handling

added last.fm plugin
added currency conversion plugin
fixed loop bug in gImages
echo no longer replies
added blacklist feature
more flexible moderation.lua commands
This commit is contained in:
topkecleon 2015-08-04 16:11:55 -04:00
parent 89e7906d03
commit 566086c64d
14 changed files with 145 additions and 51 deletions

2
.gitignore vendored
View File

@ -1,2 +1,4 @@
config.lua config.lua
loc/weeb.lua loc/weeb.lua
console.lua
*.json

View File

@ -33,10 +33,10 @@ end
function get_updates(offset) function get_updates(offset)
local url = BASE_URL .. 'getUpdates' local url = BASE_URL .. 'getUpdates?timeout=30'
if offset then if offset then
url = url .. '?offset=' .. offset url = url .. '&offset=' .. offset
end end
return send_request(url) return send_request(url)

23
bot.lua
View File

@ -6,20 +6,18 @@ HTTPS = require('ssl.https')
URL = require('socket.url') URL = require('socket.url')
JSON = require('dkjson') JSON = require('dkjson')
VERSION = 2.5 VERSION = 2.6
function on_msg_receive(msg) function on_msg_receive(msg)
if config.blacklist[msg.from.id] then return end
msg = process_msg(msg) msg = process_msg(msg)
if msg.date < os.time() - 5 then return end -- don't react to old messages if msg.date < os.time() - 5 then return end -- don't react to old messages
if not msg.text then return end -- don't react to media messages if not msg.text then return end -- don't react to media messages
if msg.forward_from then return end -- don't react to forwarded messages if msg.forward_from then return end -- don't react to forwarded messages
--[[
if msg.from.id == 77029297 then
send_message(msg.chat.id, '/END@MINDSTORMER619')
end
]]--
local lower = string.lower(msg.text) local lower = string.lower(msg.text)
for i,v in pairs(plugins) do for i,v in pairs(plugins) do
for j,w in pairs(v.triggers) do for j,w in pairs(v.triggers) do
@ -27,7 +25,13 @@ function on_msg_receive(msg)
if not v.no_typing then if not v.no_typing then
send_chat_action(msg.chat.id, 'typing') send_chat_action(msg.chat.id, 'typing')
end end
v.action(msg) local a,b = pcall(function() -- Janky error handling, but it works.
v.action(msg)
end)
if not a then
print(b)
send_msg(msg, b)
end
end end
end end
end end
@ -109,9 +113,9 @@ end
bot_init() bot_init()
reminders = {} reminders = {}
last_update = 0 last_update = 0
while is_started == true do while is_started do
local res = get_updates(last_update) local res = get_updates(last_update+1)
if not res then if not res then
print('Error getting updates.') print('Error getting updates.')
else else
@ -131,6 +135,5 @@ while is_started == true do
end end
end end
end end
end end
print('Halted.') print('Halted.')

View File

@ -8,6 +8,9 @@ return {
admins = { admins = {
0 0
}, },
blaclist = {
0
},
plugins = { plugins = {
'about.lua', 'about.lua',
'help.lua', 'help.lua',
@ -40,7 +43,9 @@ return {
'reaction.lua', 'reaction.lua',
'slap.lua', 'slap.lua',
'whoami.lua', 'whoami.lua',
'lmgtfy.lua' 'lmgtfy.lua',
'translate.lua',
'currency.lua'
}, },
people = { people = {
['55994550'] = 'topkecleon' ['55994550'] = 'topkecleon'

View File

@ -35,7 +35,7 @@ PLUGIN.answers = {
"There is a time and place for everything, but not now." "There is a time and place for everything, but not now."
} }
PLUGIN.yesno = {'Absolutely.', 'In your dreams.', 'Yes.', 'No.', 'Maybe.'} PLUGIN.yesno = {'Absolutely.', 'In your dreams.', 'Yes.', 'No.'}
function PLUGIN.action(msg) function PLUGIN.action(msg)

View File

@ -4,6 +4,8 @@ PLUGIN.triggers = {
'^/admin ' '^/admin '
} }
PLUGIN.no_typing = true
function PLUGIN.action(msg) function PLUGIN.action(msg)
if msg.date < os.time() - 1 then return end if msg.date < os.time() - 1 then return end
@ -12,17 +14,11 @@ function PLUGIN.action(msg)
local message = config.locale.errors.argument local message = config.locale.errors.argument
local sudo = 0 if not config.admins[msg.from.id] then
for i,v in ipairs(config.admins) do return send_msg(msg, 'Permission denied.')
if msg.from.id == v then
sudo = v
end
end end
if sudo == 0 then if string.lower(first_word(input)) == 'run' then
message = 'Permission denied.'
elseif string.lower(first_word(input)) == 'run' then
local output = get_input(input) local output = get_input(input)
if not output then if not output then

55
plugins/currency.lua Normal file
View File

@ -0,0 +1,55 @@
local doc = [[
/cash <from> <to> [amount]
Convert an amount from one currency to another.
Example: /cash USD EUR 5
]]
local triggers = {
'^/cash'
}
local action = function(msg)
local input = get_input(msg.text)
if not input then
return send_msg(msg, doc)
end
local url = 'http://www.google.com/finance/converter' -- thanks juan :^)
local from = first_word(input):upper()
local to = first_word(input, 2):upper()
local amount = first_word(input, 3)
local result
if not tonumber(amount) then
amount = 1
result = 1
end
if from ~= to then
local url = url .. '?from=' .. from .. '&to=' .. to .. '&a=' .. amount
local str, res = HTTP.request(url)
if res ~= 200 then
return send_msg(msg, config.locale.errors.connection)
end
local str = str:match('<span class=bld>.*</span>')
if not str then return send_msg(msg, config.locale.errors.results) end
local str = str:sub(str:find('>')+1)
result = str:sub(1, str:find(' ')-1)
end
local message = amount .. ' ' .. from .. ' = ' .. result .. ' ' .. to
send_msg(msg, message)
end
return {
doc = doc,
triggers = triggers,
action = action
}

View File

@ -16,7 +16,7 @@ function PLUGIN.action(msg)
return send_msg(msg, PLUGIN.doc) return send_msg(msg, PLUGIN.doc)
end end
send_msg(msg, latcyr(input)) send_message(msg.chat.id, latcyr(input))
end end

View File

@ -55,8 +55,13 @@ function PLUGIN.action(msg)
return return
end end
is_real = false local is_real = false
local counter = 0
while is_real == false do while is_real == false do
counter = counter + 1
if counter > 5 then
return send_msg(msg, config.locale.errors.results)
end
local i = math.random(#jdat.responseData.results) local i = math.random(#jdat.responseData.results)
result_url = jdat.responseData.results[i].url result_url = jdat.responseData.results[i].url
for i,v in pairs(PLUGIN.exts) do for i,v in pairs(PLUGIN.exts) do

View File

@ -48,7 +48,7 @@ function PLUGIN.action(msg)
return send_msg(msg, config.locale.errors.results) return send_msg(msg, config.locale.errors.results)
end end
message = '' local message = ''
for i = 1, #jdat.responseData.results do for i = 1, #jdat.responseData.results do
local result_url = jdat.responseData.results[i].unescapedUrl local result_url = jdat.responseData.results[i].unescapedUrl
@ -56,6 +56,8 @@ function PLUGIN.action(msg)
message = message .. ' - ' .. result_title ..'\n'.. result_url .. '\n' message = message .. ' - ' .. result_title ..'\n'.. result_url .. '\n'
end end
local message = message:gsub('&amp;', '&') -- blah
send_msg(msg, message) send_msg(msg, message)
end end

View File

@ -3,22 +3,49 @@ local PLUGIN = {}
PLUGIN.doc = [[ PLUGIN.doc = [[
/lastfm [username] /lastfm [username]
Get current- or last-played track data from last.fm. If a username is specified, it will return info for that username rather than your own. Get current- or last-played track data from last.fm. If a username is specified, it will return info for that username rather than your own.
"/fmset username" will configure your last.fm username.
]] ]]
PLUGIN.triggers = { PLUGIN.triggers = {
'^/lastfm' '^/lastfm',
'^/np$',
'^/fm$',
'^/fmset'
} }
function PLUGIN.action(msg) function PLUGIN.action(msg)
if msg.text:match('^/fmset') then
local input = get_input(msg.text)
if not input then
return send_msg(msg, PLUGIN.doc)
end
local data = load_data('lastfm.json')
local id = tostring(msg.from.id)
data[id] = input
save_data('lastfm.json', data)
send_msg(msg, 'Your last.fm username has been set to ' .. input .. '.')
return
end
local base_url = 'http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&format=json&limit=1&api_key=' .. config.lastfm_api_key .. '&user=' local base_url = 'http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&format=json&limit=1&api_key=' .. config.lastfm_api_key .. '&user='
local input = get_input(msg.text) local input = get_input(msg.text)
if not input then if not input then
if msg.from.username then local data = load_data('lastfm.json')
if data[tostring(msg.from.id)] then
input = data[tostring(msg.from.id)]
elseif msg.from.username then
input = msg.from.username input = msg.from.username
else else
return send_msg(msg, 'Please provide a valid last.fm username.') return send_msg(msg, 'Please provide a valid last.fm username.\nYou can set yours with /fmset.')
end end
end end
@ -31,7 +58,7 @@ function PLUGIN.action(msg)
local jdat = JSON.decode(jstr) local jdat = JSON.decode(jstr)
if jdat.error then if jdat.error then
return send_msg(msg, jdat.message) return send_msg(msg, 'Please provide a valid last.fm username.\nYou can set yours with /fmset.')
end end
if not jdat.recenttracks.track then if not jdat.recenttracks.track then

View File

@ -28,10 +28,10 @@ help.action = function(msg)
/modkick - Kick a user via reply or username. /modkick - Kick a user via reply or username.
/modlist - Get a list of moderators for this group. /modlist - Get a list of moderators for this group.
Administrator commands: Administrator commands:
/modadd - Add this group to the database. /add - Add this group to the database.
/modrem - Remove this group from the database. /remove - Remove this group from the database.
/modprom - Promote a user via reply. /promote - Promote a user via reply.
/moddem - Demote a user via reply. /demote - Demote a user via reply.
/modcast - Send a broastcast to every group. /modcast - Send a broastcast to every group.
]] ]]
@ -108,7 +108,7 @@ end
local add = {} local add = {}
add.trigger = '^/modadd' add.trigger = '^/[mod]*add$'
add.action = function(msg) add.action = function(msg)
@ -130,7 +130,7 @@ end
local rem = {} local rem = {}
rem.trigger = '^/modrem' rem.trigger = '^/[mod]*rem[ove]*$'
rem.action = function(msg) rem.action = function(msg)
@ -152,7 +152,7 @@ end
local promote = {} local promote = {}
promote.trigger = '^/modprom' promote.trigger = '^/[mod]*prom[ote]*$'
promote.action = function(msg) promote.action = function(msg)
@ -186,7 +186,7 @@ end
local demote = {} local demote = {}
demote.trigger = '^/moddem' demote.trigger = '^/[mod]*dem[ote]*$'
demote.action = function(msg) demote.action = function(msg)
@ -281,10 +281,10 @@ local triggers = {
'^/modhelp', '^/modhelp',
'^/modlist', '^/modlist',
'^/modcast', '^/modcast',
'^/modadd', '^/[mod]*add$',
'^/modrem', '^/[mod]*rem[ove]*$',
'^/modprom', '^/[mod]*prom[ote]*$',
'^/moddem', '^/[mod]*dem[ote]*$',
'^/modkick', '^/modkick',
'^/modban' '^/modban'
} }

View File

@ -7,7 +7,8 @@ PLUGIN.doc = [[
PLUGIN.triggers = { PLUGIN.triggers = {
'^/ud', '^/ud',
'^/urbandictionary' '^/urbandictionary',
'^/urban'
} }
function PLUGIN.action(msg) function PLUGIN.action(msg)

View File

@ -4,6 +4,7 @@ PLUGIN.doc = [[
/weather <location> /weather <location>
Returns the current temperature and weather conditions for a specified location. Returns the current temperature and weather conditions for a specified location.
Non-city locations are accepted; "/weather Buckingham Palace" will return the weather for Westminster. Non-city locations are accepted; "/weather Buckingham Palace" will return the weather for Westminster.
Results and weather data are powered by Yahoo.
]] ]]
PLUGIN.triggers = { PLUGIN.triggers = {
@ -17,21 +18,18 @@ function PLUGIN.action(msg)
return send_msg(msg, PLUGIN.doc) return send_msg(msg, PLUGIN.doc)
end end
coords = get_coords(input) local url = 'https://query.yahooapis.com/v1/public/yql?q=select%20item.condition%20from%20weather.forecast%20where%20woeid%20in%20%28select%20woeid%20from%20geo.places%281%29%20where%20text%3D%22' .. URL.escape(input) .. '%22%29&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys'
if not coords then
return send_msg(msg, config.locale.errors.results)
end
local url = 'http://api.openweathermap.org/data/2.5/weather?lat=' .. coords.lat .. '&lon=' .. coords.lon
local jstr, res = HTTP.request(url) local jstr, res = HTTP.request(url)
if res ~= 200 then if res ~= 200 then
return send_msg(msg, config.locale.errors.connection) return send_msg(msg, config.locale.errors.connection)
end end
local jdat = JSON.decode(jstr) local jdat = JSON.decode(jstr)
local data = jdat.query.results.channel.item.condition
local celsius = jdat.main.temp - 273.15 local fahrenheit = data.temp
local fahrenheit = tonumber(string.format("%.2f", celsius * (9/5) + 32)) local celsius = string.format('%.0f', (fahrenheit - 32) * 5/9)
local message = jdat.name .. ': ' .. celsius .. '°C | ' .. fahrenheit .. '°F, ' .. jdat.weather[1].description .. '.' local message = celsius .. '°C | ' .. fahrenheit .. '°F, ' .. data.text
send_msg(msg, message) send_msg(msg, message)