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

Commit

Permalink
Fix import/export for the new headless mode (Electron), which disallo…
Browse files Browse the repository at this point in the history
…ws synchronous XHR.
  • Loading branch information
BobVul committed Nov 7, 2015
1 parent 834d562 commit 1fb7b6e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 36 deletions.
45 changes: 27 additions & 18 deletions source/plugins/export.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,40 @@
(function() {
"use strict";

bot.addCommand({
name : 'export',
fun : function(args) {
var req = new XMLHttpRequest();
req.open('POST', 'https://api.github.com/gists', false);
req.send(JSON.stringify({
files: {
'bot.json': {
content: JSON.stringify(bot.memory.data)
}
}
}));

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');
}
return 'Failed: ' + req.status + ': ' + req.statusText + resp;
args.reply('Failed: ' + req.status + ': ' + req.statusText + resp);
}

var resp = JSON.parse(req.responseText);

return 'Exported to gist, id: `' + resp.id + '` viewable at ' + resp.html_url;
},

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`'
});
Expand Down
45 changes: 27 additions & 18 deletions source/plugins/import.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,43 @@
(function() {
"use strict";

var doImport = function (args) {
if (args.trim() === 'clear') {
bot.memory.clear();

bot.addCommand({
name : 'import',
fun : function (args) {
if (args.trim() === 'clear') {
bot.memory.clear();

return 'Bot memory cleared. Please restart the bot.';
}

var req = new XMLHttpRequest();
req.open('GET', 'https://api.github.com/gists/' + args, false);
req.send(null);

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');
}
return 'Failed: ' + req.status + ': ' + req.statusText + resp;
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);
};

return "Imported and persisted successfully. Please restart the bot.";
},
bot.addCommand({
name : 'import',
fun : doImport,
permissions : { del : 'NONE', use : 'OWNER' },
description : 'Imports the persistent memory described in args `/export <exported-content>`'
});
Expand Down

0 comments on commit 1fb7b6e

Please sign in to comment.