From 211a0de564c28f1800dbf9b7745d525eb5ba4d23 Mon Sep 17 00:00:00 2001 From: hydrabolt Date: Wed, 28 Oct 2015 12:28:38 +0000 Subject: [PATCH] Updated examples --- examples/permissions/colortest.js | 100 +++++++++++++--------------- examples/permissions/permissions.js | 6 +- lib/Client.js | 7 ++ ref/colours.js | 8 ++- src/Client.js | 7 ++ 5 files changed, 73 insertions(+), 55 deletions(-) diff --git a/examples/permissions/colortest.js b/examples/permissions/colortest.js index e876e653b..2ff6b2c26 100644 --- a/examples/permissions/colortest.js +++ b/examples/permissions/colortest.js @@ -22,85 +22,81 @@ bot.on("disconnected", function () { }); bot.on("message", function (msg) { + + // to use this example, you first have to send 'create role' + // you can then change colors afterwards. + if (msg.content === "create role") { // create the role and add the user to it bot.createRoleIfNotExists(msg.channel.server, { - name : "Custom Colors", - hoist : true, // so it is visible in the members list - }).then(function(permission){ + name: "Custom Colors", + hoist: true, // so it is visible in the members list + }).then(function (permission) { + // this is executed when the role has been created or exists - bot.addMemberToRole(msg.sender, permission).then(function(){ - + // adds the sender to the role + bot.addMemberToRole(msg.sender, permission).then(function () { bot.reply(msg, "added you to the role!"); - }); - + }); - + } if (msg.content.indexOf("preset color") === 0) { // set the role to a preset color var colorName = msg.content.split(" ")[2]; - var role = msg.channel.server.getRole("name", "Custom Colors"); - if(Discord.Color[colorName]){ // if the color is a preset + // get the role by its name + var role = msg.channel.server.getRole("name", "Custom Colors"); + + // if the color exists as a preset + if (Discord.Color[colorName]) { + // update the role with the new color bot.updateRole(role, { - color : Discord.Color[colorName] - }).then(function(){ - bot.reply(msg, "done!"); + color: Discord.Color[colorName] + }).then(function (role) { + // this executes if the change was correct + bot.reply(msg, "done! using the color " + Discord.Color.toHex(role.color)); }); - - }else{ + + } else { bot.reply(msg, "that color isn't a preset color!"); } } - + if (msg.content.indexOf("custom color") === 0) { - + + // valid custom colors must follow the format of any of the following: + /* + #ff0000 <- valid 7 digit hex (including #) + ff0000 <- valid 6 digit hex + 16711680 <- valid decimal number (this if #ff0000 as a decimal) + */ + var colorName = msg.content.split(" ")[2]; + + // get the role by its name var role = msg.channel.server.getRole("name", "Custom Colors"); - + + // updates the role with the given color bot.updateRole(role, { - color : colorName - }).then(function(){ - bot.reply(msg, "done!"); - }).catch(function(e){ - bot.reply(msg, "an error occurred. Was that a valid hex/dec color?"); - }) - - } - - if (msg.content === "remove me") { - // remove the user from the good people list, if it exists - var found = false; - - for (var role of msg.channel.server.roles) { - if (role.name === "good people") { - found = role; - break; - } - } - - if (found) { - // if the role exists + color: colorName + }).then(function (role) { - if (msg.sender.hasRole(role)) { - // remove the member from the role - bot.removeMemberFromRole(msg.sender, role); - bot.reply(msg, "removed!") - } else { - bot.reply(msg, "you're not in the role!"); - } - - } else { - // role doesn't exist - bot.reply(msg, "the role doesn't even exist!"); - } + // this executes if the change was successful + bot.reply(msg, "done! using the color " + Discord.Color.toHex(role.color)); + + }).catch(function (e) { + + // this executes if it wasn't successful + bot.reply(msg, "an error occurred. Was that a valid hex/dec color?"); + + }) } diff --git a/examples/permissions/permissions.js b/examples/permissions/permissions.js index 518e05ccf..9f7c7775f 100644 --- a/examples/permissions/permissions.js +++ b/examples/permissions/permissions.js @@ -2,6 +2,8 @@ var Discord = require("../../"); +Discord.patchStrings(); + var AuthDetails = require("../auth.json"); var bot = new Discord.Client(); @@ -20,9 +22,9 @@ bot.on("message", (msg) => { var permissions = msg.channel.permissionsOf(user); if(permissions.sendTTSMessages){ - bot.reply(msg, "You *can* send TTS messages."); + bot.reply(msg, "You " + "can".italic.bold + " send TTS messages."); }else{ - bot.reply(msg, "You *can't* send TTS messages."); + bot.reply(msg, "You " + "can't".italic.bold + " send TTS messages."); } }else if(msg.content === "what are my full permissions?"){ diff --git a/lib/Client.js b/lib/Client.js index e8a4920c9..01048cc3f 100644 --- a/lib/Client.js +++ b/lib/Client.js @@ -825,6 +825,13 @@ var Client = (function (_EventEmitter) { var server = role.server.id; + if (isNaN(Color.toDec(data.color))) { + var err = new Error("Invalid Color"); + reject(err); + cb(err); + return; + } + request.patch(Endpoints.SERVERS + "/" + server + "/roles/" + role.id).set("authorization", self.token).send({ color: Color.toDec(data.color) || role.color, hoist: data.hoist || role.hoist, diff --git a/ref/colours.js b/ref/colours.js index 2c6bf59b3..b621a9c69 100644 --- a/ref/colours.js +++ b/ref/colours.js @@ -57,6 +57,12 @@ exports.toDec = function (data) { exports.toHex = function (data) { - return "#" + data.toString(16); + var text = data.toString(16); + + while(text.length < 6){ + text = "0" + text; + } + + return "#" + text; } \ No newline at end of file diff --git a/src/Client.js b/src/Client.js index c6e6c6631..2e8b19afd 100644 --- a/src/Client.js +++ b/src/Client.js @@ -874,6 +874,13 @@ class Client extends EventEmitter { var server = role.server.id; + if(isNaN(Color.toDec(data.color))){ + var err = new Error("Invalid Color"); + reject(err); + cb(err); + return; + } + request .patch(`${Endpoints.SERVERS}/${server}/roles/${role.id}`) .set("authorization", self.token)