From 42cda22ab66c272defd5e13c3fc48662aa6182ac Mon Sep 17 00:00:00 2001 From: topkecleon Date: Sat, 11 Jul 2015 04:31:29 -0400 Subject: [PATCH] much improved personality.lua interactions partial localization support --- README.md | 2 ++ bot.lua | 10 +++++--- config.json | 1 + config.json.default | 47 ------------------------------------- loc/en_US.json | 40 +++++++++++++++++++++++++++++++ loc/weeb.json | 46 ++++++++++++++++++++++++++++++++++++ plugins/admin.lua | 4 +++- plugins/bible.lua | 2 +- plugins/btc.lua | 6 ++--- plugins/calc.lua | 2 +- plugins/dice.lua | 16 +++++-------- plugins/gImages.lua | 4 ++-- plugins/gMaps.lua | 4 ++-- plugins/gSearch.lua | 4 ++-- plugins/giphy.lua | 4 ++-- plugins/hex.lua | 2 +- plugins/imdb.lua | 2 +- plugins/personality.lua | 32 +++++++++++-------------- plugins/pokedex.lua | 4 ++-- plugins/reaction.lua | 3 ++- plugins/reddit.lua | 10 ++++---- plugins/time.lua | 5 ++-- plugins/urbandictionary.lua | 4 ++-- plugins/weather.lua | 5 ++-- plugins/whoami.lua | 2 +- plugins/xkcd.lua | 6 ++--- 26 files changed, 153 insertions(+), 114 deletions(-) delete mode 100644 config.json.default create mode 100644 loc/en_US.json create mode 100644 loc/weeb.json diff --git a/README.md b/README.md index 856984c..314c6d5 100644 --- a/README.md +++ b/README.md @@ -172,6 +172,8 @@ Add your bot API key, and other API keys if desirable. The plugins which require API keys that are not provided are disabled by default. The provided Giphy key is the public test key, and is subject to rate limitaton. +The "fortune.lua" plugin requires the fortune program to be installed on the host computer. + TIME_OFFSET is the time difference, in seconds, between your system clock. It is sometimes necessary for accurate output of the time plugin. Default at 0 (duh). "admins" table includes the ID numbers, as integers, of any privileged users. These will have access to the admin plugin and any addition privileged commands. diff --git a/bot.lua b/bot.lua index 74d3159..12ed18c 100644 --- a/bot.lua +++ b/bot.lua @@ -31,9 +31,13 @@ function bot_init() print('\nLoading configuration...') - local jstr = io.open('config.json') - local jstr = jstr:read('*all') - config = JSON.decode(jstr) + local j = io.open('config.json') + local j = j:read('*all') + config = JSON.decode(j) + local j = io.open('loc/'..config.LOCALE..'.json') + local j = j:read('*all') + locale = JSON.decode(j) + print(#config.plugins .. ' plugins enabled.') require('bindings') diff --git a/config.json b/config.json index 9bf21d5..5e602c7 100644 --- a/config.json +++ b/config.json @@ -3,6 +3,7 @@ "BIBLIA_API_KEY": "", "GIPHY_API_KEY": "dc6zaTOxFJmzC", "TIME_OFFSET": 0, + "LOCALE": "en_US", "admins": [ 0 ], diff --git a/config.json.default b/config.json.default deleted file mode 100644 index 9bf21d5..0000000 --- a/config.json.default +++ /dev/null @@ -1,47 +0,0 @@ -{ - "BOT_API_KEY": "", - "BIBLIA_API_KEY": "", - "GIPHY_API_KEY": "dc6zaTOxFJmzC", - "TIME_OFFSET": 0, - "admins": [ - 0 - ], - "plugins": [ - "about.lua", - "8ball.lua", - "admin.lua", - "bandersnatch.lua", - "btc.lua", - "calc.lua", - "chatter.lua", - "commit.lua", - "dice.lua", - "dogify.lua", - "echo.lua", - "fortune.lua", - "gImages.lua", - "giphy.lua", - "gMaps.lua", - "gSearch.lua", - "hackernews.lua", - "help.lua", - "hex.lua", - "imdb.lua", - "personality.lua", - "pokedex.lua", - "pun.lua", - "reaction.lua", - "reddit.lua", - "remind.lua", - "slap.lua", - "time.lua", - "urbandictionary.lua", - "weather.lua", - "whoami.lua", - "xkcd.lua" - ], - "people": { - "55994550": "topkecleon", - } -} - diff --git a/loc/en_US.json b/loc/en_US.json new file mode 100644 index 0000000..769d850 --- /dev/null +++ b/loc/en_US.json @@ -0,0 +1,40 @@ +{ + "hello": [ + "hello", + "hey", + "hi", + "good morning", + "good day", + "good afternoon", + "good evening" + ], + "goodbye": [ + "bye", + "later", + "see ya", + "good night" + ], + "thankyou": [ + "thanks", + "thank you" + ], + "love": [ + "love you" + ], + "hate": [ + "hate you", + "screw you", + "fuck you", + "go away" + ], + "responses": { + "hello": "Hi", + "goodbye": "Bye-bye", + "thankyou": "No problem", + "love": "You, too", + "hate": "Screw you" + }, + "conn_err": "Connection error.", + "noresults": "No results found.", + "inv_arg": "Invalid argument." +} diff --git a/loc/weeb.json b/loc/weeb.json new file mode 100644 index 0000000..883aab9 --- /dev/null +++ b/loc/weeb.json @@ -0,0 +1,46 @@ +{ + "hello": [ + "hello", + "hey", + "hi", + "good morning", + "good day", + "good afternoon", + "good evening", + "ohayou?", + "kon?'?nichiwa", + "konbanwa" + ], + "goodbye": [ + "bye", + "later", + "see ya", + "good night", + "sayou?nara", + "oyasumi" + ], + "thankyou": [ + "thanks", + "thank you", + "arigatou?" + ], + "love": [ + "love you" + ], + "hate": [ + "hate you", + "screw you", + "fuck you", + "go away" + ], + "responses": { + "hello": "Hi", + "goodbye": "Bye-bye", + "thankyou": "No problem", + "love": "You, too", + "hate": "Screw you" + }, + "conn_err": "Connection error.", + "noresults": "No results found.", + "inv_arg": "Invalid argument." +} diff --git a/plugins/admin.lua b/plugins/admin.lua index deca5f3..6c88297 100644 --- a/plugins/admin.lua +++ b/plugins/admin.lua @@ -6,9 +6,11 @@ PLUGIN.triggers = { function PLUGIN.action(msg) + if msg.date < os.time() - 1 then return end + local input = get_input(msg.text) - local message = 'Command not found.' + local message = locale.inv_arg local sudo = 0 for i,v in ipairs(config.admins) do diff --git a/plugins/bible.lua b/plugins/bible.lua index 7a735ef..0f8cac5 100644 --- a/plugins/bible.lua +++ b/plugins/bible.lua @@ -22,7 +22,7 @@ function PLUGIN.action(msg) local message, res = HTTP.request(url) if res ~= 200 then - message = 'Connection error.' + message = locale.conn_err end send_msg(msg, message) diff --git a/plugins/btc.lua b/plugins/btc.lua index c70233a..a4e94e3 100644 --- a/plugins/btc.lua +++ b/plugins/btc.lua @@ -19,7 +19,7 @@ function PLUGIN.action(msg) local jstr, res = HTTPS.request('https://api.bitcoinaverage.com/ticker/global/') if res ~= 200 then - return send_msg(msg, 'Connection error.') + return send_msg(msg, locale.conn_err) end local jdat = JSON.decode(jstr) @@ -29,7 +29,7 @@ function PLUGIN.action(msg) arg1 = string.upper(string.sub(input, 1, 3)) arg2 = string.sub(input, 5) if not tonumber(arg2) then - return send_msg(msg, 'Invalid argument.') + return send_msg(msg, locale.inv_arg) end end @@ -43,7 +43,7 @@ function PLUGIN.action(msg) if url then jstr, b = HTTPS.request(url) else - return send_msg(msg, 'Error: Currency not found.') + return send_msg(msg, locale.noresults) end jdat = JSON.decode(jstr) diff --git a/plugins/calc.lua b/plugins/calc.lua index 77de397..152e9dc 100644 --- a/plugins/calc.lua +++ b/plugins/calc.lua @@ -20,7 +20,7 @@ function PLUGIN.action(msg) local message, res = HTTP.request(url) if res ~= 200 then - return send_msg(msg, 'Connection error.') + return send_msg(msg, locale.conn_err) end send_msg(msg, message) diff --git a/plugins/dice.lua b/plugins/dice.lua index d5d48ba..3d700ba 100644 --- a/plugins/dice.lua +++ b/plugins/dice.lua @@ -33,10 +33,10 @@ function PLUGIN.action(msg) end range = string.sub(input, dloc+1) if not tonumber(rolls) or not tonumber(range) then - return send_msg(msg, 'Invalid syntax.') + return send_msg(msg, locale.inv_arg) end else - return send_msg(msg, 'Invalid syntax.') + return send_msg(msg, locale.inv_arg) end if tonumber(rolls) == 1 then @@ -44,19 +44,15 @@ function PLUGIN.action(msg) elseif tonumber(rolls) > 1 then results = rolls .. 'D' .. range .. ':\n' else - return send_msg(msg, 'Invalid syntax.') + return send_msg(msg, locale.inv_arg) end if tonumber(range) < 2 then - return send_msg(msg, 'Invalid syntax.') + return send_msg(msg, locale.inv_arg) end - if tonumber(rolls) > 100 then - return send_msg(msg, 'Maximum dice is 100.') - end - - if tonumber(range) > 100000 then - return send_msg(msg, 'Maximum range is 100000.') + if tonumber(rolls) > 100 or tonumber(range) > 100000 then + return send_msg(msg, 'Max 100D100000') end for i = 1, tonumber(rolls) do diff --git a/plugins/gImages.lua b/plugins/gImages.lua index 7b3af77..5b9cdb1 100644 --- a/plugins/gImages.lua +++ b/plugins/gImages.lua @@ -38,14 +38,14 @@ function PLUGIN.action(msg) local jstr, res = HTTP.request(url) if res ~= 200 then - send_msg(msg, 'Connection error.') + send_msg(msg, locale.conn_err) return end local jdat = JSON.decode(jstr) if #jdat.responseData.results < 1 then - send_msg(msg, 'No results found.') + send_msg(msg, locale.noresults) return end diff --git a/plugins/gMaps.lua b/plugins/gMaps.lua index ce76f70..731712f 100644 --- a/plugins/gMaps.lua +++ b/plugins/gMaps.lua @@ -20,13 +20,13 @@ function PLUGIN.action(msg) local jstr, res = HTTP.request(url) if res ~= 200 then - return send_msg(msg, 'Connection error.') + return send_msg(msg, locale.conn_err) end local jdat = JSON.decode(jstr) if jdat.status ~= 'OK' then - local message = 'Error: \"' .. input .. '\" not found.' + local message = locale.noresults return send_msg(msg, message) end diff --git a/plugins/gSearch.lua b/plugins/gSearch.lua index 3ed16ca..68ff21f 100644 --- a/plugins/gSearch.lua +++ b/plugins/gSearch.lua @@ -33,13 +33,13 @@ function PLUGIN.action(msg) local jstr, res = HTTP.request(url) if res ~= 200 then - return send_msg(msg, 'Connection error.') + return send_msg(msg, locale.conn_err) end local jdat = JSON.decode(jstr) if #jdat.responseData.results < 1 then - return send_msg(msg, 'No results found.') + return send_msg(msg, locale.noresults) end message = '' diff --git a/plugins/giphy.lua b/plugins/giphy.lua index d652ec2..459812c 100644 --- a/plugins/giphy.lua +++ b/plugins/giphy.lua @@ -30,7 +30,7 @@ function PLUGIN.action(msg) local jstr, res = HTTP.request(random_url) if res ~= 200 then - return send_msg(msg, 'Connection error.') + return send_msg(msg, locale.conn_err) end local jdat = JSON.decode(jstr) result_url = jdat.data.image_url @@ -39,7 +39,7 @@ function PLUGIN.action(msg) local jstr, res = HTTP.request(search_url .. input) if res ~= 200 then - return send_msg(msg, 'Connection error.') + return send_msg(msg, locale.conn_err) end local jdat = JSON.decode(jstr) result_url = jdat.data[math.random(#jdat.data)].images.original.url diff --git a/plugins/hex.lua b/plugins/hex.lua index 50b2c84..cc7a6eb 100644 --- a/plugins/hex.lua +++ b/plugins/hex.lua @@ -20,7 +20,7 @@ function PLUGIN.action(msg) send_msg(msg, string.format('%x', input)) else - send_msg(msg, 'Invalid number.') + send_msg(msg, locale.inv_arg) end diff --git a/plugins/imdb.lua b/plugins/imdb.lua index b1d726b..1ab2d85 100644 --- a/plugins/imdb.lua +++ b/plugins/imdb.lua @@ -21,7 +21,7 @@ function PLUGIN.action(msg) local jdat = JSON.decode(jstr) if res ~= 200 then - return send_msg(msg, 'Error connecting to server.') + return send_msg(msg, locale.conn_err) end if jdat.Response ~= 'True' then diff --git a/plugins/personality.lua b/plugins/personality.lua index ad6f448..60bba7a 100644 --- a/plugins/personality.lua +++ b/plugins/personality.lua @@ -12,7 +12,7 @@ PLUGIN.triggers = { '^i\'m back%p?$' } -function PLUGIN.action(msg) -- I WISH LUA HAD PROPER REGEX SUPPORT +function PLUGIN.action(msg) local input = string.lower(msg.text) @@ -24,24 +24,20 @@ function PLUGIN.action(msg) -- I WISH LUA HAD PROPER REGEX SUPPORT end end - if input:match('thanks,? '..bot.first_name) or input:match('thank you,? '..bot.first_name) then - return send_message(msg.chat.id, 'No problem, ' .. msg.from.first_name .. '!') - end + interactions = { + [locale.responses.hello] = locale.hello, + [locale.responses.goodbye] = locale.goodbye, + [locale.responses.thankyou] = locale.thankyou, + [locale.responses.love] = locale.love, + [locale.responses.hate] = locale.hate + } - if input:match('hello,? '..bot.first_name) or input:match('hey,? '..bot.first_name) or input:match('hi,? '..bot.first_name) then - return send_message(msg.chat.id, 'Hi, ' .. msg.from.first_name .. '!') - end - - if input:match('bye,? '..bot.first_name) or input:match('later,? '..bot.first_name) then - return send_message(msg.chat.id, 'Bye-bye, ' .. msg.from.first_name .. '!') - end - - if input:match('i hate you,? '..bot.first_name) or input:match('screw you,? '..bot.first_name) or input:match('fuck you,? '..bot.first_name) then - return send_msg(msg, '; _ ;') - end - - if string.match(input, 'i love you,? '..bot.first_name) then - return send_msg(msg, '<3') + for k,v in pairs(interactions) do + for key,val in pairs(v) do + if input:match(val..',? '..bot.first_name) then + return send_message(msg.chat.id, k..', '..msg.from.first_name..'!') + end + end end -- msg.text = '@' .. bot.username .. ', ' .. msg.text:gsub(bot.first_name, '') diff --git a/plugins/pokedex.lua b/plugins/pokedex.lua index ebef794..ae81e39 100644 --- a/plugins/pokedex.lua +++ b/plugins/pokedex.lua @@ -23,7 +23,7 @@ function PLUGIN.action(msg) local dex_url = base_url .. '/api/v1/pokemon/' .. input local dex_jstr, res = HTTP.request(dex_url) if res ~= 200 then - return send_msg(msg, 'Pokemon not found..') + return send_msg(msg, locale.noresults) end local dex_jdat = JSON.decode(dex_jstr) @@ -31,7 +31,7 @@ function PLUGIN.action(msg) local desc_url = base_url .. dex_jdat.descriptions[math.random(#dex_jdat.descriptions)].resource_uri local desc_jstr, res = HTTP.request(desc_url) if res ~= 200 then - return send_msg(msg, 'Connection error.') + return send_msg(msg, locale.conn_err) end local desc_jdat = JSON.decode(desc_jstr) diff --git a/plugins/reaction.lua b/plugins/reaction.lua index 13c5843..033b3eb 100644 --- a/plugins/reaction.lua +++ b/plugins/reaction.lua @@ -4,7 +4,8 @@ PLUGIN.triggers = { ['¯\\_(ツ)_/¯'] = '/shrug$', ['( ͡° ͜ʖ ͡°)'] = '/lenny$', ['(╯°□°)╯︵ ┻━┻'] = '/flip$', - ['┌(┌ ^o^)┐'] = '/homo$' + ['┌(┌ ^o^)┐'] = '/homo$', + ['ಠ_ಠ'] = '/look$' } function PLUGIN.action(msg) diff --git a/plugins/reddit.lua b/plugins/reddit.lua index 6704229..e60f3ac 100644 --- a/plugins/reddit.lua +++ b/plugins/reddit.lua @@ -24,11 +24,11 @@ function PLUGIN.action(msg) local url = 'http://www.reddit.com/' .. first_word(input) .. '/.json' local jstr, res = HTTP.request(url) if res ~= 200 then - return send_msg(msg, 'Connection error.') + return send_msg(msg, locale.conn_err) end jdat = JSON.decode(jstr) if #jdat.data.children == 0 then - return send_msg(msg, 'Subreddit not found.') + return send_msg(msg, locale.noresults) end else @@ -36,11 +36,11 @@ function PLUGIN.action(msg) local url = 'http://www.reddit.com/search.json?q=' .. URL.escape(input) local jstr, res = HTTP.request(url) if res ~= 200 then - return send_msg(msg, 'Connection error.') + return send_msg(msg, locale.conn_err) end jdat = JSON.decode(jstr) if #jdat.data.children == 0 then - return send_msg(msg, 'No results found.') + return send_msg(msg, locale.noresults) end end @@ -50,7 +50,7 @@ function PLUGIN.action(msg) url = 'https://www.reddit.com/.json' local jstr, res = HTTP.request(url) if res ~= 200 then - return send_msg(msg, 'Connection error.') + return send_msg(msg, locale.conn_err) end jdat = JSON.decode(jstr) diff --git a/plugins/time.lua b/plugins/time.lua index 9348388..dc9502c 100644 --- a/plugins/time.lua +++ b/plugins/time.lua @@ -20,14 +20,13 @@ function PLUGIN.action(msg) coords = get_coords(input) if not coords then - local message = 'Error: \"' .. input .. '\" not found.' - return send_msg(msg, message) + return send_msg(msg, locale.noresults) end local url = 'http://maps.googleapis.com/maps/api/timezone/json?location=' .. coords.lat ..','.. coords.lon .. '×tamp='..os.time() local jstr, res = HTTPS.request(url) if res ~= 200 then - return send_msg(msg, 'Connection error.') + return send_msg(msg, locale.conn_err) end local jdat = JSON.decode(jstr) diff --git a/plugins/urbandictionary.lua b/plugins/urbandictionary.lua index 570e2df..962d7e3 100644 --- a/plugins/urbandictionary.lua +++ b/plugins/urbandictionary.lua @@ -21,13 +21,13 @@ function PLUGIN.action(msg) local jstr, res = HTTP.request(url) if res ~= 200 then - return send_msg(msg, 'Connection error.') + return send_msg(msg, locale.conn_err) end local jdat = JSON.decode(jstr) if jdat.result_type == "no_results" then - return send_msg(msg, 'No results found.') + return send_msg(msg, locale.noresults) end message = '"' .. jdat.list[1].word .. '"\n' .. trim_string(jdat.list[1].definition) diff --git a/plugins/weather.lua b/plugins/weather.lua index 75d64b7..2894ba2 100644 --- a/plugins/weather.lua +++ b/plugins/weather.lua @@ -19,14 +19,13 @@ function PLUGIN.action(msg) coords = get_coords(input) if not coords then - local message = 'Error: \"' .. input .. '\" not found.' - return send_msg(msg, message) + return send_msg(msg, locale.noresults) end 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, 'Connection error.') + return send_msg(msg, locale.conn_err) end local jdat = JSON.decode(jstr) diff --git a/plugins/whoami.lua b/plugins/whoami.lua index 26912ab..69b23c5 100644 --- a/plugins/whoami.lua +++ b/plugins/whoami.lua @@ -1,7 +1,7 @@ local PLUGIN = {} PLUGIN.doc = [[ - /who + /whoami Get the user ID for yourself and the group. Use it in a reply to get info for the sender of the original message. ]] diff --git a/plugins/xkcd.lua b/plugins/xkcd.lua index 1578489..d8a1182 100644 --- a/plugins/xkcd.lua +++ b/plugins/xkcd.lua @@ -15,7 +15,7 @@ function PLUGIN.action(msg) local url = 'http://xkcd.com/info.0.json' local jstr, res = HTTP.request(url) if res ~= 200 then - return send_msg(msg, 'Connection error.') + return send_msg(msg, locale.conn_err) end local latest = JSON.decode(jstr).num @@ -24,7 +24,7 @@ function PLUGIN.action(msg) local jstr, res = HTTP.request(url) if res ~= 200 then print('here') - return send_msg(msg, 'Connection error.') + return send_msg(msg, locale.conn_err) end url = JSON.decode(jstr).responseData.results[1].url .. 'info.0.json' else @@ -34,7 +34,7 @@ function PLUGIN.action(msg) local jstr, res = HTTP.request(url) if res ~= 200 then - return send_msg(msg, 'Connection error.') + return send_msg(msg, locale.conn_err) end local jdat = JSON.decode(jstr)