1 |
import { CommandInteraction, Message, Role, TextChannel } from 'discord.js'; |
2 |
import BaseCommand from '../../utils/structures/BaseCommand'; |
3 |
import DiscordClient from '../../client/Client'; |
4 |
import CommandOptions from '../../types/CommandOptions'; |
5 |
import InteractionOptions from '../../types/InteractionOptions'; |
6 |
import MessageEmbed from '../../client/MessageEmbed'; |
7 |
|
8 |
export default class UnlockCommand extends BaseCommand { |
9 |
supportsInteractions: boolean = true; |
10 |
|
11 |
constructor() { |
12 |
super('unlock', 'moderation', []); |
13 |
} |
14 |
|
15 |
async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) { |
16 |
let channel: TextChannel = <TextChannel> msg.channel; |
17 |
let role: Role = <Role> msg.guild!.roles.everyone; |
18 |
|
19 |
if (msg instanceof CommandInteraction) |
20 |
await msg.deferReply({ ephemeral: true }); |
21 |
|
22 |
const force = options.isInteraction ? options.options.getBoolean('force') === true : (options.options.indexOf('--force') !== -1); |
23 |
|
24 |
if (options.isInteraction) { |
25 |
if (options.options.getChannel('channel')) { |
26 |
channel = await <TextChannel> options.options.getChannel('channel'); |
27 |
} |
28 |
|
29 |
if (options.options.getRole('role')) { |
30 |
role = await <Role> options.options.getRole('role'); |
31 |
} |
32 |
} |
33 |
else { |
34 |
if ((msg as Message).mentions.roles.first()) { |
35 |
role = await <Role> (msg as Message).mentions.roles.first(); |
36 |
} |
37 |
else if (options.normalArgs[0] && options.normalArgs[0] !== 'everyone') { |
38 |
role = <Role> await (msg as Message).guild?.roles.fetch(options.normalArgs[0]); |
39 |
} |
40 |
|
41 |
if ((msg as Message).mentions.channels.first()) { |
42 |
channel = await <TextChannel> (msg as Message).mentions.channels.first(); |
43 |
} |
44 |
else if (options.normalArgs[1]) { |
45 |
channel = <TextChannel> await (msg as Message).guild?.channels.fetch(options.normalArgs[1]); |
46 |
} |
47 |
|
48 |
if (!role) { |
49 |
await msg.reply({ |
50 |
embeds: [ |
51 |
new MessageEmbed() |
52 |
.setColor('#f14a60') |
53 |
.setDescription(`Invalid role given.`) |
54 |
], |
55 |
ephemeral: true |
56 |
}); |
57 |
|
58 |
return; |
59 |
} |
60 |
|
61 |
if (!channel || channel.type !== 'GUILD_TEXT') { |
62 |
await msg.reply({ |
63 |
embeds: [ |
64 |
new MessageEmbed() |
65 |
.setColor('#f14a60') |
66 |
.setDescription(`Invalid text channel given.`) |
67 |
], |
68 |
ephemeral: true |
69 |
}); |
70 |
|
71 |
return; |
72 |
} |
73 |
} |
74 |
|
75 |
try { |
76 |
const result = await client.channelLock.unlock(channel, { sendConfirmation: true, force }); |
77 |
let error = null; |
78 |
|
79 |
if (!result) { |
80 |
error = 'This channel wasn\'t locked' + (role.id === msg.guild!.id ? '' : ' for the given role') + '. If you want to force unlock, run this command with `--force` option or select `True` if using slash commands.'; |
81 |
} |
82 |
|
83 |
if (error) { |
84 |
await this.deferReply(msg, { |
85 |
content: error, |
86 |
}); |
87 |
|
88 |
return; |
89 |
} |
90 |
|
91 |
if (options.isInteraction) { |
92 |
await this.deferReply(msg, { |
93 |
content: "Channel unlocked.", |
94 |
}); |
95 |
} |
96 |
else { |
97 |
await (msg as Message).react('🔓'); |
98 |
} |
99 |
} |
100 |
catch (e) { |
101 |
console.log(e); |
102 |
|
103 |
await msg.reply({ |
104 |
embeds: [ |
105 |
new MessageEmbed() |
106 |
.setColor('#f14a60') |
107 |
.setDescription(`Failed to unlock channel. Maybe missing permissions?`) |
108 |
], |
109 |
ephemeral: true |
110 |
}); |
111 |
|
112 |
return; |
113 |
} |
114 |
} |
115 |
} |