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

Add roleinfo command #394

Merged
merged 8 commits into from
Dec 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
93 changes: 93 additions & 0 deletions Commands/Public/roleinfo.js
Original file line number Diff line number Diff line change
@@ -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`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Title is inconsistent with title of non-paged message. (Missing ":")

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;
Gilbert142 marked this conversation as resolved.
Show resolved Hide resolved
}
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"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Single new-line might be too crowded. Consider using two instead?

},
});
}
};
1 change: 1 addition & 0 deletions Modules/Utils/TitlecasePermissions.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ module.exports = {
StreamerUtils: require("./StreamerUtils.js"),
StreamingRSS: require("./StreamingRSS.js"),
StructureExtender: require("./StructureExtender.js"),
TitlecasePermissions: require("./TitlecasePermissions.js"),
};