From 446e6db437de21726e0f812cd2d21fcc4ee346ff Mon Sep 17 00:00:00 2001 From: Ded Shot Date: Wed, 15 Apr 2020 07:25:02 -0400 Subject: [PATCH 1/3] Added role info command --- src/commands/roleinfo.ts | 99 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 src/commands/roleinfo.ts diff --git a/src/commands/roleinfo.ts b/src/commands/roleinfo.ts new file mode 100644 index 0000000..5b501f7 --- /dev/null +++ b/src/commands/roleinfo.ts @@ -0,0 +1,99 @@ +import { Message } from 'eris'; +import { Client, Command } from '../class'; + +export default class Roleinfo extends Command { + constructor(client: Client) { + super(client); + this.name = 'roleinfo'; + this.description = 'Get information about a role.'; + this.permissions = 0; + this.guildOnly = true; + this.enabled = true; + } + + public async run(message: Message, args: Array) { + if (!args[0]) return this.client.createMessage(message.channel.id, `${this.client.util.emojis.ERROR} You need to specifiy a role ID or a role name.`); + + let role: any; + if (!isNaN(args[0])) { // if it's a role id + // @ts-ignore + role = message.channel.guild.roles.find((role) => role.id == args[0]); + } else if (isNaN(args[0])) { // if it's a role name + // @ts-ignore + role = message.channel.guild.roles.find((role) => role.name == args[0]); + } + + const ms = role.createdAt; + const date = new Date(ms).toLocaleDateString('en-us'); + const time = new Date(ms).toLocaleTimeString('en-us'); + + const perms = role.permissions; + let permsArray: any = []; + if (perms.has('administrator')) permsArray.push('Administrator'); + if (perms.has('manageGuild')) permsArray.push('Manage Server'); + if (perms.has('manageChannels')) permsArray.push('Manage Channels'); + if (perms.has('manageRoles')) permsArray.push('Manage Roles'); + if (perms.has('manageMessages')) permsArray.push('Manage Messages'); + if (perms.has('manageNicknames')) permsArray.push('Manage Nicknames'); + if (perms.has('manageEmojis')) permsArray.push('Manage Emojis'); + if (perms.has('banMembers')) permsArray.push('Ban Members'); + if (perms.has('kickMembers')) permsArray.push('Kick Members'); + + let hasPerms: boolean; + if (permsArray.length > 0) { + permsArray = permsArray.join(', '); + hasPerms = true; + } + + const embed = { embed: { + description: `<@&${role.id}> ID: \`${role.id}\``, + author: { + name: `${message.author.username}#${message.author.discriminator}`, + icon_url: message.author.avatarURL, + }, + color: role.color, + fields: [ + { + name: 'Name', + value: role.name, + inline: true, + }, + { + name: 'Color', + value: `#${role.color.toString(16)}`, + inline: true, + }, + { + name: 'Hoisted', + value: role.hoist, + inline: true, + }, + { + name: 'Position', + value: role.position, + inline: true, + }, + { + name: 'Creation Date', + value: `${date} ${time}`, + inline: true, + }, + { + name: 'Mentionable', + value: role.mentionable, + inline: true, + }, + ], + footer: { + text: `${this.client.user.username}`, + icon_url: this.client.user.avatarURL, + }, + timestamp: new Date(), + } }; + + if (hasPerms) { + embed.embed.fields.push({ name: 'Permissions', value: permsArray, inline: false }); + } + message.channel.createMessage(embed); + } +} From 4141482c211cb843cf46f724cb522074f21dfe14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?DedShot=E2=84=A2=239195?= Date: Wed, 15 Apr 2020 09:54:19 -0400 Subject: [PATCH 2/3] Added try/catch block --- src/commands/roleinfo.ts | 168 ++++++++++++++++++++------------------- 1 file changed, 87 insertions(+), 81 deletions(-) diff --git a/src/commands/roleinfo.ts b/src/commands/roleinfo.ts index 5b501f7..d618117 100644 --- a/src/commands/roleinfo.ts +++ b/src/commands/roleinfo.ts @@ -11,89 +11,95 @@ export default class Roleinfo extends Command { this.enabled = true; } - public async run(message: Message, args: Array) { - if (!args[0]) return this.client.createMessage(message.channel.id, `${this.client.util.emojis.ERROR} You need to specifiy a role ID or a role name.`); + public async run(message: Message, args: any) { + try { + if (!args[0]) return this.client.createMessage(message.channel.id, `${this.client.util.emojis.ERROR} You need to specifiy a role ID or a role name.`); - let role: any; - if (!isNaN(args[0])) { // if it's a role id + let role: any; + if (!isNaN(args[0])) { // if it's a role id // @ts-ignore - role = message.channel.guild.roles.find((role) => role.id == args[0]); - } else if (isNaN(args[0])) { // if it's a role name - // @ts-ignore - role = message.channel.guild.roles.find((role) => role.name == args[0]); + role = message.channel.guild.roles.find((role) => role.id == args[0]); + } else if (isNaN(args[0])) { // if it's a role name + args = args.join(' '); + // @ts-ignore + role = message.channel.guild.roles.find((role) => role.name == args); + } + if (!role) return this.client.createMessage(message.channel.id, `${this.client.util.emojis.ERROR} Could not find role.`); + + const ms = role.createdAt; + const date = new Date(ms).toLocaleDateString('en-us'); + const time = new Date(ms).toLocaleTimeString('en-us'); + + const perms = role.permissions; + let permsArray: any = []; + if (perms.has('administrator')) permsArray.push('Administrator'); + if (perms.has('manageGuild')) permsArray.push('Manage Server'); + if (perms.has('manageChannels')) permsArray.push('Manage Channels'); + if (perms.has('manageRoles')) permsArray.push('Manage Roles'); + if (perms.has('manageMessages')) permsArray.push('Manage Messages'); + if (perms.has('manageNicknames')) permsArray.push('Manage Nicknames'); + if (perms.has('manageEmojis')) permsArray.push('Manage Emojis'); + if (perms.has('banMembers')) permsArray.push('Ban Members'); + if (perms.has('kickMembers')) permsArray.push('Kick Members'); + + let hasPerms: boolean; + if (permsArray.length > 0) { + permsArray = permsArray.join(', '); + hasPerms = true; + } + + const embed = { embed: { + description: `<@&${role.id}> ID: \`${role.id}\``, + author: { + name: `${message.author.username}#${message.author.discriminator}`, + icon_url: message.author.avatarURL, + }, + color: role.color, + fields: [ + { + name: 'Name', + value: role.name, + inline: true, + }, + { + name: 'Color', + value: `#${role.color.toString(16)}`, + inline: true, + }, + { + name: 'Hoisted', + value: role.hoist, + inline: true, + }, + { + name: 'Position', + value: role.position, + inline: true, + }, + { + name: 'Creation Date', + value: `${date} ${time}`, + inline: true, + }, + { + name: 'Mentionable', + value: role.mentionable, + inline: true, + }, + ], + footer: { + text: `${this.client.user.username}`, + icon_url: this.client.user.avatarURL, + }, + timestamp: new Date(), + } }; + + if (hasPerms) { + embed.embed.fields.push({ name: 'Permissions', value: permsArray, inline: false }); + } + message.channel.createMessage(embed); + } catch (err) { + this.client.createMessage(message.channel.id, `${this.client.util.emojis.ERROR} An unexpected error has occured.`); } - - const ms = role.createdAt; - const date = new Date(ms).toLocaleDateString('en-us'); - const time = new Date(ms).toLocaleTimeString('en-us'); - - const perms = role.permissions; - let permsArray: any = []; - if (perms.has('administrator')) permsArray.push('Administrator'); - if (perms.has('manageGuild')) permsArray.push('Manage Server'); - if (perms.has('manageChannels')) permsArray.push('Manage Channels'); - if (perms.has('manageRoles')) permsArray.push('Manage Roles'); - if (perms.has('manageMessages')) permsArray.push('Manage Messages'); - if (perms.has('manageNicknames')) permsArray.push('Manage Nicknames'); - if (perms.has('manageEmojis')) permsArray.push('Manage Emojis'); - if (perms.has('banMembers')) permsArray.push('Ban Members'); - if (perms.has('kickMembers')) permsArray.push('Kick Members'); - - let hasPerms: boolean; - if (permsArray.length > 0) { - permsArray = permsArray.join(', '); - hasPerms = true; - } - - const embed = { embed: { - description: `<@&${role.id}> ID: \`${role.id}\``, - author: { - name: `${message.author.username}#${message.author.discriminator}`, - icon_url: message.author.avatarURL, - }, - color: role.color, - fields: [ - { - name: 'Name', - value: role.name, - inline: true, - }, - { - name: 'Color', - value: `#${role.color.toString(16)}`, - inline: true, - }, - { - name: 'Hoisted', - value: role.hoist, - inline: true, - }, - { - name: 'Position', - value: role.position, - inline: true, - }, - { - name: 'Creation Date', - value: `${date} ${time}`, - inline: true, - }, - { - name: 'Mentionable', - value: role.mentionable, - inline: true, - }, - ], - footer: { - text: `${this.client.user.username}`, - icon_url: this.client.user.avatarURL, - }, - timestamp: new Date(), - } }; - - if (hasPerms) { - embed.embed.fields.push({ name: 'Permissions', value: permsArray, inline: false }); - } - message.channel.createMessage(embed); } } From 8678b0878b7142945e47e8134d94f001fd713097 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?DedShot=E2=84=A2=239195?= Date: Wed, 15 Apr 2020 20:14:20 -0400 Subject: [PATCH 3/3] Used RichEmbed class --- src/commands/roleinfo.ts | 66 +++++++++++----------------------------- 1 file changed, 17 insertions(+), 49 deletions(-) diff --git a/src/commands/roleinfo.ts b/src/commands/roleinfo.ts index d618117..a55df68 100644 --- a/src/commands/roleinfo.ts +++ b/src/commands/roleinfo.ts @@ -1,11 +1,12 @@ import { Message } from 'eris'; -import { Client, Command } from '../class'; +import { Client, Command, RichEmbed } from '../class'; export default class Roleinfo extends Command { constructor(client: Client) { super(client); this.name = 'roleinfo'; this.description = 'Get information about a role.'; + this.usage = "roleinfo [role ID or role name]" this.permissions = 0; this.guildOnly = true; this.enabled = true; @@ -48,56 +49,23 @@ export default class Roleinfo extends Command { hasPerms = true; } - const embed = { embed: { - description: `<@&${role.id}> ID: \`${role.id}\``, - author: { - name: `${message.author.username}#${message.author.discriminator}`, - icon_url: message.author.avatarURL, - }, - color: role.color, - fields: [ - { - name: 'Name', - value: role.name, - inline: true, - }, - { - name: 'Color', - value: `#${role.color.toString(16)}`, - inline: true, - }, - { - name: 'Hoisted', - value: role.hoist, - inline: true, - }, - { - name: 'Position', - value: role.position, - inline: true, - }, - { - name: 'Creation Date', - value: `${date} ${time}`, - inline: true, - }, - { - name: 'Mentionable', - value: role.mentionable, - inline: true, - }, - ], - footer: { - text: `${this.client.user.username}`, - icon_url: this.client.user.avatarURL, - }, - timestamp: new Date(), - } }; - + let embed = new RichEmbed() + embed.setDescription(`<@&${role.id}> ID: \`${role.id}\``) + embed.setAuthor(`${message.author.username}#${message.author.discriminator}`, message.author.avatarURL) + embed.setColor(role.color) + embed.addField("Name", role.name, true) + embed.addField("Color", `#${role.color.toString(16)}`, true) + embed.addField("Hoisted", role.hoist.toString(), true) + embed.addField("Position", role.position.toString(), true) + embed.addField("Creation Date", `${date} ${time}`, true) + embed.addField("Mentionnable", role.mentionable.toString(), true) + embed.setFooter(this.client.user.username, this.client.user.avatarURL) + embed.setTimestamp() + if (hasPerms) { - embed.embed.fields.push({ name: 'Permissions', value: permsArray, inline: false }); + embed.fields.push({ name: 'Permissions', value: permsArray, inline: false }); } - message.channel.createMessage(embed); + message.channel.createMessage({embed: embed}); } catch (err) { this.client.createMessage(message.channel.id, `${this.client.util.emojis.ERROR} An unexpected error has occured.`); }