aboutsummaryrefslogtreecommitdiff
path: root/src/commands/dev/servers.ts
blob: 08f74e8bf560472630b659f56f9f1f5e80077011 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { Guild, MessageEmbed } from 'discord.js';
import { BushCommand, BushMessage, BushSlashMessage } from '../../lib';

export default class ServersCommand extends BushCommand {
	public constructor() {
		super('servers', {
			aliases: ['servers'],
			category: 'dev',
			description: {
				content: 'Displays all the severs the bot is in',
				usage: 'servers',
				examples: ['servers']
			},
			clientPermissions: ['SEND_MESSAGES'],
			userPermissions: ['SEND_MESSAGES'],
			superUserOnly: true
		});
	}

	public async exec(message: BushMessage | BushSlashMessage): Promise<unknown> {
		const maxLength = 10;
		const guilds = this.client.guilds.cache.sort((a, b) => (a.memberCount < b.memberCount ? 1 : -1)).array();
		const chunkedGuilds: Guild[][] = [];
		const embeds: MessageEmbed[] = [];

		for (let i = 0, j = guilds.length; i < j; i += maxLength) {
			chunkedGuilds.push(guilds.slice(i, i + maxLength));
		}

		chunkedGuilds.forEach((c: Guild[]) => {
			const embed = new MessageEmbed();
			c.forEach((g: Guild) => {
				const owner = this.client.users.cache.get(g.ownerId)?.tag;
				embed
					.addField(
						`**${g.name}**`,
						`**ID:** ${g.id}\n**Owner:** ${owner ? owner : g.ownerId}\n**Members:** ${g.memberCount.toLocaleString()}`,
						false
					)
					.setTitle('Server List')
					.setColor(this.client.util.colors.default);
			});
			embeds.push(embed);
		});

		return await this.client.util.buttonPaginate(message, embeds);
	}
}