/[sudobot]/branches/4.x/src/commands/utils/PrivateChannelCommand.ts
ViewVC logotype

Annotation of /branches/4.x/src/commands/utils/PrivateChannelCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (hide annotations)
Mon Jul 29 18:52:37 2024 UTC (8 months, 1 week ago) by rakinar2
File MIME type: application/typescript
File size: 6269 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
1 rakinar2 577 /**
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 { Message, Interaction, CacheType, CommandInteraction, Util, CategoryChannel, GuildMember, OverwriteResolvable, Permissions } from "discord.js";
21     import Client from "../../client/Client";
22     import MessageEmbed from "../../client/MessageEmbed";
23     import CommandOptions from "../../types/CommandOptions";
24     import InteractionOptions from "../../types/InteractionOptions";
25     import reply from "../../utils/embeds/reply";
26     import { emoji, fetchEmoji } from "../../utils/Emoji";
27     import BaseCommand from "../../utils/structures/BaseCommand";
28    
29     export default class PrivateChannelCommand extends BaseCommand {
30     supportsInteractions: boolean = true;
31     permissions = [Permissions.FLAGS.MANAGE_CHANNELS];
32    
33     constructor() {
34     super('privatechannel', 'utils', ['private', 'createpch']);
35     }
36    
37     async run(client: Client, message: CommandInteraction<CacheType> | Message<boolean>, options: CommandOptions | InteractionOptions): Promise<void> {
38     if (!options.isInteraction && options.args[0] === undefined) {
39     await message.reply({
40     content: `${await fetchEmoji('error')} You must specify at least a single user to create a private channel.`,
41     ephemeral: true
42     });
43    
44     return;
45     }
46    
47     if (message instanceof CommandInteraction) {
48     await message.deferReply({ ephemeral: true });
49     }
50    
51     const users: GuildMember[] = [];
52     let categoryChannel: CategoryChannel | null = null;
53    
54     if (message instanceof Message && !options.isInteraction) {
55     if (message.mentions.members) {
56     for (const user of message.mentions.members.values()) {
57     users.push(user);
58     }
59     }
60    
61     const { args } = options;
62    
63     for (const arg of args) {
64     if (arg === '--category' || arg === '-c') {
65     const index = args.indexOf(arg) + 1;
66    
67     if (args[index] === undefined || !/^\d+$/.test(args[index])) {
68     await message.reply(`${await fetchEmoji('error')} You must specify a valid category channel ID after the \`${arg}\` option.`);
69     return;
70     }
71    
72     const channel = await message.guild!.channels.fetch(args[index]);
73    
74     if (channel?.type !== 'GUILD_CATEGORY') {
75     await message.reply(`${await fetchEmoji('error')} The given channel is not a category.`);
76     return;
77     }
78    
79     categoryChannel = channel;
80    
81     break;
82     }
83    
84     let id: string;
85    
86     if (arg.startsWith('<@') && arg.endsWith('>')) {
87     id = arg.substring(arg.startsWith('<@!') ? 2 : 1, arg.length - 1);
88     }
89     else {
90     id = arg;
91     }
92    
93     if (!/^\d+$/.test(id) || users.find(u => u.id === id)) {
94     continue;
95     }
96    
97     try {
98     const user = await message.guild!.members.fetch(id);
99    
100     if (user) {
101     users.push(user);
102     }
103     }
104     catch (e) {
105     console.log(e);
106     }
107     }
108     }
109     else if (message instanceof CommandInteraction) {
110     const member = message.options.getMember('member', true);
111    
112     if (!member) {
113     return;
114     }
115    
116     users.push(member as GuildMember);
117    
118     const channel = message.options.getChannel('category');
119    
120     if (channel && channel.type !== 'GUILD_CATEGORY') {
121     await this.deferReply(message, `${await fetchEmoji('error')} You must specify a category channel!`);
122     return;
123     }
124    
125     if (channel) {
126     categoryChannel = channel;
127     }
128     }
129    
130     if (users.length < 1) {
131     await this.deferReply(message, `${await fetchEmoji('error')} You must specify at least a single user to create a private channel.`);
132     return;
133     }
134    
135     try {
136     const channel = await message.guild!.channels.create(users[0].user.tag.replace('#', '-'), {
137     type: 'GUILD_TEXT',
138     parent: categoryChannel ?? undefined,
139     reason: 'User commanded to do so',
140     topic: `Private channel for ${users.map(u => u.user.tag).join(', ')}`,
141     permissionOverwrites: [
142     {
143     id: message.member!.user.id,
144     allow: ['VIEW_CHANNEL', 'SEND_MESSAGES', 'ADD_REACTIONS']
145     },
146     ...users.map(user => ({
147     id: user.id,
148     allow: ['VIEW_CHANNEL', 'SEND_MESSAGES', 'ADD_REACTIONS']
149     } as OverwriteResolvable)),
150     {
151     id: message.guild!.id,
152     deny: ['VIEW_CHANNEL', 'SEND_MESSAGES', 'ADD_REACTIONS']
153     }
154     ]
155     });
156    
157     await this.deferReply(message, {
158     embeds: [
159     reply(`A private channel ${channel} has been created for the following user(s): ${users.map(u => u.toString()).join(', ')}`)
160     ]
161     });
162     }
163     catch (e) {
164     console.log(e);
165     }
166     }
167     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26