Skip to content
This repository has been archived by the owner on Jul 17, 2020. It is now read-only.

Export import #215

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions source/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
}
};

Expand Down
41 changes: 41 additions & 0 deletions source/plugins/export.js
Original file line number Diff line number Diff line change
@@ -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`'
});
})();
44 changes: 44 additions & 0 deletions source/plugins/import.js
Original file line number Diff line number Diff line change
@@ -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 <exported-content>`'
});
})();