diff --git a/source/bot.js b/source/bot.js index db14048..b6752bb 100644 --- a/source/bot.js +++ b/source/bot.js @@ -314,6 +314,15 @@ bot.memory = { saveLoop : function () { clearTimeout( this.saveIntervalId ); setTimeout( this.saveLoop.bind(this), this.saveInterval ); + }, + + clear : function () { + Object.iterate( localStorage, function ( key, val ) { + if ( key.startsWith('bot_') ) { + localStorage.removeItem(key); + } + }); + this.data = {}; } }; diff --git a/source/plugins/export.js b/source/plugins/export.js new file mode 100644 index 0000000..9b0c940 --- /dev/null +++ b/source/plugins/export.js @@ -0,0 +1,41 @@ +(function() { + "use strict"; + + var doExport = function(args) { + var req = new XMLHttpRequest(); + req.addEventListener('abort', function() { + args.reply('Failed: Gist request aborted by user (???)'); + }); + req.addEventListener('error', function() { + args.reply('Failed: Gist request encountered a network error'); + }); + req.addEventListener('load', function() { + if (req.status !== 201) { + var resp = ''; + if (req.responseText) { + resp = '\n' + req.responseText.match(/.{1,400}/g).join('\n'); + } + args.reply('Failed: ' + req.status + ': ' + req.statusText + resp); + } + + var resp = JSON.parse(req.responseText); + + args.reply('Exported to gist, id: `' + resp.id + '` viewable at ' + resp.html_url); + }); + req.open('POST', 'https://api.github.com/gists', true); + req.send(JSON.stringify({ + files: { + 'bot.json': { + content: JSON.stringify(bot.memory.data) + } + } + })); + }; + + bot.addCommand({ + name : 'export', + fun : doExport, + permissions : { del : 'NONE', use : 'OWNER' }, + description : 'Blurts out a message with the persistent memory storage for export `/export`' + }); +})(); diff --git a/source/plugins/import.js b/source/plugins/import.js new file mode 100644 index 0000000..5904c74 --- /dev/null +++ b/source/plugins/import.js @@ -0,0 +1,44 @@ +(function() { + "use strict"; + + var doImport = function (args) { + if (args.trim() === 'clear') { + bot.memory.clear(); + + return 'Bot memory cleared. Please restart the bot.'; + } + + var req = new XMLHttpRequest(); + req.addEventListener('abort', function() { + args.reply('Failed: Gist request aborted by user (???)'); + }); + req.addEventListener('error', function() { + args.reply('Failed: Gist request encountered a network error'); + }); + req.addEventListener('load', function() { + if (req.status !== 200) { + var resp = ''; + if (req.responseText) { + resp = '\n' + req.responseText.match(/.{1,400}/g).join('\n'); + } + args.reply('Failed: ' + req.status + ': ' + req.statusText + resp); + } + + var resp = JSON.parse(req.responseText); + + bot.memory.data = JSON.parse(resp.files['bot.json'].content); + bot.memory.save(); + + args.reply("Imported and persisted successfully. Please restart the bot."); + }); + req.open('GET', 'https://api.github.com/gists/' + args, true); + req.send(null); + }; + + bot.addCommand({ + name : 'import', + fun : doImport, + permissions : { del : 'NONE', use : 'OWNER' }, + description : 'Imports the persistent memory described in args `/export `' + }); +})();