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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 468 - (hide annotations)
Mon Jul 29 17:30:23 2024 UTC (8 months, 2 weeks ago) by rakin
File MIME type: application/typescript
File size: 4928 byte(s)
refactor: add pagination to rolelist command
1 rakin 393 /**
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 rakin 468 import { CommandInteraction, Interaction, Message, Role } from 'discord.js';
21 rakin 60 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 rakin 468 import Pagination from '../../utils/Pagination';
26     import CommandOptions from '../../types/CommandOptions';
27     import getRole from '../../utils/getRole';
28 rakin 60
29     export default class RoleListCommand extends BaseCommand {
30     supportsInteractions: boolean = true;
31    
32     constructor() {
33     super('rolelist', 'information', []);
34     }
35    
36 rakin 468 async run(client: DiscordClient, message: CommandInteraction | Message, options: InteractionOptions | CommandOptions) {
37     let role: Role | null = null, order = "d";
38 rakin 60
39 rakin 468 if (options.isInteraction && options.options.getRole('role'))
40 rakin 74 role = <Role> options.options.getRole('role');
41 rakin 468 else if (!options.isInteraction && options.args[0])
42     role = (await getRole(message as Message, options, 1)) ?? null;
43 rakin 60
44 rakin 468 if (options.isInteraction && options.options.getString('order'))
45     order = <string> options.options.getString('order');
46 rakin 74
47 rakin 60 if (!role) {
48 rakin 468 message instanceof CommandInteraction ? await message.deferReply() : null;
49 rakin 74
50 rakin 468 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 rakin 60
53 rakin 468 if (popped && popped.id !== message.guild!.id) {
54     roles.push(popped);
55 rakin 74 }
56    
57 rakin 468 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 rakin 74
66 rakin 468 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 rakin 60 }
87    
88 rakin 468 await pagination.start(reply! as Message);
89 rakin 60 }
90     else {
91 rakin 468 await message.reply({
92 rakin 60 embeds: [
93     new MessageEmbed()
94     .setAuthor({
95     name: `Role Information`
96     })
97     .setColor(role.hexColor)
98     .addFields([
99     {
100     name: 'Name',
101 rakin 468 value: role.name,
102     inline: true
103 rakin 60 },
104     {
105     name: 'Color',
106 rakin 468 value: role.hexColor,
107     inline: true
108 rakin 60 },
109     {
110     name: 'Members',
111     value: role.members.size + ''
112     },
113     {
114     name: 'Bot Role',
115 rakin 468 value: role.members.size === 1 && role.members.first()?.user.bot ? 'Yes' : 'No',
116     inline: true
117 rakin 60 }
118     ])
119     ]
120     });
121     }
122     }
123     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26