This repository has been archived by the owner on Jul 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for exporting the bot memory to a GitHub Gist, and import…
…ing from a previous export. Also allow clearing of bot memory.
- Loading branch information
Showing
2 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
(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) | ||
} | ||
} | ||
})); | ||
|
||
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; | ||
} | ||
|
||
var resp = JSON.parse(req.responseText); | ||
|
||
return 'Exported to gist, id: `' + resp.id + '` viewable at ' + resp.html_url; | ||
}, | ||
permissions : { del : 'NONE', use : 'OWNER' }, | ||
description : 'Blurts out a message with the persistent memory storage for export `/export`' | ||
}); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
(function() { | ||
"use strict"; | ||
|
||
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); | ||
|
||
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; | ||
} | ||
|
||
var resp = JSON.parse(req.responseText); | ||
|
||
bot.memory.data = JSON.parse(resp.files['bot.json'].content); | ||
bot.memory.save(); | ||
|
||
return "Imported and persisted successfully. Please restart the bot."; | ||
}, | ||
permissions : { del : 'NONE', use : 'OWNER' }, | ||
description : 'Imports the persistent memory described in args `/export <exported-content>`' | ||
}); | ||
})(); |