/[sudobot]/trunk/src/commands/information/RoleListCommand.ts
ViewVC logotype

Contents of /trunk/src/commands/information/RoleListCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 468 - (show annotations)
Mon Jul 29 17:30:23 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 4928 byte(s)
refactor: add pagination to rolelist command
1 /**
2 * This file is part of SudoBot.
3 *
4 * Copyright (C) 2021-2022 OSN Inc.
5 *
6 * SudoBot is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * SudoBot is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with SudoBot. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20 import { CommandInteraction, Interaction, Message, Role } from 'discord.js';
21 import BaseCommand from '../../utils/structures/BaseCommand';
22 import DiscordClient from '../../client/Client';
23 import InteractionOptions from '../../types/InteractionOptions';
24 import MessageEmbed from '../../client/MessageEmbed';
25 import Pagination from '../../utils/Pagination';
26 import CommandOptions from '../../types/CommandOptions';
27 import getRole from '../../utils/getRole';
28
29 export default class RoleListCommand extends BaseCommand {
30 supportsInteractions: boolean = true;
31
32 constructor() {
33 super('rolelist', 'information', []);
34 }
35
36 async run(client: DiscordClient, message: CommandInteraction | Message, options: InteractionOptions | CommandOptions) {
37 let role: Role | null = null, order = "d";
38
39 if (options.isInteraction && options.options.getRole('role'))
40 role = <Role> options.options.getRole('role');
41 else if (!options.isInteraction && options.args[0])
42 role = (await getRole(message as Message, options, 1)) ?? null;
43
44 if (options.isInteraction && options.options.getString('order'))
45 order = <string> options.options.getString('order');
46
47 if (!role) {
48 message instanceof CommandInteraction ? await message.deferReply() : null;
49
50 const roles = message.guild!.roles.cache.sort((role1, role2) => order === 'a' ? role1.position - role2.position : role2.position - role1.position).toJSON();
51 const popped = roles.pop();
52
53 if (popped && popped.id !== message.guild!.id) {
54 roles.push(popped);
55 }
56
57 const pagination = new Pagination(roles, {
58 channel_id: message.channel!.id,
59 guild_id: message.guild!.id,
60 user_id: message.member!.user.id,
61 limit: 10,
62 timeout: 120_000,
63 embedBuilder({ data, currentPage, maxPages }) {
64 let description = '';
65
66 for (const role of data) {
67 description += `${role} • \`${role.id}\` • ${role.hexColor}\n`;
68 // description += `${role}\n**ID**: ${role.id}\n**Color**: ${role.hexColor}\n**Hoisting**: ${role.hoist ? 'Enabled' : 'Disabled'}\n**Position**: ${role.position}\n\n`;
69 }
70
71 return new MessageEmbed({
72 author: {
73 name: "Roles",
74 iconURL: message.guild!.iconURL() ?? undefined
75 },
76 description,
77 footer: { text: `Page ${currentPage} of ${maxPages}` }
78 });
79 },
80 });
81
82 let reply = await this.deferReply(message, pagination.getMessageOptions(1));
83
84 if (message instanceof Interaction) {
85 reply = (await message.fetchReply()) as Message;
86 }
87
88 await pagination.start(reply! as Message);
89 }
90 else {
91 await message.reply({
92 embeds: [
93 new MessageEmbed()
94 .setAuthor({
95 name: `Role Information`
96 })
97 .setColor(role.hexColor)
98 .addFields([
99 {
100 name: 'Name',
101 value: role.name,
102 inline: true
103 },
104 {
105 name: 'Color',
106 value: role.hexColor,
107 inline: true
108 },
109 {
110 name: 'Members',
111 value: role.members.size + ''
112 },
113 {
114 name: 'Bot Role',
115 value: role.members.size === 1 && role.members.first()?.user.bot ? 'Yes' : 'No',
116 inline: true
117 }
118 ])
119 ]
120 });
121 }
122 }
123 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26