From 82fe1a8dc6abba089c21cc7f5aaa863327295a29 Mon Sep 17 00:00:00 2001 From: Brayden Banks Date: Wed, 13 Jul 2016 17:00:58 -0700 Subject: [PATCH 1/2] =?UTF-8?q?Add=20Pok=C3=A9mon=20Go=20plugin.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- otouto/plugins/pokemon-go.lua | 82 +++++++++++++++++++++++++++++++++++ otouto/utilities.lua | 31 +++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 otouto/plugins/pokemon-go.lua diff --git a/otouto/plugins/pokemon-go.lua b/otouto/plugins/pokemon-go.lua new file mode 100644 index 0000000..c2b0a8b --- /dev/null +++ b/otouto/plugins/pokemon-go.lua @@ -0,0 +1,82 @@ +-- For some reason you can't have an é in variable names. :( +local pokemon_go = {} + +local utilities = require('otouto.utilities') + +pokemon_go.command = 'pokego ' + +function pokemon_go:init(config) + pokemon_go.triggers = utilities.triggers(self.info.username, config.cmd_pat) + :t('pokego', true):t('pokégo', true) + :t('pokemongo', true):t('pokémongo', true).table + pokemon_go.doc = [[``` +]]..config.cmd_pat..[[pokego +Set your Pokémon Go team for statistical purposes. The team must be valid, and can be referred to by name or color (or the first letter of either). Giving no team name will show statistics. +```]] + local db = self.database.pokemon_go + if not db then + self.database.pokemon_go = {} + db = self.database.pokemon_go + end + if not db.membership then + db.membership = {} + end +end + +local team_ref = { + mystic = "Mystic", + m = "Mystic", + valor = "Valor", + v = "Valor", + instinct = "Instinct", + i = "Instinct", + blue = "Mystic", + b = "Mystic", + red = "Valor", + r = "Valor", + yellow = "Instinct", + y = "Instinct" +} + +function pokemon_go:action(msg, config) + local output + local input = utilities.input(msg.text_lower) + + if input then + local team = team_ref[input] + if not team then + output = 'Invalid team.' + else + local id_str = tostring(msg.from.id) + local db = self.database.pokemon_go + local db_membership = db.membership + if not db_membership[team] then + db_membership[team] = utilities.new_set() + end + + for t, set in pairs(db_membership) do + if t ~= team then + set:remove(id_str) + else + set:add(id_str) + end + end + + output = 'Your team is now '..team..'.' + end + else + local db = self.database.pokemon_go + local db_membership = db.membership + + local output_temp = {'Membership:'} + for t, set in pairs(db_membership) do + table.insert(output_temp, t..': '..#set) + end + + output = table.concat(output_temp, '\n') + end + + utilities.send_reply(self, msg, output) +end + +return pokemon_go diff --git a/otouto/utilities.lua b/otouto/utilities.lua index 4fa6fba..79016e1 100755 --- a/otouto/utilities.lua +++ b/otouto/utilities.lua @@ -323,4 +323,35 @@ utilities.char = { utf_8 = '([%z\1-\127\194-\244][\128-\191]*)', } +utilities.set_meta = {} +utilities.set_meta.__index = utilities.set_meta +function utilities.new_set() + return setmetatable({__count = 0}, utilities.set_meta) +end +function utilities.set_meta:add(x) + if x == "__count" then + return false + else + if not self[x] then + self[x] = true + self.__count = self.__count + 1 + end + return true + end +end +function utilities.set_meta:remove(x) + if x == "__count" then + return false + else + if self[x] then + self[x] = nil + self.__count = self.__count - 1 + end + return true + end +end +function utilities.set_meta:__len() + return self.__count +end + return utilities From 67f7d7e12fcd5f1b04bdf35dd91a15c844d42fef Mon Sep 17 00:00:00 2001 From: topkecleon Date: Wed, 13 Jul 2016 21:57:23 -0400 Subject: [PATCH 2/2] blacklist.lua rewritten to remove toggle and support multiple arguments. The /reload command no longer has stupid, undocumented behavior (though there is now an opt-in for such stupid, undocumented behavior for those of you who like stupid, undocumented behavior). Slight modification to reactions.lua to prevent accidents. New plugin: rmspic.lua. why --- otouto/bot.lua | 2 +- otouto/plugins/blacklist.lua | 112 +++++++++++++++++++++++++++-------- otouto/plugins/control.lua | 8 ++- otouto/plugins/reactions.lua | 11 +++- otouto/plugins/rmspic.lua | 34 +++++++++++ 5 files changed, 135 insertions(+), 32 deletions(-) create mode 100644 otouto/plugins/rmspic.lua diff --git a/otouto/bot.lua b/otouto/bot.lua index ea83a76..6569714 100755 --- a/otouto/bot.lua +++ b/otouto/bot.lua @@ -95,7 +95,7 @@ function bot:on_msg_receive(msg, config) -- The fn run whenever a message is rec end for _, plugin in ipairs(self.plugins) do - for _, trigger in pairs(plugin.triggers or {}) do + for _, trigger in ipairs(plugin.triggers or {}) do if string.match(msg.text_lower, trigger) then local success, result = pcall(function() return plugin.action(self, msg, config) diff --git a/otouto/plugins/blacklist.lua b/otouto/plugins/blacklist.lua index 29a852e..33cebb7 100755 --- a/otouto/plugins/blacklist.lua +++ b/otouto/plugins/blacklist.lua @@ -16,36 +16,96 @@ blacklist.triggers = { } function blacklist:action(msg, config) - - if self.database.blacklist[tostring(msg.from.id)] then return end - if self.database.blacklist[tostring(msg.chat.id)] then return end - if not msg.text:match('^'..config.cmd_pat..'blacklist') then return true end - if msg.from.id ~= config.admin then return end - - local target = utilities.user_from_message(self, msg) - if target.err then - utilities.send_reply(self, msg, target.err) + if self.database.blacklist[tostring(msg.from.id)] or self.database.blacklist[tostring(msg.chat.id)] then return end - - if tonumber(target.id) < 0 then - target.name = 'Group' + if not ( + msg.from.id == config.admin + and ( + msg.text:match('^'..config.cmd_pat..'blacklist') + or msg.text:match('^'..config.cmd_pat..'unblacklist') + ) + ) then + return true end - - if self.database.blacklist[tostring(target.id)] then - self.database.blacklist[tostring(target.id)] = nil - utilities.send_reply(self, msg, target.name .. ' has been removed from the blacklist.') - if config.drua_block_on_blacklist then - require('drua-tg').unblock(target.id) - end + local targets = {} + if msg.reply_to_message then + table.insert(targets, { + id_str = tostring(msg.reply_to_message.from.id), + name = utilities.build_name(msg.reply_to_message.from.first_name, msg.reply_to_message.from.last_name) + }) else - self.database.blacklist[tostring(target.id)] = true - utilities.send_reply(self, msg, target.name .. ' has been added to the blacklist.') - if config.drua_block_on_blacklist then - require('drua-tg').block(target.id) + local input = utilities.input(msg.text) + if input then + for _, user in ipairs(utilities.index(input)) do + if self.database.users[user] then + table.insert(targets, { + id = self.database.users[user].id, + id_str = tostring(self.database.users[user].id), + name = utilities.build_name(self.database.users[user].first_name, self.database.users[user].last_name) + }) + elseif tonumber(user) then + local t = { + id_str = user, + id = tonumber(user) + } + if tonumber(user) < 0 then + t.name = 'Group (' .. user .. ')' + else + t.name = 'Unknown (' .. user .. ')' + end + table.insert(targets, t) + elseif user:match('^@') then + local u = utilities.resolve_username(self, user) + if u then + table.insert(targets, { + id = u.id, + id_str = tostring(u.id), + name = utilities.build_name(u.first_name, u.last_name) + }) + else + table.insert(targets, { err = 'Sorry, I do not recognize that username ('..user..').' }) + end + else + table.insert(targets, { err = 'Invalid username or ID ('..user..').' }) + end + end + else + utilities.send_reply(self, msg, 'Please specify a user or users via reply, username, or ID, or a group or groups via ID.') + return end end + local output = '' + if msg.text:match('^'..config.cmd_pat..'blacklist') then + for _, target in ipairs(targets) do + if target.err then + output = output .. target.err .. '\n' + elseif self.database.blacklist[target.id_str] then + output = output .. target.name .. ' is already blacklisted.\n' + else + self.database.blacklist[target.id_str] = true + output = output .. target.name .. ' is now blacklisted.\n' + if config.drua_block_on_blacklist and target.id > 0 then + require('drua-tg').block(target.id) + end + end + end + elseif msg.text:match('^'..config.cmd_pat..'unblacklist') then + for _, target in ipairs(targets) do + if target.err then + output = output .. target.err .. '\n' + elseif not self.database.blacklist[target.id_str] then + output = output .. target.name .. ' is not blacklisted.\n' + else + self.database.blacklist[target.id_str] = nil + output = output .. target.name .. ' is no longer blacklisted.\n' + if config.drua_block_on_blacklist and target.id > 0 then + require('drua-tg').unblock(target.id) + end + end + end + end + utilities.send_reply(self, msg, output) +end - end - - return blacklist +return blacklist diff --git a/otouto/plugins/control.lua b/otouto/plugins/control.lua index a9bbaaf..1599037 100644 --- a/otouto/plugins/control.lua +++ b/otouto/plugins/control.lua @@ -28,9 +28,11 @@ function control:action(msg, config) package.loaded['otouto.bindings'] = nil package.loaded['otouto.utilities'] = nil package.loaded['config'] = nil - if msg.text_lower:match('%+config') then for k, v in pairs(require('config')) do - config[k] = v - end end + if not msg.text_lower:match('%-config') then + for k, v in pairs(require('config')) do + config[k] = v + end + end bot.init(self, config) utilities.send_reply(self, msg, 'Bot reloaded!') elseif msg.text_lower:match('^'..cmd_pat..'halt') then diff --git a/otouto/plugins/reactions.lua b/otouto/plugins/reactions.lua index e594d50..d75a3c3 100755 --- a/otouto/plugins/reactions.lua +++ b/otouto/plugins/reactions.lua @@ -29,10 +29,17 @@ function reactions:init(config) -- Generate a "help" message triggered by "/reactions". help = 'Reactions:\n' reactions.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('reactions').table + local username = self.info.username:lower() for trigger,reaction in pairs(mapping) do help = help .. '• ' .. config.cmd_pat .. trigger:gsub('.%?', '') .. ': ' .. reaction .. '\n' - table.insert(reactions.triggers, config.cmd_pat..trigger) - table.insert(reactions.triggers, config.cmd_pat..trigger..'@'..self.info.username:lower()) + table.insert(reactions.triggers, '^'..config.cmd_pat..trigger) + table.insert(reactions.triggers, '^'..config.cmd_pat..trigger..'@'..username) + table.insert(reactions.triggers, config.cmd_pat..trigger..'$') + table.insert(reactions.triggers, config.cmd_pat..trigger..'@'..username..'$') + table.insert(reactions.triggers, '\n'..config.cmd_pat..trigger) + table.insert(reactions.triggers, '\n'..config.cmd_pat..trigger..'@'..username) + table.insert(reactions.triggers, config.cmd_pat..trigger..'\n') + table.insert(reactions.triggers, config.cmd_pat..trigger..'@'..username..'\n') end end diff --git a/otouto/plugins/rmspic.lua b/otouto/plugins/rmspic.lua new file mode 100644 index 0000000..40cf66f --- /dev/null +++ b/otouto/plugins/rmspic.lua @@ -0,0 +1,34 @@ +local https = require('ssl.https') +local utilities = require('otouto.utilities') +local bindings = require('otouto.bindings') + +local rms = {} + +function rms:init(config) + rms.BASE_URL = 'https://rms.sexy/img/' + rms.LIST = {} + local s, r = https.request(rms.BASE_URL) + if r ~= 200 then + print('Error connecting to rms.sexy.\nrmspic.lua will not be enabled.') + return + end + for link in s:gmatch('(.-)') do + table.insert(rms.LIST, link) + end + rms.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('rms').table +end + +function rms:action(msg, config) + bindings.sendChatAction(self, { chat_id = msg.chat.id, action = 'upload_photo' }) + local choice = rms.LIST[math.random(#rms.LIST)] + local filename = '/tmp/' .. choice + local image_file = io.open(filename) + if image_file then + image_file:close() + else + utilities.download_file(rms.BASE_URL .. choice, filename) + end + bindings.sendPhoto(self, { chat_id = msg.chat.id }, { photo = filename }) +end + +return rms