Updated examples

This commit is contained in:
hydrabolt
2015-10-28 12:28:38 +00:00
parent c3b6dba6be
commit 211a0de564
5 changed files with 73 additions and 55 deletions

View File

@@ -22,85 +22,81 @@ bot.on("disconnected", function () {
}); });
bot.on("message", function (msg) { 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") { if (msg.content === "create role") {
// create the role and add the user to it // create the role and add the user to it
bot.createRoleIfNotExists(msg.channel.server, { bot.createRoleIfNotExists(msg.channel.server, {
name : "Custom Colors", name: "Custom Colors",
hoist : true, // so it is visible in the members list hoist: true, // so it is visible in the members list
}).then(function(permission){ }).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!"); bot.reply(msg, "added you to the role!");
}); });
}); });
} }
if (msg.content.indexOf("preset color") === 0) { if (msg.content.indexOf("preset color") === 0) {
// set the role to a preset color // set the role to a preset color
var colorName = msg.content.split(" ")[2]; 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, { bot.updateRole(role, {
color : Discord.Color[colorName] color: Discord.Color[colorName]
}).then(function(){ }).then(function (role) {
bot.reply(msg, "done!"); // 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!"); bot.reply(msg, "that color isn't a preset color!");
} }
} }
if (msg.content.indexOf("custom color") === 0) { 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]; var colorName = msg.content.split(" ")[2];
// get the role by its name
var role = msg.channel.server.getRole("name", "Custom Colors"); var role = msg.channel.server.getRole("name", "Custom Colors");
// updates the role with the given color
bot.updateRole(role, { bot.updateRole(role, {
color : colorName color: colorName
}).then(function(){ }).then(function (role) {
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
if (msg.sender.hasRole(role)) { // this executes if the change was successful
// remove the member from the role bot.reply(msg, "done! using the color " + Discord.Color.toHex(role.color));
bot.removeMemberFromRole(msg.sender, role);
bot.reply(msg, "removed!") }).catch(function (e) {
} else {
bot.reply(msg, "you're not in the role!"); // this executes if it wasn't successful
} bot.reply(msg, "an error occurred. Was that a valid hex/dec color?");
} else { })
// role doesn't exist
bot.reply(msg, "the role doesn't even exist!");
}
} }

View File

@@ -2,6 +2,8 @@
var Discord = require("../../"); var Discord = require("../../");
Discord.patchStrings();
var AuthDetails = require("../auth.json"); var AuthDetails = require("../auth.json");
var bot = new Discord.Client(); var bot = new Discord.Client();
@@ -20,9 +22,9 @@ bot.on("message", (msg) => {
var permissions = msg.channel.permissionsOf(user); var permissions = msg.channel.permissionsOf(user);
if(permissions.sendTTSMessages){ if(permissions.sendTTSMessages){
bot.reply(msg, "You *can* send TTS messages."); bot.reply(msg, "You " + "can".italic.bold + " send TTS messages.");
}else{ }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?"){ }else if(msg.content === "what are my full permissions?"){

View File

@@ -825,6 +825,13 @@ var Client = (function (_EventEmitter) {
var server = role.server.id; 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({ request.patch(Endpoints.SERVERS + "/" + server + "/roles/" + role.id).set("authorization", self.token).send({
color: Color.toDec(data.color) || role.color, color: Color.toDec(data.color) || role.color,
hoist: data.hoist || role.hoist, hoist: data.hoist || role.hoist,

View File

@@ -57,6 +57,12 @@ exports.toDec = function (data) {
exports.toHex = function (data) { exports.toHex = function (data) {
return "#" + data.toString(16); var text = data.toString(16);
while(text.length < 6){
text = "0" + text;
}
return "#" + text;
} }

View File

@@ -874,6 +874,13 @@ class Client extends EventEmitter {
var server = role.server.id; var server = role.server.id;
if(isNaN(Color.toDec(data.color))){
var err = new Error("Invalid Color");
reject(err);
cb(err);
return;
}
request request
.patch(`${Endpoints.SERVERS}/${server}/roles/${role.id}`) .patch(`${Endpoints.SERVERS}/${server}/roles/${role.id}`)
.set("authorization", self.token) .set("authorization", self.token)