-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
LockingCommands.kt
383 lines (336 loc) · 11.9 KB
/
LockingCommands.kt
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package org.hyacinthbots.lilybot.extensions.moderation
import com.kotlindiscord.kord.extensions.DISCORD_GREEN
import com.kotlindiscord.kord.extensions.DISCORD_RED
import com.kotlindiscord.kord.extensions.checks.anyGuild
import com.kotlindiscord.kord.extensions.checks.hasPermission
import com.kotlindiscord.kord.extensions.commands.Arguments
import com.kotlindiscord.kord.extensions.commands.application.slash.ephemeralSubCommand
import com.kotlindiscord.kord.extensions.commands.converters.impl.defaultingString
import com.kotlindiscord.kord.extensions.commands.converters.impl.optionalChannel
import com.kotlindiscord.kord.extensions.extensions.Extension
import com.kotlindiscord.kord.extensions.extensions.ephemeralSlashCommand
import com.kotlindiscord.kord.extensions.types.EphemeralInteractionContext
import com.kotlindiscord.kord.extensions.types.respond
import dev.kord.common.entity.Permission
import dev.kord.common.entity.Permissions
import dev.kord.core.behavior.channel.asChannelOfOrNull
import dev.kord.core.behavior.channel.createEmbed
import dev.kord.core.behavior.channel.editRolePermission
import dev.kord.core.behavior.edit
import dev.kord.core.entity.channel.Channel
import dev.kord.core.entity.channel.TextChannel
import dev.kord.core.entity.channel.ThreadParentChannel
import dev.kord.core.entity.channel.thread.TextChannelThread
import kotlinx.datetime.Clock
import org.hyacinthbots.lilybot.extensions.config.ConfigOptions
import org.hyacinthbots.lilybot.utils.botHasChannelPerms
import org.hyacinthbots.lilybot.utils.getLoggingChannelWithPerms
import org.hyacinthbots.lilybot.utils.requiredConfigs
class LockingCommands : Extension() {
override val name = "locking-commands"
override suspend fun setup() {
/**
* Server and channel locking commands
*
* @author tempest15
* @since 3.1.0
*/
ephemeralSlashCommand {
name = "lock"
description = "The parent command for all locking commands"
ephemeralSubCommand(::LockChannelArgs) {
name = "channel"
description = "Lock a channel so those with default permissions cannot send messages"
requirePermission(Permission.ModerateMembers)
check {
anyGuild()
requiredConfigs(
ConfigOptions.MODERATION_ENABLED
)
hasPermission(Permission.ModerateMembers)
requireBotPermissions(
Permission.ManageChannels,
Permission.ManageRoles,
Permission.SendMessages
)
botHasChannelPerms(Permissions(Permission.ManageChannels))
}
action {
val channelArg = arguments.channel ?: event.interaction.getChannelOrNull()
val channelParent = getChannelParent(channelArg)
val targetChannel = getTargetChannel(channelParent, channelArg)
val channelPerms = targetChannel!!.getPermissionOverwritesForRole(guild!!.id)
if (channelPerms != null && channelPerms.denied.contains(Permission.SendMessages)) {
respond { content = "This channel is already locked!" }
return@action
}
val everyoneRole = guild!!.getRoleOrNull(guild!!.id)
if (everyoneRole == null) {
respond { content = "I was unable to get the `@everyone` role. Please try again." }
return@action
} else if (!everyoneRole.permissions.contains(Permission.SendMessages)) {
respond { content = "The server is locked, so I cannot lock this channel." }
return@action
}
targetChannel.createEmbed {
title = "Channel Locked"
description = "This channel has been locked by a moderator."
color = DISCORD_RED
}
targetChannel.editRolePermission(guild!!.id) {
denied += Permission.SendMessages
denied += Permission.SendMessagesInThreads
denied += Permission.AddReactions
denied += Permission.UseApplicationCommands
}
respond { content = "${targetChannel.mention} has been locked." }
val actionLog =
getLoggingChannelWithPerms(ConfigOptions.ACTION_LOG, this.getGuild()!!) ?: return@action
actionLog.createEmbed {
title = "Channel Locked"
description = "${targetChannel.mention} has been locked.\n\n**Reason:** ${arguments.reason}"
footer {
text = user.asUserOrNull()?.tag ?: "Unable to get tag"
icon = user.asUserOrNull()?.avatar?.url
}
timestamp = Clock.System.now()
color = DISCORD_RED
}
}
}
ephemeralSubCommand(::LockServerArgs) {
name = "server"
description = "Lock the server so those with default permissions cannot send messages"
requirePermission(Permission.ModerateMembers)
check {
anyGuild()
requiredConfigs(
ConfigOptions.MODERATION_ENABLED
)
hasPermission(Permission.ModerateMembers)
requireBotPermissions(
Permission.ManageChannels,
Permission.ManageRoles,
Permission.SendMessages
)
}
action {
val everyoneRole = guild!!.getRoleOrNull(guild!!.id)
if (everyoneRole == null) {
respond { content = "I was unable to get the `@everyone` role. Please try again." }
return@action
} else if (!everyoneRole.permissions.contains(Permission.SendMessages)) {
respond { content = "The server is already locked." }
return@action
}
everyoneRole.edit {
permissions = everyoneRole.permissions
.minus(Permission.SendMessages)
.minus(Permission.SendMessagesInThreads)
.minus(Permission.AddReactions)
.minus(Permission.UseApplicationCommands)
}
respond { content = "Server locked." }
val actionLog =
getLoggingChannelWithPerms(ConfigOptions.ACTION_LOG, this.getGuild()!!) ?: return@action
actionLog.createEmbed {
title = "Server locked"
description = "**Reason:** ${arguments.reason}"
footer {
text = user.asUserOrNull()?.tag ?: "Unable to get user tag"
icon = user.asUserOrNull()?.avatar?.url
}
timestamp = Clock.System.now()
color = DISCORD_RED
}
}
}
}
/**
* Server and channel unlocking commands
*
* @author tempest15
* @since 3.1.0
*/
ephemeralSlashCommand {
name = "unlock"
description = "The parent command for all unlocking commands"
ephemeralSubCommand(::UnlockChannelArgs) {
name = "channel"
description = "Unlock a channel so everyone can send messages again"
requirePermission(Permission.ModerateMembers)
check {
anyGuild()
requiredConfigs(ConfigOptions.MODERATION_ENABLED)
hasPermission(Permission.ModerateMembers)
requireBotPermissions(
Permission.ManageChannels,
Permission.ManageRoles,
Permission.SendMessages
)
botHasChannelPerms(Permissions(Permission.ManageChannels))
}
action {
val channelArg = arguments.channel ?: event.interaction.getChannelOrNull()
val channelParent = getChannelParent(channelArg)
val targetChannel = getTargetChannel(channelParent, channelArg)
val everyoneRole = guild!!.getRoleOrNull(guild!!.id)
if (everyoneRole == null) {
respond { content = "Unable to get `@everyone` role. Please try again" }
return@action
} else if (!everyoneRole.permissions.contains(Permission.SendMessages)) {
respond { content = "Please unlock the server to unlock this channel." }
return@action
}
val channelPerms = targetChannel!!.getPermissionOverwritesForRole(guild!!.id)
if (channelPerms == null) {
respond { content = "This channel is not locked!" }
return@action
}
if (!channelPerms.denied.contains(Permission.SendMessages)) {
respond { content = "This channel is not locked!" }
return@action
}
targetChannel.editRolePermission(guild!!.id) {
denied -= Permission.SendMessages
denied -= Permission.SendMessagesInThreads
denied -= Permission.AddReactions
denied -= Permission.UseApplicationCommands
}
targetChannel.createEmbed {
title = "Channel Unlocked"
description = "This channel has been unlocked by a moderator.\n" +
"Please be aware of the rules when continuing discussion."
color = DISCORD_GREEN
}
respond { content = "${targetChannel.mention} has been unlocked." }
val actionLog =
getLoggingChannelWithPerms(ConfigOptions.ACTION_LOG, this.getGuild()!!) ?: return@action
actionLog.createEmbed {
title = "Channel Unlocked"
description = "${targetChannel.mention} has been unlocked."
footer {
text = user.asUserOrNull()?.tag ?: "Unable to get user tag"
icon = user.asUserOrNull()?.avatar?.url
}
timestamp = Clock.System.now()
color = DISCORD_GREEN
}
}
}
ephemeralSubCommand {
name = "server"
description = "Unlock the server so everyone can send messages again"
requirePermission(Permission.ModerateMembers)
check {
anyGuild()
requiredConfigs(
ConfigOptions.MODERATION_ENABLED
)
hasPermission(Permission.ModerateMembers)
requireBotPermissions(
Permission.ManageChannels,
Permission.ManageRoles,
Permission.SendMessages
)
}
action {
val everyoneRole = guild!!.getRoleOrNull(guild!!.id)
if (everyoneRole == null) {
respond { content = "Unable to get `@everyone` role. Please try again" }
return@action
} else if (everyoneRole.permissions.contains(Permission.SendMessages)) {
respond { content = "The server isn't locked!" }
return@action
}
everyoneRole.edit {
permissions = everyoneRole.permissions
.plus(Permission.SendMessages)
.plus(Permission.SendMessagesInThreads)
.plus(Permission.AddReactions)
.plus(Permission.UseApplicationCommands)
}
respond { content = "Server unlocked." }
val actionLog =
getLoggingChannelWithPerms(ConfigOptions.ACTION_LOG, this.getGuild()!!) ?: return@action
actionLog.createEmbed {
title = "Server unlocked"
footer {
text = user.asUserOrNull()?.tag ?: "Unable to get user tag"
icon = user.asUserOrNull()?.avatar?.url
}
timestamp = Clock.System.now()
color = DISCORD_GREEN
}
}
}
}
}
/**
* Gets the parent of the channel argument.
*
* @param channelArg The channel to get the parent of
* @return The channel parent as a [TextChannel]
*
* @author NoComment1105
* @since 4.8.0
*/
private suspend inline fun getChannelParent(channelArg: Channel?): TextChannel? {
var channelParent: ThreadParentChannel? = null
if (channelArg is TextChannelThread) {
channelParent = channelArg.getParent()
}
return channelParent?.asChannelOfOrNull()
}
/**
* Gets the target channel and responds appropriately if unable to get it.
*
* @param channelParent The parent channel if that is the target
* @param channelArg The channel argument, if that is the target
* @return The channel as a [TextChannel]
*
* @author NoComment1105
* @since 4.8.0
*/
private suspend inline fun EphemeralInteractionContext.getTargetChannel(
channelParent: TextChannel?,
channelArg: Channel?
): TextChannel? {
val targetChannel = channelParent ?: channelArg?.asChannelOfOrNull()
if (targetChannel == null) {
respond {
content = "I can't fetch the targeted channel properly."
return null
}
}
return targetChannel
}
inner class LockChannelArgs : Arguments() {
/** The channel that the user wants to lock. */
val channel by optionalChannel {
name = "channel"
description = "Channel to lock. Defaults to current channel"
}
/** The reason for the locking. */
val reason by defaultingString {
name = "reason"
description = "Reason for locking the channel"
defaultValue = "No reason provided"
}
}
inner class LockServerArgs : Arguments() {
/** The reason for the locking. */
val reason by defaultingString {
name = "reason"
description = "Reason for locking the server"
defaultValue = "No reason provided"
}
}
inner class UnlockChannelArgs : Arguments() {
/** The channel to unlock. */
val channel by optionalChannel {
name = "channel"
description = "Channel to unlock. Defaults to current channel"
}
}
}