Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
feat: add guru & role count command
  • Loading branch information
ashiishme committed Jul 28, 2021
2 parents 161d1a6 + 2b9c71e commit b417ebd
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 4 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<p align="center">
<a href="https://www.askbuddie.com">
<img src="https://raw.githubusercontent.com/askbuddie/readme/master/ask-buddie-icon-200x150.png" align="center" alt="askbuddie-icon"/>
</a>
<a href="https://www.askbuddie.com">
<img src="askbuddie-bot.png" align="center" alt="askbuddie-icon" width="300"/>
</a>
</p>
<h1 align="center" style="border: 0;"> Ask Buddie Bot </h1>

Expand Down
Binary file added askbuddie-bot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions src/commands/guru.ts
Original file line number Diff line number Diff line change
@@ -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.
<https://dontasktoask.com/>`
},
{
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 });
}
}
1 change: 1 addition & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { default as help } from './help';
export { default as role } from './role';
export { default as guru } from './guru';
82 changes: 81 additions & 1 deletion src/commands/role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ interface RemoveParams extends Params {
arg: string;
}

interface CountParams extends Params {
args: string[];
}

class RoleCMD extends Command {
constructor() {
super({
Expand Down Expand Up @@ -58,6 +62,10 @@ class RoleCMD extends Command {
{
name: '`--rm <role>`',
value: 'Remove an existing role. \n'
},
{
name: '`--count [<role>]`',
value: 'Count members in one or all roles. \n'
}
]
};
Expand Down Expand Up @@ -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 = {};
Expand All @@ -154,6 +167,67 @@ class RoleCMD extends Command {
return roleList;
}

// Role count command
private async countCMD(params: CountParams): Promise<void> {
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<string, Role> = 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<void> {
const first = args.shift();

Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand Down

0 comments on commit b417ebd

Please sign in to comment.