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
loc/weeb.lua
console.lua
*.json

View File

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

23
bot.lua
View File

@ -6,20 +6,18 @@ HTTPS = require('ssl.https')
URL = require('socket.url')
JSON = require('dkjson')
VERSION = 2.5
VERSION = 2.6
function on_msg_receive(msg)
if config.blacklist[msg.from.id] then return end
msg = process_msg(msg)
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 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)
for i,v in pairs(plugins) do
for j,w in pairs(v.triggers) do
@ -27,7 +25,13 @@ function on_msg_receive(msg)
if not v.no_typing then
send_chat_action(msg.chat.id, 'typing')
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
@ -109,9 +113,9 @@ end
bot_init()
reminders = {}
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
print('Error getting updates.')
else
@ -131,6 +135,5 @@ while is_started == true do
end
end
end
end
print('Halted.')

View File

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

View File

@ -35,7 +35,7 @@ PLUGIN.answers = {
"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)

View File

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

View File

@ -55,8 +55,13 @@ function PLUGIN.action(msg)
return
end
is_real = false
local is_real = false
local counter = 0
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)
result_url = jdat.responseData.results[i].url
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)
end
message = ''
local message = ''
for i = 1, #jdat.responseData.results do
local result_url = jdat.responseData.results[i].unescapedUrl
@ -56,6 +56,8 @@ function PLUGIN.action(msg)
message = message .. ' - ' .. result_title ..'\n'.. result_url .. '\n'
end
local message = message:gsub('&amp;', '&') -- blah
send_msg(msg, message)
end

View File

@ -3,22 +3,49 @@ local PLUGIN = {}
PLUGIN.doc = [[
/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.
"/fmset username" will configure your last.fm username.
]]
PLUGIN.triggers = {
'^/lastfm'
'^/lastfm',
'^/np$',
'^/fm$',
'^/fmset'
}
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 input = get_input(msg.text)
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
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
@ -31,7 +58,7 @@ function PLUGIN.action(msg)
local jdat = JSON.decode(jstr)
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
if not jdat.recenttracks.track then

View File

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

View File

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

View File

@ -4,6 +4,7 @@ PLUGIN.doc = [[
/weather <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.
Results and weather data are powered by Yahoo.
]]
PLUGIN.triggers = {
@ -17,21 +18,18 @@ function PLUGIN.action(msg)
return send_msg(msg, PLUGIN.doc)
end
coords = get_coords(input)
if not coords then
return send_msg(msg, config.locale.errors.results)
end
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'
local url = 'http://api.openweathermap.org/data/2.5/weather?lat=' .. coords.lat .. '&lon=' .. coords.lon
local jstr, res = HTTP.request(url)
if res ~= 200 then
return send_msg(msg, config.locale.errors.connection)
end
local jdat = JSON.decode(jstr)
local data = jdat.query.results.channel.item.condition
local celsius = jdat.main.temp - 273.15
local fahrenheit = tonumber(string.format("%.2f", celsius * (9/5) + 32))
local message = jdat.name .. ': ' .. celsius .. '°C | ' .. fahrenheit .. '°F, ' .. jdat.weather[1].description .. '.'
local fahrenheit = data.temp
local celsius = string.format('%.0f', (fahrenheit - 32) * 5/9)
local message = celsius .. '°C | ' .. fahrenheit .. '°F, ' .. data.text
send_msg(msg, message)