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

Commit 5c5efaf

Browse files
committed
Fix import/export for the new headless mode (Electron), which disallows synchronous XHR.
1 parent 8afe1f0 commit 5c5efaf

File tree

2 files changed

+54
-36
lines changed

2 files changed

+54
-36
lines changed

source/plugins/export.js

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,40 @@
11
(function() {
22
"use strict";
33

4-
bot.addCommand({
5-
name : 'export',
6-
fun : function(args) {
7-
var req = new XMLHttpRequest();
8-
req.open('POST', 'https://api.github.com/gists', false);
9-
req.send(JSON.stringify({
10-
files: {
11-
'bot.json': {
12-
content: JSON.stringify(bot.memory.data)
13-
}
14-
}
15-
}));
16-
4+
var doExport = function(args) {
5+
var req = new XMLHttpRequest();
6+
req.addEventListener('abort', function() {
7+
args.reply('Failed: Gist request aborted by user (???)');
8+
});
9+
req.addEventListener('error', function() {
10+
args.reply('Failed: Gist request encountered a network error');
11+
});
12+
req.addEventListener('load', function() {
1713
if (req.status !== 201) {
1814
var resp = '';
1915
if (req.responseText) {
2016
resp = '\n' + req.responseText.match(/.{1,400}/g).join('\n');
2117
}
22-
return 'Failed: ' + req.status + ': ' + req.statusText + resp;
18+
args.reply('Failed: ' + req.status + ': ' + req.statusText + resp);
2319
}
24-
20+
2521
var resp = JSON.parse(req.responseText);
26-
27-
return 'Exported to gist, id: `' + resp.id + '` viewable at ' + resp.html_url;
28-
},
22+
23+
args.reply('Exported to gist, id: `' + resp.id + '` viewable at ' + resp.html_url);
24+
});
25+
req.open('POST', 'https://api.github.com/gists', true);
26+
req.send(JSON.stringify({
27+
files: {
28+
'bot.json': {
29+
content: JSON.stringify(bot.memory.data)
30+
}
31+
}
32+
}));
33+
};
34+
35+
bot.addCommand({
36+
name : 'export',
37+
fun : doExport,
2938
permissions : { del : 'NONE', use : 'OWNER' },
3039
description : 'Blurts out a message with the persistent memory storage for export `/export`'
3140
});

source/plugins/import.js

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,43 @@
11
(function() {
22
"use strict";
3+
4+
var doImport = function (args) {
5+
if (args.trim() === 'clear') {
6+
bot.memory.clear();
37

4-
bot.addCommand({
5-
name : 'import',
6-
fun : function (args) {
7-
if (args.trim() === 'clear') {
8-
bot.memory.clear();
9-
10-
return 'Bot memory cleared. Please restart the bot.';
11-
}
12-
13-
var req = new XMLHttpRequest();
14-
req.open('GET', 'https://api.github.com/gists/' + args, false);
15-
req.send(null);
16-
8+
return 'Bot memory cleared. Please restart the bot.';
9+
}
10+
11+
var req = new XMLHttpRequest();
12+
req.addEventListener('abort', function() {
13+
args.reply('Failed: Gist request aborted by user (???)');
14+
});
15+
req.addEventListener('error', function() {
16+
args.reply('Failed: Gist request encountered a network error');
17+
});
18+
req.addEventListener('load', function() {
1719
if (req.status !== 200) {
1820
var resp = '';
1921
if (req.responseText) {
2022
resp = '\n' + req.responseText.match(/.{1,400}/g).join('\n');
2123
}
22-
return 'Failed: ' + req.status + ': ' + req.statusText + resp;
24+
args.reply('Failed: ' + req.status + ': ' + req.statusText + resp);
2325
}
24-
26+
2527
var resp = JSON.parse(req.responseText);
26-
28+
2729
bot.memory.data = JSON.parse(resp.files['bot.json'].content);
2830
bot.memory.save();
31+
32+
args.reply("Imported and persisted successfully. Please restart the bot.");
33+
});
34+
req.open('GET', 'https://api.github.com/gists/' + args, true);
35+
req.send(null);
36+
};
2937

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

0 commit comments

Comments
 (0)