diff --git a/README.md b/README.md index b0a6356..738a8cb 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@

- - askbuddie-icon - + + askbuddie-icon +

Ask Buddie Bot

diff --git a/askbuddie-bot.png b/askbuddie-bot.png new file mode 100644 index 0000000..87c0c11 Binary files /dev/null and b/askbuddie-bot.png differ diff --git a/src/commands/guru.ts b/src/commands/guru.ts new file mode 100644 index 0000000..c8ace92 --- /dev/null +++ b/src/commands/guru.ts @@ -0,0 +1,41 @@ +import { Message } from 'discord.js'; +import Command from 'src/libs/command'; + +export default class GuruCMD extends Command { + constructor() { + super({ + name: 'guru', + aliases: [''], + description: 'Please check the guide below on how to ask questions.' + }); + } + + execute(message: Message): void { + const embedObj = { + title: 'Guidelines', + description: this.description, + color: '#e53935', + fields: [ + { + name: "Don't ask to ask, just ask", + value: `Include as much as information you can to describe to problem directly. + Don't ask to ask, just ask it and the members will not have to ask you in return. + Such question will decrease the user engagement and the problem gets unnoticed most of the time. + ` + }, + { + name: 'Code formatting', + value: `No plain text or screenshot + Make use of markdown provided by Discord to format the code and improve readability. It will be easier for others to go through the code. Don't post the screenshot of code as well. Keep it clean with the available features. + Read more about markdown: https://www.markdownguide.org/cheat-sheet/ + + Apart from markdown you can use below platform for code sharing: + GitHub Gist: https://gist.github.com/ + CodeSandbox: https://codesandbox.io/s/ + Pastebin: https://pastebin.com/"` + } + ] + }; + message.channel.send({ embed: embedObj }); + } +} diff --git a/src/commands/index.ts b/src/commands/index.ts index 1250b44..6485622 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -1,2 +1,3 @@ export { default as help } from './help'; export { default as role } from './role'; +export { default as guru } from './guru'; diff --git a/src/commands/role.ts b/src/commands/role.ts index 5a27d27..8a9d703 100644 --- a/src/commands/role.ts +++ b/src/commands/role.ts @@ -23,6 +23,10 @@ interface RemoveParams extends Params { arg: string; } +interface CountParams extends Params { + args: string[]; +} + class RoleCMD extends Command { constructor() { super({ @@ -58,6 +62,10 @@ class RoleCMD extends Command { { name: '`--rm `', value: 'Remove an existing role. \n' + }, + { + name: '`--count []`', + value: 'Count members in one or all roles. \n' } ] }; @@ -132,13 +140,18 @@ class RoleCMD extends Command { } } - private getAvailableRoles(guild: Guild, botMember: GuildMember): RoleList { + private getBotRolePosition(botMember: GuildMember): number { // Get bot role position let botHighestPosition = 0; const botRole = botMember.roles.highest; if (botRole !== undefined) { botHighestPosition = botRole.rawPosition; } + return botHighestPosition; + } + + private getAvailableRoles(guild: Guild, botMember: GuildMember): RoleList { + const botHighestPosition = this.getBotRolePosition(botMember); // List of guild roles const roleList: RoleList = {}; @@ -154,6 +167,67 @@ class RoleCMD extends Command { return roleList; } + // Role count command + private async countCMD(params: CountParams): Promise { + const { guild, message, bot, args } = params; + const botMember: GuildMember = await guild.members.fetch(bot.id); + + const botHighestPosition = this.getBotRolePosition(botMember); + + // Get available roles + const roles: Collection = guild?.roles.cache.filter( + (role: Role) => { + if ( + botHighestPosition > role.rawPosition && + role.name.toLowerCase() !== '@everyone' + ) { + return true; + } + return false; + } + ); + + // count members of all roles + if (args.length === 0) { + const field = { name: '\u200b', value: '' }; + if (roles.size === 0) { + const msg = 'No public role available.'; + message.channel.send(msg); + return; + } else { + roles.map((role: Role) => { + field.value += `**${role.name}** : \`${role.members.size}\`\n`; + }); + } + const embedObj = { + title: 'Members count by role: \n', + color: '#e53935', + fields: [field] + }; + message.channel.send({ embed: embedObj }); + } else { + // count members of requested role + let found = false; + + roles.every((role: Role) => { + // found required role + if (args[0].toLowerCase() === role.name.toLowerCase()) { + message.channel.send( + `Total members in role **${role.name}** : \`${role.members.size}\`` + ); + found = true; + return false; + } + + return true; + }); + + if (!found) { + message.channel.send(`Role not found.`); + } + } + } + public async execute(message: Message, args: string[]): Promise { const first = args.shift(); @@ -187,6 +261,7 @@ class RoleCMD extends Command { await this.listRoles({ guild, message, bot }); return; + // remove role flag case '--rm': if (args.length === 0) { this.invalidCMD(message); @@ -201,6 +276,11 @@ class RoleCMD extends Command { return; + // count role flag + case '--count': + this.countCMD({ guild, message, bot, args }); + return; + default: // Single role if (first === undefined) {