|
1 | 1 | (function() {
|
2 | 2 | "use strict";
|
| 3 | + |
| 4 | + var doImport = function (args) { |
| 5 | + if (args.trim() === 'clear') { |
| 6 | + bot.memory.clear(); |
3 | 7 |
|
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() { |
17 | 19 | if (req.status !== 200) {
|
18 | 20 | var resp = '';
|
19 | 21 | if (req.responseText) {
|
20 | 22 | resp = '\n' + req.responseText.match(/.{1,400}/g).join('\n');
|
21 | 23 | }
|
22 |
| - return 'Failed: ' + req.status + ': ' + req.statusText + resp; |
| 24 | + args.reply('Failed: ' + req.status + ': ' + req.statusText + resp); |
23 | 25 | }
|
24 |
| - |
| 26 | + |
25 | 27 | var resp = JSON.parse(req.responseText);
|
26 |
| - |
| 28 | + |
27 | 29 | bot.memory.data = JSON.parse(resp.files['bot.json'].content);
|
28 | 30 | 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 | + }; |
29 | 37 |
|
30 |
| - return "Imported and persisted successfully. Please restart the bot."; |
31 |
| - }, |
| 38 | + bot.addCommand({ |
| 39 | + name : 'import', |
| 40 | + fun : doImport, |
32 | 41 | permissions : { del : 'NONE', use : 'OWNER' },
|
33 | 42 | description : 'Imports the persistent memory described in args `/export <exported-content>`'
|
34 | 43 | });
|
|
0 commit comments