This repository was archived by the owner on Sep 24, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
🔐 Lock commands #241
Open
Ikinon
wants to merge
19
commits into
CascadeBot:dev
Choose a base branch
from
Ikinon:lock
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
🔐 Lock commands #241
Changes from 12 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
5e87492
Start of lock command
Ikinon 5b7f841
commit
Ikinon 4bfde75
Merge remote-tracking branch 'origin/dev' into feature/lock
Ikinon 2c3a43e
Merge remote-tracking branch 'origin/dev' into feature/lock
Ikinon 22f369b
Lock/Unlock/Templock commands
Ikinon 4318a5a
make unlock work
Ikinon bac5ecb
lock changes
Ikinon 855801b
Use more descriptive types and improve some logic?
binaryoverload 75da6b5
Renamy stuff
binaryoverload 4bd1d77
Merge remote-tracking branch 'origin/dev' into lock
binaryoverload 7db3b28
Some changes
binaryoverload eb52642
Remove duplicate JSON keys
binaryoverload f265b3f
Create a util class to hold previous lock state
binaryoverload 6b8b92f
Hold the time at which the permission state was applied
binaryoverload 75159a3
Convert lock and unlock to callback functions
binaryoverload b5f8608
LockCommand: Use callbacks
binaryoverload 4777607
Refactor log commands
binaryoverload a97c864
Add check if the channel is already locked
binaryoverload 350f87e
Allow new LockStatus object to be serialised
binaryoverload File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
src/main/kotlin/org/cascadebot/cascadebot/commands/moderation/LockCommand.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package org.cascadebot.cascadebot.commands.moderation | ||
|
|
||
| import net.dv8tion.jda.api.Permission | ||
| import net.dv8tion.jda.api.entities.ISnowflake | ||
| import net.dv8tion.jda.api.entities.Member | ||
| import net.dv8tion.jda.api.entities.Role | ||
| import net.dv8tion.jda.api.entities.TextChannel | ||
| import net.dv8tion.jda.api.exceptions.PermissionException | ||
| import org.cascadebot.cascadebot.commandmeta.CommandContext | ||
| import org.cascadebot.cascadebot.commandmeta.MainCommand | ||
| import org.cascadebot.cascadebot.commandmeta.Module | ||
| import org.cascadebot.cascadebot.data.managers.LockManager | ||
| import org.cascadebot.cascadebot.permissions.CascadePermission | ||
| import org.cascadebot.cascadebot.utils.DiscordUtils | ||
|
|
||
| class LockCommand : MainCommand() { | ||
| override fun onCommand(sender: Member, context: CommandContext) { | ||
| var channel: TextChannel = context.channel | ||
| if (context.args.isNotEmpty()) { | ||
| channel = DiscordUtils.getTextChannel(context.guild, context.getArg(0)) | ||
| ?: return context.typedMessaging.replyDanger(context.i18n("responses.cannot_find_channel_matching", context.getArg(0))) | ||
|
|
||
| } | ||
|
|
||
| val target: ISnowflake = if (context.args.size == 2) { | ||
| DiscordUtils.getRole(context.getArg(1), context.guild) | ||
| ?: DiscordUtils.getMember(context.guild, context.getArg(1)) | ||
| ?: DiscordUtils.getTextChannel(context.guild, context.getArg(1)) | ||
| ?: return context.typedMessaging.replyDanger(context.i18n("commands.lock.invalid_argument", context.getArg(0))) | ||
| } else { | ||
| context.guild.publicRole | ||
| } | ||
|
|
||
| if (target is TextChannel) channel = target; | ||
|
|
||
| val name: String = try { | ||
| when (target) { | ||
| is Role -> { | ||
| LockManager.lock(channel, target) | ||
| "%s %s".format(context.i18n("arguments.role"), target.asMention) | ||
| } | ||
| is Member -> { | ||
| LockManager.lock(channel, target) | ||
| "%s %s".format(context.i18n("arguments.member"), target.asMention) | ||
| } | ||
| else -> "" | ||
| } | ||
| } catch (e: PermissionException) { | ||
| context.uiMessaging.sendBotDiscordPermError(e.permission) | ||
| return | ||
| } | ||
|
|
||
| context.typedMessaging.replySuccess((context.i18n("commands.lock.success", channel.name, name))) | ||
| } | ||
|
|
||
|
|
||
| override fun command(): String { | ||
| return "lock" | ||
| } | ||
|
|
||
| override fun permission(): CascadePermission? { | ||
| return CascadePermission.of("lock", false, Permission.MANAGE_CHANNEL) | ||
| } | ||
|
|
||
| override fun module(): Module { | ||
| return Module.MODERATION | ||
| } | ||
|
|
||
| } |
113 changes: 113 additions & 0 deletions
113
src/main/kotlin/org/cascadebot/cascadebot/commands/moderation/TempLockCommand.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| package org.cascadebot.cascadebot.commands.moderation | ||
|
|
||
| import net.dv8tion.jda.api.Permission | ||
| import net.dv8tion.jda.api.entities.ISnowflake | ||
| import net.dv8tion.jda.api.entities.Member | ||
| import net.dv8tion.jda.api.entities.Role | ||
| import net.dv8tion.jda.api.entities.TextChannel | ||
| import net.dv8tion.jda.api.exceptions.PermissionException | ||
| import org.cascadebot.cascadebot.commandmeta.CommandContext | ||
| import org.cascadebot.cascadebot.commandmeta.MainCommand | ||
| import org.cascadebot.cascadebot.commandmeta.Module | ||
| import org.cascadebot.cascadebot.data.managers.LockManager | ||
| import org.cascadebot.cascadebot.data.managers.Status | ||
| import org.cascadebot.cascadebot.data.managers.ScheduledActionManager | ||
| import org.cascadebot.cascadebot.permissions.CascadePermission | ||
| import org.cascadebot.cascadebot.scheduler.ActionType | ||
| import org.cascadebot.cascadebot.scheduler.ScheduledAction | ||
| import org.cascadebot.cascadebot.utils.DiscordUtils | ||
| import org.cascadebot.cascadebot.utils.FormatUtils | ||
| import org.cascadebot.cascadebot.utils.ParserUtils | ||
| import java.time.Instant | ||
| import java.time.OffsetDateTime | ||
| import java.time.temporal.ChronoUnit | ||
|
|
||
| class TempLockCommand : MainCommand() { | ||
| override fun onCommand(sender: Member, context: CommandContext) { | ||
| if (context.args.isEmpty()) { | ||
| context.uiMessaging.replyUsage() | ||
| return | ||
| } | ||
|
|
||
| val longDuration = ParserUtils.parseTextTime(context.getArg(0), false) | ||
| if (longDuration <= 0) { | ||
| context.typedMessaging.replyDanger(context.i18n("responses.invalid_duration")) | ||
| return | ||
| } | ||
|
|
||
| var channel: TextChannel = context.channel | ||
| if (context.args.size == 2) { | ||
| val tempChannel = DiscordUtils.getTextChannel(context.guild, context.getArg(1)) | ||
| if (tempChannel != null) { | ||
| channel = tempChannel | ||
| } else { | ||
| context.typedMessaging.replyDanger(context.i18n("responses.cannot_find_channel_matching", context.getArg(1))) | ||
| return | ||
| } | ||
| } | ||
|
|
||
| val target: ISnowflake = if (context.args.size == 3) { | ||
| DiscordUtils.getRole(context.getArg(1), context.guild) | ||
| ?: DiscordUtils.getMember(context.guild, context.getArg(2)) | ||
| ?: return context.typedMessaging.replyDanger(context.i18n("commands.templock.invalid_argument", context.getArg(2))) | ||
| } else { | ||
| context.channel | ||
| } | ||
|
|
||
| val toAction = ScheduledAction.LockActionData(channel.idLong, Status.NEUTRAL, 0, 0) | ||
|
|
||
| var name: String? = null | ||
| try { | ||
| when (target) { | ||
| is Role -> { | ||
| toAction.oldPermission = LockManager.getPerm(channel, target).left | ||
|
|
||
| LockManager.lock(channel, target) | ||
|
|
||
| toAction.targetRoleID = target.idLong | ||
| name = "%s %s".format(context.i18n("arguments.role"), target.asMention) | ||
| } | ||
| is Member -> { | ||
| toAction.oldPermission = LockManager.getPerm(channel, target).left | ||
|
|
||
| LockManager.lock(channel, target) | ||
|
|
||
| toAction.targetMemberID = target.idLong | ||
| name = "%s %s".format(context.i18n("arguments.member"), target.user.asMention) | ||
| } | ||
| } | ||
| } catch (e: PermissionException) { | ||
| context.uiMessaging.sendBotDiscordPermError(e.permission) | ||
| return | ||
| } | ||
|
|
||
| ScheduledActionManager.registerScheduledAction(ScheduledAction( | ||
| ActionType.UNLOCK, | ||
| toAction, | ||
| context.guild.idLong, | ||
| context.channel.idLong, | ||
| context.member.idLong, | ||
| Instant.now(), | ||
| longDuration | ||
| )) | ||
|
|
||
|
|
||
| val textDuration = FormatUtils.formatTime(longDuration, context.locale, true).replace("(0[hm])".toRegex(), "") + | ||
| " (" + context.i18n("words.until") + " " + FormatUtils.formatDateTime(OffsetDateTime.now().plus(longDuration, ChronoUnit.MILLIS), context.locale) + ")" | ||
| context.typedMessaging.replySuccess(if (target is TextChannel) context.i18n("commands.templock.text_success", target.name, textDuration) else name?.let { context.i18n("commands.templock.success", channel.name, it, textDuration) }) | ||
| } | ||
|
|
||
|
|
||
| override fun command(): String { | ||
| return "templock" | ||
| } | ||
|
|
||
| override fun permission(): CascadePermission? { | ||
| return CascadePermission.of("templock", false, Permission.MANAGE_CHANNEL) | ||
| } | ||
|
|
||
| override fun module(): Module { | ||
| return Module.MODERATION | ||
| } | ||
|
|
||
| } |
73 changes: 73 additions & 0 deletions
73
src/main/kotlin/org/cascadebot/cascadebot/commands/moderation/UnlockCommand.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| package org.cascadebot.cascadebot.commands.moderation | ||
|
|
||
| import javassist.NotFoundException | ||
| import net.dv8tion.jda.api.Permission | ||
| import net.dv8tion.jda.api.entities.ISnowflake | ||
| import net.dv8tion.jda.api.entities.Member | ||
| import net.dv8tion.jda.api.entities.Role | ||
| import net.dv8tion.jda.api.entities.TextChannel | ||
| import net.dv8tion.jda.api.exceptions.PermissionException | ||
| import org.cascadebot.cascadebot.commandmeta.CommandContext | ||
| import org.cascadebot.cascadebot.commandmeta.MainCommand | ||
| import org.cascadebot.cascadebot.commandmeta.Module | ||
| import org.cascadebot.cascadebot.data.managers.LockManager | ||
| import org.cascadebot.cascadebot.permissions.CascadePermission | ||
| import org.cascadebot.cascadebot.utils.DiscordUtils | ||
|
|
||
| class UnlockCommand : MainCommand() { | ||
| override fun onCommand(sender: Member, context: CommandContext) { | ||
| var channel: TextChannel = context.channel | ||
| if (context.args.isNotEmpty()) { | ||
| channel = DiscordUtils.getTextChannel(context.guild, context.getArg(0)) | ||
| ?: return context.typedMessaging.replyDanger(context.i18n("responses.cannot_find_channel_matching", context.getArg(0))) | ||
|
|
||
| } | ||
|
|
||
| val target: ISnowflake = if (context.args.size == 2) { | ||
| DiscordUtils.getRole(context.getArg(1), context.guild) | ||
| ?: DiscordUtils.getMember(context.guild, context.getArg(1)) | ||
| ?: return context.typedMessaging.replyDanger(context.i18n("commands.unlock.invalid_argument", context.getArg(1))) | ||
| } else { | ||
| context.guild.publicRole | ||
| } | ||
|
|
||
| var name = "" | ||
| val completed = try { | ||
| when (target) { | ||
| is Role -> { | ||
| name = target.asMention | ||
| LockManager.unlock(context.guild, channel, target) | ||
| } | ||
| is Member -> { | ||
| name = "%s %s".format(context.i18n("arguments.member"), target.asMention) | ||
| LockManager.unlock(context.guild, channel, target) | ||
| } | ||
| else -> false | ||
| } | ||
| } catch (e: PermissionException) { | ||
| context.uiMessaging.sendBotDiscordPermError(e.permission) | ||
| return | ||
| } catch (e: NotFoundException) { | ||
| context.typedMessaging.replyWarning(context.i18n("commands.unlock.fail", if (target is TextChannel) context.guild.publicRole.asMention else name!!)) | ||
| return | ||
| } | ||
| if (completed) { | ||
| context.typedMessaging.replySuccess(context.i18n("commands.unlock.success", channel.name, name)) | ||
| } else { | ||
| context.typedMessaging.replyDanger(context.i18n("commands.unlock.failure", channel.name, name)) | ||
| } | ||
| } | ||
|
|
||
| override fun command(): String { | ||
| return "unlock" | ||
| } | ||
|
|
||
| override fun permission(): CascadePermission? { | ||
| return CascadePermission.of("unlock", false, Permission.MANAGE_CHANNEL) | ||
| } | ||
|
|
||
| override fun module(): Module { | ||
| return Module.MODERATION | ||
| } | ||
|
|
||
| } | ||
91 changes: 91 additions & 0 deletions
91
src/main/kotlin/org/cascadebot/cascadebot/data/managers/LockManager.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| package org.cascadebot.cascadebot.data.managers | ||
|
|
||
|
|
||
| import net.dv8tion.jda.api.Permission | ||
| import net.dv8tion.jda.api.entities.Guild | ||
| import net.dv8tion.jda.api.entities.IPermissionHolder | ||
| import net.dv8tion.jda.api.entities.TextChannel | ||
| import net.dv8tion.jda.api.requests.restaction.PermissionOverrideAction | ||
| import net.dv8tion.jda.internal.utils.tuple.MutablePair | ||
| import java.util.EnumSet | ||
|
|
||
| enum class Status { | ||
| ALLOW, | ||
| DENY, | ||
| NEUTRAL; | ||
|
|
||
| fun apply(action: PermissionOverrideAction, perm: EnumSet<Permission>) { | ||
| when (this) { | ||
| ALLOW -> action.grant(perm) | ||
| DENY -> action.deny(perm) | ||
| NEUTRAL -> action.clear(perm) | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| object LockManager { | ||
|
|
||
| fun getPerm(channel: TextChannel, target: IPermissionHolder): MutablePair<Status, Status> { | ||
| // [target, selfMember] | ||
| val perm = MutablePair<Status, Status>(Status.NEUTRAL, Status.NEUTRAL) | ||
|
|
||
| val targetOverride = channel.getPermissionOverride(target) | ||
| if (targetOverride != null) { | ||
| if (targetOverride.allowed.contains(Permission.MESSAGE_WRITE)) perm.setLeft(Status.ALLOW) | ||
| if (targetOverride.denied.contains(Permission.MESSAGE_WRITE)) perm.setLeft(Status.DENY) | ||
| } | ||
|
|
||
| val selfMemberOverride = channel.getPermissionOverride(channel.guild.selfMember) | ||
| if (selfMemberOverride != null) { | ||
| if (selfMemberOverride.allowed.contains(Permission.MESSAGE_WRITE)) perm.setRight(Status.ALLOW) | ||
| if (selfMemberOverride.denied.contains(Permission.MESSAGE_WRITE)) perm.setRight(Status.DENY) | ||
| } | ||
| return perm | ||
| } | ||
|
|
||
| private fun storePermissions(channel: TextChannel, target: IPermissionHolder) { | ||
| val lockedChannels = GuildDataManager.getGuildData(channel.guild.idLong).lockedChannels | ||
| val mutableMap = lockedChannels[channel.id] | ||
| if (mutableMap == null) { | ||
| lockedChannels[channel.id] = mutableMapOf(Pair(target.id, getPerm(channel, target))) | ||
| } else { | ||
| mutableMap[target.id] = getPerm(channel, target) | ||
| } | ||
| } | ||
|
|
||
|
|
||
| fun lock(channel: TextChannel, target: IPermissionHolder) { | ||
| storePermissions(channel, target) | ||
| channel.upsertPermissionOverride(channel.guild.selfMember).grant(Permission.MESSAGE_WRITE).queue() | ||
| channel.upsertPermissionOverride(target).deny(Permission.MESSAGE_WRITE).queue() | ||
| } | ||
|
|
||
| fun unlock(guild: Guild, channel: TextChannel, target: IPermissionHolder) : Boolean { | ||
| val state = GuildDataManager.getGuildData(channel.guild.idLong).lockedChannels[channel.id]?.get(target.id) | ||
| // If there is no state to restore, we can't do anything! | ||
| ?: return false | ||
| val perm = EnumSet.of(Permission.MESSAGE_WRITE) | ||
|
|
||
| var changed = false; | ||
|
|
||
| val selfPermissionAction = channel.getPermissionOverride(guild.selfMember)?.manager | ||
| if (selfPermissionAction != null) { | ||
| state.right.apply(selfPermissionAction, perm) | ||
| selfPermissionAction.queue { if (it.allowedRaw == 0L && it.deniedRaw == 0L) it.delete().queue() } | ||
| changed = true; | ||
| } | ||
|
|
||
| val targetPermissionAction = channel.getPermissionOverride(target)?.manager | ||
| if (targetPermissionAction != null) { | ||
| state.left.apply(targetPermissionAction, perm) | ||
| targetPermissionAction.queue { | ||
| GuildDataManager.getGuildData(guild.idLong).lockedChannels[channel.idLong.toString()]?.remove(target.idLong.toString()) | ||
| if (it.allowedRaw == 0L && it.deniedRaw == 0L) it.delete().queue() | ||
| } | ||
| changed = true | ||
| } | ||
| return changed | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.