community-relations/src/commands/members.ts

86 lines
4.6 KiB
TypeScript
Raw Normal View History

2020-07-12 00:39:34 -04:00
import { Message } from 'eris';
import { createPaginationEmbed } from 'eris-pagination';
import { Client, Command, RichEmbed } from '../class';
2020-07-13 00:01:34 -04:00
import { members } from '.';
2020-07-12 00:39:34 -04:00
export default class extends Command {
constructor(client: Client) {
super(client);
this.name = 'members';
this.description = 'Gets a list of members in the server or members in a specific role.';
this.usage = `${this.client.config.prefix}members [role name]`;
this.guildOnly = true;
2020-07-13 00:01:34 -04:00
this.enabled = true;
2020-07-12 00:39:34 -04:00
}
public async run(message: Message, args: string[]) {
try {
await this.mainGuild.fetchAllMembers();
if (!args[0]) {
const embed = new RichEmbed();
const membersOnline = this.mainGuild.members.filter((member) => member.status === 'online');
const membersIdle = this.mainGuild.members.filter((member) => member.status === 'idle');
const membersDnd = this.mainGuild.members.filter((member) => member.status === 'dnd');
2020-07-13 00:01:34 -04:00
const membersOffline = this.mainGuild.members.filter((member) => member.status === 'offline' || member.status === undefined);
2020-07-12 00:39:34 -04:00
const membersBots = this.mainGuild.members.filter((member) => member.user.bot === true);
const membersHuman = this.mainGuild.members.filter((member) => member.user.bot === false);
embed.setTitle('Members');
2020-07-13 00:01:34 -04:00
embed.setDescription(`**Total:** ${this.mainGuild.members.size}\n**Humans:** ${membersHuman.length}\n**Bots:** ${membersBots.length}\n\n**<:online:732025023547834369> Online:** ${membersOnline.length}\n**<:idle:732025087896715344> Idle:** ${membersIdle.length}\n**<:dnd:732024861853089933> Do Not Disturb:** ${membersDnd.length}\n**<:offline:732024920518688849> Offline:** ${membersOffline.length}`);
2020-07-12 00:39:34 -04:00
embed.setFooter(this.client.user.username, this.client.user.avatarURL);
embed.setTimestamp();
return message.channel.createMessage({ embed });
}
const role = this.client.util.resolveRole(args.join(' '), this.mainGuild);
if (!role) return this.error(message.channel, 'The role you specified doesn\'t exist.');
2020-07-13 00:01:34 -04:00
const statusArray: string[] = [];
2020-07-12 00:39:34 -04:00
const membersOnline: string[] = [];
const membersIdle: string[] = [];
const membersDnd: string[] = [];
const membersOffline: string[] = [];
for (const member of this.mainGuild.members.filter((m) => m.roles.includes(role.id)).sort((a, b) => a.username.localeCompare(b.username))) {
switch (member.status) {
case 'online':
2020-07-13 00:01:34 -04:00
membersOnline.push(`<:online:732025023547834369> ${member.user.username}#${member.user.discriminator} | <@${member.user.id}>`);
2020-07-12 00:39:34 -04:00
break;
case 'idle':
2020-07-13 00:01:34 -04:00
membersIdle.push(`<:idle:732025087896715344> ${member.user.username}#${member.user.discriminator} | <@${member.user.id}>`);
2020-07-12 00:39:34 -04:00
break;
case 'dnd':
2020-07-13 00:01:34 -04:00
membersDnd.push(`<:dnd:732024861853089933> ${member.user.username}#${member.user.discriminator} | <@${member.user.id}>`);
2020-07-12 00:39:34 -04:00
break;
case 'offline':
2020-07-13 00:01:34 -04:00
membersOffline.push(`<:offline:732024920518688849> ${member.user.username}#${member.user.discriminator} | <@${member.user.id}>`);
2020-07-12 00:39:34 -04:00
break;
case undefined:
2020-07-13 00:01:34 -04:00
membersOffline.push(`<:offline:732024920518688849> ${member.user.username}#${member.user.discriminator} | <@${member.user.id}>`);
2020-07-12 00:39:34 -04:00
break;
default:
break;
}
}
2020-07-13 00:01:34 -04:00
if (membersOnline.length > 0) statusArray.push(membersOnline.join('\n'));
if (membersIdle.length > 0) statusArray.push(membersIdle.join('\n'));
if (membersDnd.length > 0) statusArray.push(membersDnd.join('\n'));
if (membersOffline.length > 0) statusArray.push(membersOffline.join('\n'));
const statusSplit = this.client.util.splitString(statusArray.join('\n'), 2000);
2020-07-12 00:39:34 -04:00
const cmdPages: RichEmbed[] = [];
2020-07-13 00:01:34 -04:00
statusSplit.forEach((split) => {
2020-07-12 00:39:34 -04:00
const embed = new RichEmbed();
embed.setTitle(`Members in ${role.name}`);
2020-07-13 00:01:34 -04:00
embed.setDescription(`Members in Role: ${membersOnline.length + membersIdle.length + membersDnd.length + membersOffline.length}\n\n${split}`);
2020-07-12 00:39:34 -04:00
embed.setColor(role.color);
embed.setFooter(this.client.user.username, this.client.user.avatarURL);
embed.setTimestamp();
return cmdPages.push(embed);
});
if (cmdPages.length === 1) return message.channel.createMessage({ embed: cmdPages[0] });
return createPaginationEmbed(message, cmdPages);
} catch (err) {
return this.client.util.handleError(err, message, this);
}
}
}