diff --git a/bot.lua b/bot.lua index fd7680d..0d58f3f 100755 --- a/bot.lua +++ b/bot.lua @@ -3,7 +3,7 @@ HTTPS = require('ssl.https') URL = require('socket.url') JSON = require('cjson') -version = '3.2' +version = '3.4' bot_init = function() -- The function run when the bot is started or reloaded. @@ -11,6 +11,13 @@ bot_init = function() -- The function run when the bot is started or reloaded. dofile('bindings.lua') -- Load Telegram bindings. dofile('utilities.lua') -- Load miscellaneous and cross-plugin functions. + -- Save the database if it exists. Load it otherwise. + if database then + save_data('otouto.db', database) + else + database = load_data('otouto.db') + end + -- Fetch bot information. Try until it succeeds. repeat bot = getMe() until bot bot = bot.result @@ -29,15 +36,15 @@ bot_init = function() -- The function run when the bot is started or reloaded. last_update = last_update or 0 -- Set loop variables: Update offset, last_cron = last_cron or os.time() -- the time of the last cron job, - is_started = true -- whether the bot should be running or not. - usernames = usernames or {} -- Table to cache usernames by user ID. + is_started = true -- and whether or not the bot should be running. + database.usernames = database.usernames or {} -- Table to cache usernames by user ID. end on_msg_receive = function(msg) -- The fn run whenever a message is received. if msg.from.username then - usernames[msg.from.username:lower()] = msg.from.id + database.usernames[msg.from.username:lower()] = msg.from.id end if msg.date < os.time() - 5 then return end -- Do not process old messages. @@ -90,6 +97,7 @@ while is_started do -- Start a loop while the bot should be running. last_update = v.update_id on_msg_receive(v.message) end + save_data('otouto.db', database) else print(config.errors.connection) end @@ -108,4 +116,6 @@ while is_started do -- Start a loop while the bot should be running. end + -- Save the database before exiting. +save_data('otouto.db', database) print('Halted.') diff --git a/plugins/administration.lua b/plugins/administration.lua index d1b428d..29808fd 100644 --- a/plugins/administration.lua +++ b/plugins/administration.lua @@ -1,8 +1,9 @@ -admindata = load_data('administration.json') -if not admindata.global then - admindata.global = { - bans = {}, - admins = {} +if not database.administration then + database.administration = { + global = { + bans = {}, + admins = {} + } } end @@ -59,21 +60,21 @@ local get_rank = function(target, chat) return 5 end - if admindata.global.admins[target] then + if database.administration.global.admins[target] then return 4 end - if chat and admindata[chat] then - if admindata[chat].govs[target] then + if chat and database.administration[chat] then + if database.administration[chat].govs[target] then return 3 - elseif admindata[chat].mods[target] then + elseif database.administration[chat].mods[target] then return 2 - elseif admindata[chat].bans[target] then + elseif database.administration[chat].bans[target] then return 0 end end - if admindata.global.bans[target] then + if database.administration.global.bans[target] then return 0 end @@ -119,7 +120,7 @@ local kick_user = function(target, chat) target = tonumber(target) chat = tostring(chat) - if admindata[chat].grouptype == 'group' then + if database.administration[chat].grouptype == 'group' then tg:chat_del_user(tonumber(chat), target) else tg:channel_kick(chat, target) @@ -154,7 +155,7 @@ local commands = { if get_rank(msg.from.id, msg.chat.id) > 1 then return true end - if not admindata[msg.chat.id_str].flags[2] == true then + if not database.administration[msg.chat.id_str].flags[2] == true then return true end kick_user(msg.from.id, msg.chat.id) @@ -173,7 +174,7 @@ local commands = { action = function(msg) local rank = get_rank(msg.from.id, msg.chat.id) - local group = admindata[msg.chat.id_str] + local group = database.administration[msg.chat.id_str] -- banned if rank == 0 then @@ -302,7 +303,7 @@ local commands = { action = function(msg) local output = '' - for k,v in pairs(admindata) do + for k,v in pairs(database.administration) do -- no "global" or unlisted groups if tonumber(k) and not v.flags[1] then if v.link then @@ -348,21 +349,21 @@ local commands = { action = function(msg) local modstring = '' - for k,v in pairs(admindata[msg.chat.id_str].mods) do + for k,v in pairs(database.administration[msg.chat.id_str].mods) do modstring = modstring .. '• ' .. v .. ' (' .. k .. ')\n' end if modstring ~= '' then modstring = '*Moderators for* _' .. msg.chat.title .. '_ *:*\n' .. modstring end local govstring = '' - for k,v in pairs(admindata[msg.chat.id_str].govs) do + for k,v in pairs(database.administration[msg.chat.id_str].govs) do govstring = govstring .. '• ' .. v .. ' (' .. k .. ')\n' end if govstring ~= '' then govstring = '*Governors for* _' .. msg.chat.title .. '_ *:*\n' .. govstring end local adminstring = '*Administrators:*\n• ' .. config.admin_name .. ' (' .. config.admin .. ')\n' - for k,v in pairs(admindata.global.admins) do + for k,v in pairs(database.administration.global.admins) do adminstring = adminstring .. '• ' .. v .. ' (' .. k .. ')\n' end local output = modstring .. govstring .. adminstring @@ -382,8 +383,8 @@ local commands = { action = function(msg) local output = 'No rules have been set for ' .. msg.chat.title .. '.' - if admindata[msg.chat.id_str].rules then - output = '*Rules for* _' .. msg.chat.title .. '_ *:*\n' .. admindata[msg.chat.id_str].rules + if database.administration[msg.chat.id_str].rules then + output = '*Rules for* _' .. msg.chat.title .. '_ *:*\n' .. database.administration[msg.chat.id_str].rules end sendMessage(msg.chat.id, output, true, nil, true) end @@ -401,8 +402,8 @@ local commands = { action = function(msg) local output = 'No MOTD has been set for ' .. msg.chat.title .. '.' - if admindata[msg.chat.id_str].motd then - output = '*MOTD for* _' .. msg.chat.title .. '_ *:*\n' .. admindata[msg.chat.id_str].motd + if database.administration[msg.chat.id_str].motd then + output = '*MOTD for* _' .. msg.chat.title .. '_ *:*\n' .. database.administration[msg.chat.id_str].motd end sendMessage(msg.chat.id, output, true, nil, true) end @@ -419,8 +420,8 @@ local commands = { action = function(msg) local output = 'No link has been set for ' .. msg.chat.title .. '.' - if admindata[msg.chat.id_str].link then - output = '[' .. msg.chat.title .. '](' .. admindata[msg.chat.id_str].link .. ')' + if database.administration[msg.chat.id_str].link then + output = '[' .. msg.chat.title .. '](' .. database.administration[msg.chat.id_str].link .. ')' end sendMessage(msg.chat.id, output, true, nil, true) end @@ -491,12 +492,12 @@ local commands = { sendReply(msg, target.name .. ' cannot be banned: Too privileged.') return end - if admindata[msg.chat.id_str].bans[target.id_str] then + if database.administration[msg.chat.id_str].bans[target.id_str] then sendReply(msg, target.name .. ' is already banned.') return end kick_user(target.id, msg.chat.id) - admindata[msg.chat.id_str].bans[target.id_str] = true + database.administration[msg.chat.id_str].bans[target.id_str] = true sendMessage(msg.chat.id, target.name .. ' has been banned.') end }, @@ -516,15 +517,15 @@ local commands = { sendReply(msg, target.err) return end - if not admindata[msg.chat.id_str].bans[target.id_str] then - if admindata.global.bans[target.id_str] then + if not database.administration[msg.chat.id_str].bans[target.id_str] then + if database.administration.global.bans[target.id_str] then sendReply(msg, target.name .. ' is banned globally.') else sendReply(msg, target.name .. ' is not banned.') end return end - admindata[msg.chat.id_str].bans[target.id_str] = nil + database.administration[msg.chat.id_str].bans[target.id_str] = nil sendMessage(msg.chat.id, target.name .. ' has been unbanned.') end }, @@ -551,7 +552,7 @@ local commands = { i = i + 1 output = output .. '*' .. i .. '.* ' .. m:trim() .. '\n' end - admindata[msg.chat.id_str].rules = output + database.administration[msg.chat.id_str].rules = output output = '*Rules for* _' .. msg.chat.title .. '_ *:*\n' .. output sendMessage(msg.chat.id, output, true, nil, true) end @@ -573,7 +574,7 @@ local commands = { return end input = input:trim() - admindata[msg.chat.id_str].motd = input + database.administration[msg.chat.id_str].motd = input local output = '*MOTD for* _' .. msg.chat.title .. '_ *:*\n' .. input sendMessage(msg.chat.id, output, true, nil, true) end @@ -594,7 +595,7 @@ local commands = { sendReply(msg, '/' .. command) return end - admindata[msg.chat.id_str].link = input + database.administration[msg.chat.id_str].link = input local output = '[' .. msg.chat.title .. '](' .. input .. ')' sendMessage(msg.chat.id, output, true, nil, true) end @@ -619,18 +620,18 @@ local commands = { if not input then local output = '*Flags for* _' .. msg.chat.title .. '_ *:*\n' for i,v in ipairs(flags) do - local status = admindata[msg.chat.id_str].flags[i] or false + local status = database.administration[msg.chat.id_str].flags[i] or false output = output .. '`[' .. i .. ']` *' .. v.name .. '* = `' .. tostring(status) .. '`\n• ' .. v.desc .. '\n' end sendMessage(msg.chat.id, output, true, nil, true) return end local output - if admindata[msg.chat.id_str].flags[input] == true then - admindata[msg.chat.id_str].flags[input] = false + if database.administration[msg.chat.id_str].flags[input] == true then + database.administration[msg.chat.id_str].flags[input] = false sendReply(msg, flags[input].disabled) else - admindata[msg.chat.id_str].flags[input] = true + database.administration[msg.chat.id_str].flags[input] = true sendReply(msg, flags[input].enabled) end end @@ -655,10 +656,10 @@ local commands = { sendReply(msg, target.name .. ' cannot be promoted: Already privileged.') return end - if admindata[msg.chat.id_str].grouptype == 'supergroup' then + if database.administration[msg.chat.id_str].grouptype == 'supergroup' then tg:channel_set_admin(msg.chat.id, target, 1) end - admindata[msg.chat.id_str].mods[target.id_str] = target.name + database.administration[msg.chat.id_str].mods[target.id_str] = target.name sendReply(msg, target.name .. ' is now a moderator.') end }, @@ -682,10 +683,10 @@ local commands = { sendReply(msg, target.name .. ' is not a moderator.') return end - if admindata[msg.chat.id_str].grouptype == 'supergroup' then + if database.administration[msg.chat.id_str].grouptype == 'supergroup' then tg:channel_set_admin(msg.chat.id, target, 0) end - admindata[msg.chat.id_str].mods[target.id_str] = nil + database.administration[msg.chat.id_str].mods[target.id_str] = nil sendReply(msg, target.name .. ' is no longer a moderator.') end @@ -710,12 +711,12 @@ local commands = { sendReply(msg, target.name .. ' cannot be promoted: Already privileged.') return elseif target.rank == 2 then - admindata[msg.chat.id_str].mods[target.id_str] = nil + database.administration[msg.chat.id_str].mods[target.id_str] = nil end - if admindata[msg.chat.id_str].grouptype == 'supergroup' then + if database.administration[msg.chat.id_str].grouptype == 'supergroup' then tg:channel_set_admin(msg.chat.id, target, 1) end - admindata[msg.chat.id_str].govs[target.id_str] = target.name + database.administration[msg.chat.id_str].govs[target.id_str] = target.name sendReply(msg, target.name .. ' is now a governor.') end }, @@ -739,10 +740,10 @@ local commands = { sendReply(msg, target.name .. ' is not a governor.') return end - if admindata[msg.chat.id_str].grouptype == 'supergroup' then + if database.administration[msg.chat.id_str].grouptype == 'supergroup' then tg:channel_set_admin(msg.chat.id, target, 0) end - admindata[msg.chat.id_str].govs[target.id_str] = nil + database.administration[msg.chat.id_str].govs[target.id_str] = nil sendReply(msg, target.name .. ' is no longer a governor.') end }, @@ -767,16 +768,16 @@ local commands = { sendReply(msg, target.name .. ' cannot be banned: Too privileged.') return end - if admindata.global.bans[target.id_str] then + if database.administration.global.bans[target.id_str] then sendReply(msg, target.name .. ' is already banned globally.') return end - for k,v in pairs(admindata) do + for k,v in pairs(database.administration) do if tonumber(k) then kick_user(target.id, k) end end - admindata.global.bans[target.id_str] = true + database.administration.global.bans[target.id_str] = true sendReply(msg, target.name .. ' has been globally banned.') end }, @@ -797,11 +798,11 @@ local commands = { sendReply(msg, target.err) return end - if not admindata.global.bans[target.id_str] then + if not database.administration.global.bans[target.id_str] then sendReply(msg, target.name .. ' is not banned globally.') return end - admindata.global.bans[target.id_str] = nil + database.administration.global.bans[target.id_str] = nil sendReply(msg, target.name .. ' has been globally unbanned.') end }, @@ -825,11 +826,11 @@ local commands = { sendReply(msg, target.name .. ' cannot be promoted: Already privileged.') return elseif target.rank == 2 then - admindata[msg.chat.id_str].mods[target.id_str] = nil + database.administration[msg.chat.id_str].mods[target.id_str] = nil elseif target.rank == 3 then - admindata[msg.chat.id_str].govs[target.id_str] = nil + database.administration[msg.chat.id_str].govs[target.id_str] = nil end - admindata.global.admins[target.id_str] = target.name + database.administration.global.admins[target.id_str] = target.name sendReply(msg, target.name .. ' is now an administrator.') end }, @@ -849,7 +850,7 @@ local commands = { sendReply(msg, target.name .. ' is not an administrator.') return end - admindata.global.admins[target.id_str] = nil + database.administration.global.admins[target.id_str] = nil sendReply(msg, target.name .. ' is no longer an administrator.') end }, @@ -864,11 +865,11 @@ local commands = { interior = false, action = function(msg) - if admindata[msg.chat.id_str] then + if database.administration[msg.chat.id_str] then sendReply(msg, 'I am already administrating this group.') return end - admindata[msg.chat.id_str] = { + database.administration[msg.chat.id_str] = { mods = {}, govs = {}, bans = {}, @@ -878,8 +879,8 @@ local commands = { founded = os.time() } if msg.chat.type == 'group' then - admindata[msg.chat.id_str].photo = get_photo(msg.chat.id) - admindata[msg.chat.id_str].link = tg:export_chat_link(msg.chat.id) + database.administration[msg.chat.id_str].photo = get_photo(msg.chat.id) + database.administration[msg.chat.id_str].link = tg:export_chat_link(msg.chat.id) end sendReply(msg, 'I am now administrating this group.') end @@ -900,7 +901,7 @@ local commands = { if not input then input = msg.chat.id_str end - admindata[input] = nil + database.administration[input] = nil sendReply(msg, 'I am no longer administrating this group.') end }, @@ -921,7 +922,7 @@ local commands = { return end input = '*Admin Broadcast:*\n' .. input - for k,v in pairs(admindata) do + for k,v in pairs(database.administration) do if tonumber(k) then sendMessage(k, input, true, nil, true) end @@ -952,7 +953,7 @@ local action = function(msg) -- wee nesting for i,v in ipairs(commands) do for key,val in pairs(v.triggers) do if msg.text_lower:match(val) then - if v.interior and not admindata[msg.chat.id_str] then + if v.interior and not database.administration[msg.chat.id_str] then break end if msg.chat.type ~= 'private' and get_rank(msg.from.id, msg.chat.id) < v.privilege then @@ -972,7 +973,6 @@ local cron = function() if os.date('%M', os.time()) ~= last_admin_cron then last_admin_cron = os.date('%M', os.time()) tg = sender(localhost, config.cli_port) - save_data('administration.json', admindata) end end diff --git a/plugins/blacklist.lua b/plugins/blacklist.lua index 6cd32c7..7a83505 100755 --- a/plugins/blacklist.lua +++ b/plugins/blacklist.lua @@ -1,15 +1,17 @@ -- This plugin will allow the admin to blacklist users who will be unable to -- use the bot. This plugin should be at the top of your plugin list in config. +if not database.blacklist then + database.blacklist = {} +end + local triggers = { '' } local action = function(msg) - local blacklist = load_data('blacklist.json') - - if blacklist[msg.from.id_str] then + if database.blacklist[msg.from.id_str] then return -- End if the sender is blacklisted. end @@ -31,16 +33,14 @@ local triggers = { end end - if blacklist[input] then - blacklist[input] = nil + if database.blacklist[input] then + database.blacklist[input] = nil sendReply(msg, input .. ' has been removed from the blacklist.') else - blacklist[input] = true + database.blacklist[input] = true sendReply(msg, input .. ' has been added to the blacklist.') end - save_data('blacklist.json', blacklist) - end return { diff --git a/plugins/greetings.lua b/plugins/greetings.lua index 9f1c476..1d18309 100755 --- a/plugins/greetings.lua +++ b/plugins/greetings.lua @@ -6,9 +6,7 @@ local triggers = { local action = function(msg) - local nicks = load_data('nicknames.json') - - local nick = nicks[msg.from.id_str] or msg.from.first_name + local nick = database.nicknames[msg.from.id_str] or msg.from.first_name for k,v in pairs(config.greetings) do for key,val in pairs(v) do diff --git a/plugins/hearthstone.lua b/plugins/hearthstone.lua index 5696067..ec5e088 100755 --- a/plugins/hearthstone.lua +++ b/plugins/hearthstone.lua @@ -1,22 +1,29 @@ -- Plugin for the Hearthstone database provided by hearthstonejson.com. -if not hs_dat then +if not database.hearthstone or os.time() > database.hearthstone.expiration then - hs_dat = {} + print('Downloading Hearthstone database...') + + database.hearthstone = {} local jstr, res = HTTPS.request('http://hearthstonejson.com/json/AllSets.json') if res ~= 200 then print('Error connecting to hearthstonejson.com.') print('hearthstone.lua will not be enabled.') + return end local jdat = JSON.decode(jstr) for k,v in pairs(jdat) do for key,val in pairs(v) do - table.insert(hs_dat, val) + table.insert(database.hearthstone, val) end end + database.hearthstone.expiration = os.time() + 600000 + + print('Download complete! It will be permanently stored.') + end local command = 'hearthstone ' @@ -96,7 +103,7 @@ local action = function(msg) end local output = '' - for k,v in pairs(hs_dat) do + for k,v in pairs(database.hearthstone) do if string.lower(v.name):match(input) then output = output .. format_card(v) .. '\n\n' end diff --git a/plugins/lastfm.lua b/plugins/lastfm.lua index 9f33200..3549824 100755 --- a/plugins/lastfm.lua +++ b/plugins/lastfm.lua @@ -4,6 +4,10 @@ if not config.lastfm_api_key then return end +if not database.lastfm then + database.lastfm = {} +end + local command = 'lastfm' local doc = [[``` /np [username] @@ -21,7 +25,6 @@ local triggers = { local action = function(msg) - lastfm = load_data('lastfm.json') local input = msg.text:input() if string.match(msg.text, '^/lastfm') then @@ -31,13 +34,12 @@ local action = function(msg) if not input then sendMessage(msg.chat.id, doc, true, msg.message_id, true) elseif input == '-' then - lastfm[msg.from.id_str] = nil + database.lastfm[msg.from.id_str] = nil sendReply(msg, 'Your last.fm username has been forgotten.') else - lastfm[msg.from.id_str] = input + database.lastfm[msg.from.id_str] = input sendReply(msg, 'Your last.fm username has been set to "' .. input .. '".') end - save_data('lastfm.json', lastfm) return end @@ -47,13 +49,12 @@ local action = function(msg) local output = '' if input then username = input - elseif lastfm[msg.from.id_str] then - username = lastfm[msg.from.id_str] + elseif database.lastfm[msg.from.id_str] then + username = database.lastfm[msg.from.id_str] elseif msg.from.username then username = msg.from.username output = '\n\nYour username has been set to ' .. username .. '.\nTo change it, use /fmset .' - lastfm[msg.from.id_str] = username - save_data('lastfm.json', lastfm) + database.lastfm[msg.from.id_str] = username else sendReply(msg, 'Please specify your last.fm username or set it with /fmset.') return diff --git a/plugins/librefm.lua b/plugins/librefm.lua index b49f590..32e0b97 100644 --- a/plugins/librefm.lua +++ b/plugins/librefm.lua @@ -1,3 +1,7 @@ +if not database.librefm then + database.librefm = {} +end + local command = 'librefm' local doc = [[``` /lnp [username] @@ -15,7 +19,6 @@ local triggers = { local action = function(msg) - lastfm = load_data('librefm.json') local input = msg.text:input() if string.match(msg.text, '^/librefm') then @@ -25,13 +28,12 @@ local action = function(msg) if not input then sendMessage(msg.chat.id, doc, true, msg.message_id, true) elseif input == '-' then - lastfm[msg.from.id_str] = nil + database.lastfm[msg.from.id_str] = nil sendReply(msg, 'Your libre.fm username has been forgotten.') else - lastfm[msg.from.id_str] = input + database.lastfm[msg.from.id_str] = input sendReply(msg, 'Your libre.fm username has been set to "' .. input .. '".') end - save_data('librefm.json', lastfm) return end @@ -41,13 +43,12 @@ local action = function(msg) local output = '' if input then username = input - elseif lastfm[msg.from.id_str] then - username = lastfm[msg.from.id_str] + elseif database.lastfm[msg.from.id_str] then + username = database.lastfm[msg.from.id_str] elseif msg.from.username then username = msg.from.username output = '\n\nYour username has been set to ' .. username .. '.\nTo change it, use /lfmset .' - lastfm[msg.from.id_str] = username - save_data('lastfm.json', lastfm) + database.lastfm[msg.from.id_str] = username else sendReply(msg, 'Please specify your libre.fm username or set it with /lfmset.') return diff --git a/plugins/moderation.lua b/plugins/moderation.lua index d69b2f1..d9f713c 100755 --- a/plugins/moderation.lua +++ b/plugins/moderation.lua @@ -3,14 +3,17 @@ -- Put this near the top, after blacklist. -- If you want to enable antisquig, put that at the top, before blacklist. -moddat = load_data('moderation.json') +if not database.moderation then + database.moderation = {} +end + antisquig = {} local commands = { ['^/modhelp[@'..bot.username..']*$'] = function(msg) - if not moddat[msg.chat.id_str] then return end + if not database.moderation[msg.chat.id_str] then return end local message = [[ /modlist - List the moderators and administrators of this group. @@ -31,11 +34,11 @@ local commands = { ['^/modlist[@'..bot.username..']*$'] = function(msg) - if not moddat[msg.chat.id_str] then return end + if not database.moderation[msg.chat.id_str] then return end local message = '' - for k,v in pairs(moddat[msg.chat.id_str]) do + for k,v in pairs(database.moderation[msg.chat.id_str]) do message = message .. ' - ' .. v .. ' (' .. k .. ')\n' end @@ -67,7 +70,7 @@ local commands = { return config.errors.not_admin end - for k,v in pairs(moddat) do + for k,v in pairs(database.moderation) do sendMessage(k, message) end @@ -81,12 +84,11 @@ local commands = { return config.errors.not_admin end - if moddat[msg.chat.id_str] then + if database.moderation[msg.chat.id_str] then return 'I am already moderating this group.' end - moddat[msg.chat.id_str] = {} - save_data('moderation.json', moddat) + database.moderation[msg.chat.id_str] = {} return 'I am now moderating this group.' end, @@ -97,19 +99,18 @@ local commands = { return config.errors.not_admin end - if not moddat[msg.chat.id_str] then + if not database.moderation[msg.chat.id_str] then return config.errors.moderation end - moddat[msg.chat.id_str] = nil - save_data('moderation.json', moddat) + database.moderation[msg.chat.id_str] = nil return 'I am no longer moderating this group.' end, ['^/modprom[@'..bot.username..']*$'] = function(msg) - if not moddat[msg.chat.id_str] then return end + if not database.moderation[msg.chat.id_str] then return end if not config.moderation.admins[msg.from.id_str] then return config.errors.not_admin @@ -126,12 +127,11 @@ local commands = { return modname .. ' is already an administrator.' end - if moddat[msg.chat.id_str][modid] then + if database.moderation[msg.chat.id_str][modid] then return modname .. ' is already a moderator.' end - moddat[msg.chat.id_str][modid] = modname - save_data('moderation.json', moddat) + database.moderation[msg.chat.id_str][modid] = modname return modname .. ' is now a moderator.' @@ -139,7 +139,7 @@ local commands = { ['^/moddem[@'..bot.username..']*'] = function(msg) - if not moddat[msg.chat.id_str] then return end + if not database.moderation[msg.chat.id_str] then return end if not config.moderation.admins[msg.from.id_str] then return config.errors.not_admin @@ -159,13 +159,12 @@ local commands = { return config.moderation.admins[modid] .. ' is an administrator.' end - if not moddat[msg.chat.id_str][modid] then + if not database.moderation[msg.chat.id_str][modid] then return 'User is not a moderator.' end - local modname = moddat[msg.chat.id_str][modid] - moddat[msg.chat.id_str][modid] = nil - save_data('moderation.json', moddat) + local modname = database.moderation[msg.chat.id_str][modid] + database.moderation[msg.chat.id_str][modid] = nil return modname .. ' is no longer a moderator.' @@ -173,9 +172,9 @@ local commands = { ['/modkick[@'..bot.username..']*'] = function(msg) - if not moddat[msg.chat.id_str] then return end + if not database.moderation[msg.chat.id_str] then return end - if not moddat[msg.chat.id_str][msg.from.id_str] then + if not database.moderation[msg.chat.id_str][msg.from.id_str] then if not config.moderation.admins[msg.from.id_str] then return config.errors.not_mod end @@ -193,7 +192,7 @@ local commands = { return 'Kicks must be done via reply or specification of a user/bot\'s ID or username.' end - if moddat[msg.chat.id_str][userid] or config.moderation.admins[userid] then + if database.moderation[msg.chat.id_str][userid] or config.moderation.admins[userid] then return 'You cannot kick a moderator.' end @@ -205,9 +204,9 @@ local commands = { ['^/modban[@'..bot.username..']*'] = function(msg) - if not moddat[msg.chat.id_str] then return end + if not database.moderation[msg.chat.id_str] then return end - if not moddat[msg.chat.id_str][msg.from.id_str] then + if not database.moderation[msg.chat.id_str][msg.from.id_str] then if not config.moderation.admins[msg.from.id_str] then return config.errors.not_mod end @@ -225,7 +224,7 @@ local commands = { return 'Kicks must be done via reply or specification of a user/bot\'s ID or username.' end - if moddat[msg.chat.id_str][userid] or config.moderation.admins[userid] then + if database.moderation[msg.chat.id_str][userid] or config.moderation.admins[userid] then return 'You cannot ban a moderator.' end @@ -240,9 +239,9 @@ local commands = { if config.antisquig then commands['[\216-\219][\128-\191]'] = function(msg) - if not moddat[msg.chat.id_str] then return true end + if not database.moderation[msg.chat.id_str] then return true end if config.moderation.admins[msg.from.id_str] then return true end - if moddat[msg.chat.id_str][msg.from.id_str] then return true end + if database.moderation[msg.chat.id_str][msg.from.id_str] then return true end if antisquig[msg.from.id] == true then return diff --git a/plugins/nick.lua b/plugins/nick.lua index 90bac1e..b9294f1 100755 --- a/plugins/nick.lua +++ b/plugins/nick.lua @@ -1,3 +1,7 @@ +if not database.nicknames then + database.nicknames = {} +end + local command = 'nick ' local doc = [[``` /nick @@ -21,17 +25,14 @@ local action = function(msg) return true end - nicks = load_data('nicknames.json') - if input == '-' then - nicks[msg.from.id_str] = nil + database.nicknames[msg.from.id_str] = nil sendReply(msg, 'Your nickname has been deleted.') else - nicks[msg.from.id_str] = input + database.nicknames[msg.from.id_str] = input sendReply(msg, 'Your nickname has been set to "' .. input .. '".') end - save_data('nicknames.json', nicks) return true end diff --git a/plugins/slap.lua b/plugins/slap.lua index 0bd2484..354d8ef 100755 --- a/plugins/slap.lua +++ b/plugins/slap.lua @@ -9,105 +9,103 @@ local triggers = { } local slaps = { - '$victim was shot by $victor.', - '$victim was pricked to death.', - '$victim walked into a cactus while trying to escape $victor.', - '$victim drowned.', - '$victim drowned whilst trying to escape $victor.', - '$victim blew up.', - '$victim was blown up by $victor.', - '$victim hit the ground too hard.', - '$victim fell from a high place.', - '$victim fell off a ladder.', - '$victim fell into a patch of cacti.', - '$victim was doomed to fall by $victor.', - '$victim was blown from a high place by $victor.', - '$victim was squashed by a falling anvil.', - '$victim went up in flames.', - '$victim burned to death.', - '$victim was burnt to a crisp whilst fighting $victor.', - '$victim walked into a fire whilst fighting $victor.', - '$victim tried to swim in lava.', - '$victim tried to swim in lava while trying to escape $victor.', - '$victim was struck by lightning.', - '$victim was slain by $victor.', - '$victim got finished off by $victor.', - '$victim was killed by magic.', - '$victim was killed by $victor using magic.', - '$victim starved to death.', - '$victim suffocated in a wall.', - '$victim fell out of the world.', - '$victim was knocked into the void by $victor.', - '$victim withered away.', - '$victim was pummeled by $victor.', - '$victim was fragged by $victor.', - '$victim was desynchronized.', - '$victim was wasted.', - '$victim was busted.', - '$victim\'s bones are scraped clean by the desolate wind.', - '$victim has died of dysentery.', - '$victim fainted.', - '$victim is out of usable Pokemon! $victim whited out!', - '$victim is out of usable Pokemon! $victim blacked out!', - '$victim whited out!', - '$victim blacked out!', - '$victim says goodbye to this cruel world.', - '$victim got rekt.', - '$victim was sawn in half by $victor.', - '$victim died. I blame $victor.', - '$victim was axe-murdered by $victor.', - '$victim\'s melon was split by $victor.', - '$victim was slice and diced by $victor.', - '$victim was split from crotch to sternum by $victor.', - '$victim\'s death put another notch in $victor\'s axe.', - '$victim died impossibly!', - '$victim died from $victor\'s mysterious tropical disease.', - '$victim escaped infection by dying.', - '$victim played hot-potato with a grenade.', - '$victim was knifed by $victor.', - '$victim fell on his sword.', - '$victim ate a grenade.', - '$victim practiced being $victor\'s clay pigeon.', - '$victim is what\'s for dinner!', - '$victim was terminated by $victor.', - '$victim was shot before being thrown out of a plane.', - '$victim was not invincible.', - '$victim has encountered an error.', - '$victim died and reincarnated as a goat.', - '$victor threw $victim off a building.', - '$victim is sleeping with the fishes.', - '$victim got a premature burial.', - '$victor replaced all of $victim\'s music with Nickelback.', - '$victor spammed $victim\'s email.', - '$victor made $victim a knuckle sandwich.', - '$victor slapped $victim with pure nothing.', - '$victor hit $victim with a small, interstellar spaceship.', - '$victim was quickscoped by $victor.', - '$victor put $victim in check-mate.', - '$victor RSA-encrypted $victim and deleted the private key.', - '$victor put $victim in the friendzone.', - '$victor slaps $victim with a DMCA takedown request!', - '$victim became a corpse blanket for $victor.', - 'Death is when the monsters get you. Death comes for $victim.', - 'Cowards die many times before their death. $victim never tasted death but once.' + 'VICTIM was shot by VICTOR.', + 'VICTIM was pricked to death.', + 'VICTIM walked into a cactus while trying to escape VICTOR.', + 'VICTIM drowned.', + 'VICTIM drowned whilst trying to escape VICTOR.', + 'VICTIM blew up.', + 'VICTIM was blown up by VICTOR.', + 'VICTIM hit the ground too hard.', + 'VICTIM fell from a high place.', + 'VICTIM fell off a ladder.', + 'VICTIM fell into a patch of cacti.', + 'VICTIM was doomed to fall by VICTOR.', + 'VICTIM was blown from a high place by VICTOR.', + 'VICTIM was squashed by a falling anvil.', + 'VICTIM went up in flames.', + 'VICTIM burned to death.', + 'VICTIM was burnt to a crisp whilst fighting VICTOR.', + 'VICTIM walked into a fire whilst fighting VICTOR.', + 'VICTIM tried to swim in lava.', + 'VICTIM tried to swim in lava while trying to escape VICTOR.', + 'VICTIM was struck by lightning.', + 'VICTIM was slain by VICTOR.', + 'VICTIM got finished off by VICTOR.', + 'VICTIM was killed by magic.', + 'VICTIM was killed by VICTOR using magic.', + 'VICTIM starved to death.', + 'VICTIM suffocated in a wall.', + 'VICTIM fell out of the world.', + 'VICTIM was knocked into the void by VICTOR.', + 'VICTIM withered away.', + 'VICTIM was pummeled by VICTOR.', + 'VICTIM was fragged by VICTOR.', + 'VICTIM was desynchronized.', + 'VICTIM was wasted.', + 'VICTIM was busted.', + 'VICTIM\'s bones are scraped clean by the desolate wind.', + 'VICTIM has died of dysentery.', + 'VICTIM fainted.', + 'VICTIM is out of usable Pokemon! VICTIM whited out!', + 'VICTIM is out of usable Pokemon! VICTIM blacked out!', + 'VICTIM whited out!', + 'VICTIM blacked out!', + 'VICTIM says goodbye to this cruel world.', + 'VICTIM got rekt.', + 'VICTIM was sawn in half by VICTOR.', + 'VICTIM died. I blame VICTOR.', + 'VICTIM was axe-murdered by VICTOR.', + 'VICTIM\'s melon was split by VICTOR.', + 'VICTIM was slice and diced by VICTOR.', + 'VICTIM was split from crotch to sternum by VICTOR.', + 'VICTIM\'s death put another notch in VICTOR\'s axe.', + 'VICTIM died impossibly!', + 'VICTIM died from VICTOR\'s mysterious tropical disease.', + 'VICTIM escaped infection by dying.', + 'VICTIM played hot-potato with a grenade.', + 'VICTIM was knifed by VICTOR.', + 'VICTIM fell on his sword.', + 'VICTIM ate a grenade.', + 'VICTIM practiced being VICTOR\'s clay pigeon.', + 'VICTIM is what\'s for dinner!', + 'VICTIM was terminated by VICTOR.', + 'VICTIM was shot before being thrown out of a plane.', + 'VICTIM was not invincible.', + 'VICTIM has encountered an error.', + 'VICTIM died and reincarnated as a goat.', + 'VICTOR threw VICTIM off a building.', + 'VICTIM is sleeping with the fishes.', + 'VICTIM got a premature burial.', + 'VICTOR replaced all of VICTIM\'s music with Nickelback.', + 'VICTOR spammed VICTIM\'s email.', + 'VICTOR made VICTIM a knuckle sandwich.', + 'VICTOR slapped VICTIM with pure nothing.', + 'VICTOR hit VICTIM with a small, interstellar spaceship.', + 'VICTIM was quickscoped by VICTOR.', + 'VICTOR put VICTIM in check-mate.', + 'VICTOR RSA-encrypted VICTIM and deleted the private key.', + 'VICTOR put VICTIM in the friendzone.', + 'VICTOR slaps VICTIM with a DMCA takedown request!', + 'VICTIM became a corpse blanket for VICTOR.', + 'Death is when the monsters get you. Death comes for VICTIM.', + 'Cowards die many times before their death. VICTIM never tasted death but once.' } local action = function(msg) - local nicks = load_data('nicknames.json') - local victim = msg.text:input() if msg.reply_to_message then - if nicks[tostring(msg.reply_to_message.from.id)] then - victim = nicks[tostring(msg.reply_to_message.from.id)] + if database.nicknames[tostring(msg.reply_to_message.from.id)] then + victim = database.nicknames[tostring(msg.reply_to_message.from.id)] else victim = msg.reply_to_message.from.first_name end end local victor = msg.from.first_name - if nicks[msg.from.id_str] then - victor = nicks[msg.from.id_str] + if database.nicknames[msg.from.id_str] then + victor = database.nicknames[msg.from.id_str] end if not victim then @@ -116,8 +114,8 @@ local action = function(msg) end local message = slaps[math.random(#slaps)] - message = message:gsub('$victim', victim) - message = message:gsub('$victor', victor) + message = message:gsub('VICTIM', victim) + message = message:gsub('VICTOR', victor) sendMessage(msg.chat.id, message) diff --git a/plugins/whoami.lua b/plugins/whoami.lua index 7584c7a..4bf2d36 100755 --- a/plugins/whoami.lua +++ b/plugins/whoami.lua @@ -32,9 +32,8 @@ local action = function(msg) local message = 'You are ' .. from_name .. ' and you are messaging ' .. to_name - local nicks = load_data('nicknames.json') - if nicks[msg.from.id_str] then - message = message .. '\nYour nickname is ' .. nicks[msg.from.id_str] .. '.' + if database.nicknames[msg.from.id_str] then + message = message .. '\nYour nickname is ' .. database.nicknames[msg.from.id_str] .. '.' end sendReply(msg, message) diff --git a/utilities.lua b/utilities.lua index fd577eb..7eeb5c4 100755 --- a/utilities.lua +++ b/utilities.lua @@ -139,7 +139,7 @@ resolve_username = function(target) local input = tostring(target):lower() if input:match('^@') then local uname = input:gsub('^@', '') - return usernames[uname] + return database.usernames[uname] else return tonumber(target) or false end