Created
January 13, 2017 15:02
-
-
Save a1994sc/819ab9a630ab0708d7b2d43385c028f6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const Discord = require("discord.js") | |
const reader = require("fs") | |
const request = require("request") | |
const client = new Discord.Client() | |
var config = {} | |
try { | |
config = require("./config.json") | |
} catch(ex) { | |
config.debug = false | |
config.commandPrefix = '!' | |
config.token = "NULL" | |
reader.writeFile("./config.json", JSON.stringify(config, null, 2)) | |
} | |
//optional | |
var decks = {} | |
try { | |
decks = require("./decks.json") | |
} catch(ex) { | |
request({ | |
method: 'GET', | |
url: 'https://deckofcardsapi.com/api/deck/new/shuffle/', | |
qs: { | |
deck_count: 1 | |
} | |
}, function(err, response, body) { | |
var parBod = JSON.parse(body) | |
decks.current = { | |
id: parBod.deck_id, | |
shuffled: parBod.shuffled, | |
remaining: parBod.remaining | |
} | |
reader.writeFileSync("./decks.json", JSON.stringify(decks, null, 2), 'utf8') | |
}) | |
} | |
//optional | |
process.once('SIGUSR2', function () { | |
gracefulShutdown(function () { | |
client.msg.channel.sendMessage("Be right back, I have a change to make") | |
client.destroy() | |
process.kill(process.pid, 'SIGUSR2'); | |
}); | |
}); | |
var commands = { | |
"deck" : { | |
usage: [ | |
{ option: "new", | |
description: "crates a new deck"}, | |
{ option: "draw", | |
description: "draws a card"}, | |
{ option: "name", | |
description: "names the current deck"}], | |
description: "command to use virtual deck", | |
process: function(bot, msg, suffix) { | |
var opt = suffix.split(" ")[0] | |
if (opt === "new") { | |
} else if (opt === "draw") { | |
} else if (opt === "name") { | |
} else { | |
var info = "" | |
for (var i = 0; i < usage.length; i++) { | |
info += "\n\t" + usage[i].option | |
info += "\n\t\t" + usage[i].description | |
} | |
msg.channel.sendMessage(info) | |
} | |
} | |
}, | |
"leave" : { | |
description: "command to have the bot leave", | |
process: function(bot, msg, suffix) { | |
msg.channel.sendMessage("Ok, then I will be off.") | |
bot.destroy() | |
} | |
}, | |
"load" : { | |
usage: [ | |
{ option: "json", | |
description: "loads a json"} | |
], | |
description: "command to return a JSON text", | |
process: function(bot, msg, suffix) { | |
var opts = suffix.trim().split(" ") | |
if (opts[0] === "json") | |
request({ | |
method: 'GET', | |
headers: { | |
'User-Agent': 'request' | |
}, | |
url: opts[1] | |
}, function(err, response, body) { | |
try { | |
var json = JSON.parse(body) | |
var gsm = json | |
for(var i = 2; i < opts.length; i++) { | |
gsm = gsm[opts[i]] | |
} | |
msg.channel.sendMessage("```\n" + JSON.stringify(gsm, null, 2) + "\n```") | |
} catch(ex) { | |
msg.channel.sendMessage("Sorry, I could not load that") | |
} | |
}) | |
else | |
msg.channel.sendMessage("Sorry, I did not get that..") | |
} | |
}, | |
"buff" : { | |
usage: [ | |
{ option: "encode, decode, <connent>", | |
description: "command that takes the encoding of the content, and decodes it"}, | |
{ option: "supported types", | |
description: "ascii, utf8, utf16le, base64, binary, hex"} | |
], | |
description: "can encode and decode content", | |
process: function(bot, msg, suffix) { | |
var enc = suffix.substr(0, suffix.indexOf(',')).trim() | |
var tmp = suffix.indexOf(',', suffix.indexOf(',')+1) | |
var dec = suffix.substr(suffix.indexOf(',')+1, (tmp - suffix.indexOf(',') - 1)).trim() | |
var con = suffix.substr(tmp+1).trim() | |
var buff = Buffer.from(con, enc) | |
msg.channel.sendMessage(buff.toString(dec)) | |
} | |
} | |
} | |
//optional | |
try { | |
require("./log.txt") | |
} catch(ex) { | |
reader.writeFile("./log.txt", "", 'utf8') | |
} | |
//optional | |
function logMsg(msg) { | |
logMsg(msg, false) | |
} | |
//optional | |
function logMsg(msg, over) { | |
console.log(msg) | |
} | |
function checkMessageForCommand(msg, isEdit) { | |
if(msg.author.id != client.user.id && (msg.content[0] === config.commandPrefix)) { | |
logMsg("msg:" + msg.content + " from: " + msg.author) | |
var cmdText = msg.content.split(" ")[0].substr(1) | |
var suffix = msg.content.substring(cmdText.length+1) | |
var cmd = commands[cmdText] | |
if (cmdText === "help") { | |
if (suffix) { | |
var cmds = suffix.split(" ").filter(function(cmd) { | |
return commands[cmd] | |
}) | |
var info = "" | |
for (var i = 0; i < cmds.length; i++) { | |
info += getCommandString(cmds[i]) | |
} | |
msg.channel.sendMessage(info) | |
} else { | |
msg.author.sendMessage("Commands").then(function() { | |
var batch = "" | |
var sortedCommands = Object.keys(commands).sort() | |
for(var i in sortedCommands) { | |
var cmd = sortedCommands[i] | |
var info = getCommandString(cmd) | |
var newBatch = batch + "\n" + info | |
if(newBatch.length > (1024 - 8)) { | |
msg.author.sendMessage(batch) | |
batch = info | |
} else { | |
batch = newBatch | |
} | |
} | |
if(batch.length > 0) { | |
msg.author.sendMessage(batch) | |
} | |
}) | |
} | |
} else if (cmd) { | |
try { | |
cmd.process(client, msg, suffix) | |
} catch(ex) { | |
var msgText = "Commnad " + cmdText + " failed" | |
if (config.debug) { | |
msgText += "\n" + ex.stack | |
} | |
msg.channel.sendMessage(msgText) | |
} | |
} | |
} | |
} | |
function getCommandString(cmd) { | |
var info = "```Markdown\n" + config.commandPrefix + cmd | |
var desc = commands[cmd].description | |
if (desc) { | |
info += "\n#description" | |
info += "\n\t\t" + desc | |
} | |
var usage = commands[cmd].usage | |
if (usage && usage.length > 0) { | |
info += "\n#options" | |
for(var j = 0; j < usage.length; j++) { | |
info += "\n\t" + usage[j].option | |
info += "\n\t\t" + usage[j].description | |
} | |
} | |
return info + "```" | |
} | |
client.on('message', msg => checkMessageForCommand(msg, false)) | |
client.on('messageUpdate', (oldmsg, newmsg) => checkMessageForCommand(newmsg, true)) | |
client.on('ready', function() { | |
logMsg("Connected", true) | |
}) | |
client.on('destroy', function() { | |
logMsg("Disconnected", true) | |
process.exit(1) | |
}) | |
client.login(config.token) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment