Skip to content
This repository has been archived by the owner on Jul 16, 2022. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Add roleinfo command (#394)
* Initial roleinfo progress

* Extract roles per page into variable

* Roleinfo changes

* Some more changes half a year later

* Something changed that I have to commit now to mess with git

* Finishing touches

* You won't belive what this commit does!

* This commit will blow your socks off!
  • Loading branch information
wasdennnoch authored and Gilbert142 committed Dec 4, 2018
1 parent 13d76fe commit 8d590de
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
93 changes: 93 additions & 0 deletions Commands/Public/roleinfo.js
@@ -0,0 +1,93 @@
const moment = require("moment");
const PaginatedEmbed = require("../../Modules/MessageUtils/PaginatedEmbed");
const Permissions = require("discord.js/src/util/Permissions.js");

const rolesPerPage = 25;

module.exports = async ({ client, Constants: { Colors, Text }, Utils: { TitlecasePermissions } }, documents, msg, commandData) => {
if (!msg.suffix) {
const guildRoles = [...msg.guild.roles.values()].sort((a, b) => b.position - a.position);
const descriptions = [];
for (let i = 0; i < guildRoles.length; i += rolesPerPage) {
const roleSegment = guildRoles.slice(i, i + rolesPerPage).join("\n");
descriptions.push(`These are the roles on this server:\n\n${i ? `...${i} previous roles\n` : ""}${roleSegment}${i + rolesPerPage < guildRoles.length ? `\n...and ${guildRoles.length - i - rolesPerPage} more` : ""}`);
}

const memberRoles = [...msg.member.roles.values()].sort((a, b) => b.position - a.position);
const totalPermissionsBitfield = memberRoles.reduce((a, b) => a | b.permissions, 0); // eslint-disable-line no-bitwise
const totalPermissions = new Permissions(totalPermissionsBitfield);
for (let i = 0; i < memberRoles.length; i += rolesPerPage) {
const roleSegment = memberRoles.slice(i, i + rolesPerPage).join("\n");
descriptions.push([
`You currently have those roles:\n\n${i ? `...${i} previous roles\n` : ""}${roleSegment}${i + rolesPerPage < memberRoles.length ? `\n...and ${memberRoles.length - i - rolesPerPage} more` : ""}`,
"\nYour roles grant you get the following permissions:",
totalPermissionsBitfield ? `\`\`\`${TitlecasePermissions(totalPermissions.toArray(false).join(", "))}\`\`\`` : "You do not have any permissions on this server ⁉️",
totalPermissions.has(Permissions.FLAGS.ADMINISTRATOR, false) ? "⚠️ You have Administrator permissions which bypasses any other permission or override" : "",
].join("\n"));
}
if (descriptions.length === 2 && descriptions[0].length + descriptions[1].length < 2048) {
return msg.send({
embed: {
color: Colors.INFO,
title: `This guild has ${guildRoles.length} roles`,
description: `${descriptions[0]}\n\n${descriptions[1]}`,
},
});
}

await new PaginatedEmbed(msg, {
color: Colors.INFO,
title: `This guild has ${guildRoles.length} roles`,
description: `{description}`,
footer: `Page {currentPage} out of {totalPages}`,
}, {
descriptions,
}).init();
} else {
let role;
try {
role = await client.roleSearch(msg.suffix, msg.guild);
} catch (err) {
if (err.code === "FAILED_TO_FIND") {
return msg.send({
embed: {
color: Colors.SOFT_ERR,
title: "What's that role? 🏷️",
description: "I do not know anything about a role with that name.",
footer: {
text: "You can find a list of all roles by running this command without any arguments.",
},
},
});
}
throw err;
}
const permissions = role.permissions.toArray(false).join(", ");
const elements = [
`🎨 Color: **${role.color ? role.hexColor.toUpperCase() : "None"}**`,
`πŸ‘₯ Members: **${role.members.size}**`,
`#⃣ Position: **${msg.guild.roles.size - role.position}**`,
`πŸ•’ Created: **${moment(role.createdTimestamp).fromNow()}**`,
];
if (role.mentionable) {
elements.push("πŸ“’ Mentionable by everyone");
}
if (role.hoist) {
elements.push("πŸ“Œ Hoisted in member list");
}
if (role.managed) {
elements.push("πŸ€– Managed by an integration");
}
elements.push(`βœ… Permissions:${permissions.length ? `\n\`\`\`${TitlecasePermissions(permissions)}\`\`\`` : " This role does not grant any additional permissions"}`);
if (role.permissions.has(Permissions.FLAGS.ADMINISTRATOR, false)) {
elements.push("⚠️ This role grants Administrator permissions which bypasses any other permission or override");
}
return msg.send({
embed: {
title: `Information about role ${role.name} :: ${role.id}`,
color: role.color || null,
description: elements.join("\n"),
},
});
}
};
1 change: 1 addition & 0 deletions Modules/Utils/TitlecasePermissions.js
@@ -0,0 +1 @@
module.exports = str => str.replace(/\w+(_\w+)*/g, match => match.split("_").map(v => `${v.charAt(0)}${v.substr(1).toLowerCase()}`).join(" "));
1 change: 1 addition & 0 deletions Modules/Utils/index.js
Expand Up @@ -22,4 +22,5 @@ module.exports = {
StreamerUtils: require("./StreamerUtils.js"),
StreamingRSS: require("./StreamingRSS.js"),
StructureExtender: require("./StructureExtender.js"),
TitlecasePermissions: require("./TitlecasePermissions.js"),
};

0 comments on commit 8d590de

Please sign in to comment.