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

Commit a58810d

Browse files
committed
Add support for exporting the bot memory to a GitHub Gist, and importing from a previous export. Also allow clearing of bot memory.
1 parent 20e64ea commit a58810d

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

source/plugins/export.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
(function() {
2+
"use strict";
3+
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+
17+
if (req.status !== 201) {
18+
var resp = '';
19+
if (req.responseText) {
20+
resp = '\n' + req.responseText.match(/.{1,400}/g).join('\n');
21+
}
22+
return 'Failed: ' + req.status + ': ' + req.statusText + resp;
23+
}
24+
25+
var resp = JSON.parse(req.responseText);
26+
27+
return 'Exported to gist, id: `' + resp.id + '` viewable at ' + resp.html_url;
28+
},
29+
permissions : { del : 'NONE', use : 'OWNER' },
30+
description : 'Blurts out a message with the persistent memory storage for export `/export`'
31+
});
32+
})();

source/plugins/import.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
(function() {
2+
"use strict";
3+
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+
17+
if (req.status !== 200) {
18+
var resp = '';
19+
if (req.responseText) {
20+
resp = '\n' + req.responseText.match(/.{1,400}/g).join('\n');
21+
}
22+
return 'Failed: ' + req.status + ': ' + req.statusText + resp;
23+
}
24+
25+
var resp = JSON.parse(req.responseText);
26+
27+
bot.memory.data = JSON.parse(resp.files['bot.json'].content);
28+
bot.memory.save();
29+
30+
return "Imported and persisted successfully. Please restart the bot.";
31+
},
32+
permissions : { del : 'NONE', use : 'OWNER' },
33+
description : 'Imports the persistent memory described in args `/export <exported-content>`'
34+
});
35+
})();

0 commit comments

Comments
 (0)