diff --git a/backend/src/plugins/Utility/commands/InfoCmd.ts b/backend/src/plugins/Utility/commands/InfoCmd.ts index 2fd2256a..991c6fed 100644 --- a/backend/src/plugins/Utility/commands/InfoCmd.ts +++ b/backend/src/plugins/Utility/commands/InfoCmd.ts @@ -9,9 +9,11 @@ import { canReadChannel } from "../../../utils/canReadChannel"; import { getMessageInfoEmbed } from "../functions/getMessageInfoEmbed"; import { getChannelInfoEmbed } from "../functions/getChannelInfoEmbed"; import { getServerInfoEmbed } from "../functions/getServerInfoEmbed"; +import { getRoleInfoEmbed } from "../functions/getRoleInfoEmbed"; import { getChannelId } from "knub/dist/utils"; import { getGuildPreview } from "../functions/getGuildPreview"; import { getSnowflakeInfoEmbed } from "../functions/getSnowflakeInfoEmbed"; +import { getRoleId } from "knub/dist/utils"; export const InfoCmd = utilityCmd({ trigger: "info", @@ -99,14 +101,27 @@ export const InfoCmd = utilityCmd({ } } - // 7. Arbitrary ID + // 7. Role ID + const roleId = getRoleId(value); + const role = roleId && pluginData.guild.roles.get(roleId); + if (role) { + const embed = await getRoleInfoEmbed(pluginData, roleId, message.author.id); + if (embed) { + message.channel.createMessage({ embed }); + return; + } + } + + // TODO 8. Emoji ID + + // 9. Arbitrary ID if (isValidSnowflake(value)) { const embed = await getSnowflakeInfoEmbed(pluginData, value, true, message.author.id); message.channel.createMessage({ embed }); return; } - // 7. No can do + // 10. No can do sendErrorMessage(pluginData, message.channel, "Could not find anything with that value"); }, }); diff --git a/backend/src/plugins/Utility/functions/getRoleInfoEmbed.ts b/backend/src/plugins/Utility/functions/getRoleInfoEmbed.ts new file mode 100644 index 00000000..8643c718 --- /dev/null +++ b/backend/src/plugins/Utility/functions/getRoleInfoEmbed.ts @@ -0,0 +1,58 @@ +import { PluginData } from "knub"; +import { UtilityPluginType } from "../types"; +import { Constants, EmbedOptions } from "eris"; +import moment from "moment-timezone"; +import humanizeDuration from "humanize-duration"; +import { formatNumber, preEmbedPadding, trimLines } from "../../../utils"; +import { TimeAndDatePlugin } from "../../TimeAndDate/TimeAndDatePlugin"; + +export async function getRoleInfoEmbed( + pluginData: PluginData, + roleId: string, + requestMemberId?: string, +): Promise { + const role = pluginData.guild.roles.get(roleId); + if (!role) { + return null; + } + + const embed: EmbedOptions = { + fields: [] + } + + // TODO Perhaps use a '@' as icon? + // embed.author = { + // name: role.name, + // icon_url: icon, + // }; + + const createdAt = moment.utc(role.createdAt, "x"); + const timeAndDate = pluginData.getPlugin(TimeAndDatePlugin); + const tzCreatedAt = requestMemberId + ? await timeAndDate.inMemberTz(requestMemberId, createdAt) + : timeAndDate.inGuildTz(createdAt); + const prettyCreatedAt = tzCreatedAt.format(timeAndDate.getDateFormat("pretty_datetime")); + const roleAge = humanizeDuration(Date.now() - role.createdAt, { + largest: 2, + round: true + }); + + embed.fields.push({ + name: preEmbedPadding + "Role information", + value: trimLines(` + Name: **${role.name}** + ID: \`${role.id}\` + Created: **${roleAge} ago** (\`${prettyCreatedAt}\`) + Position: ${role.position} + Color: #${role.color.toString(16).toUpperCase()} + Mentionable: ${role.mentionable} + Hoisted: ${role.hoist} + Managed: ${role.managed} + Permissions: TODO + Members: TODO + Mention: <@&${role.id}> + `), + }); + + return embed; +}