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 all 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
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
87 changes: 87 additions & 0 deletions
87
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,87 @@ | ||
package org.cascadebot.cascadebot.commands.moderation | ||
|
||
import net.dv8tion.jda.api.Permission | ||
import net.dv8tion.jda.api.entities.IMentionable | ||
import net.dv8tion.jda.api.entities.IPermissionHolder | ||
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 | ||
import java.lang.IllegalStateException | ||
|
||
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)) | ||
?: return context.typedMessaging.replyDanger(context.i18n("commands.lock.invalid_argument", context.getArg(0))) | ||
} else { | ||
context.guild.publicRole | ||
} | ||
|
||
// Member and Role are both IPermissionHolder so this should not happen | ||
// This check is here to smart-cast target to IPermissionHolder for later code | ||
if (target !is IPermissionHolder) error("Target must be a IPermissionHolder") | ||
|
||
if (LockManager.isLocked(channel, target)) { | ||
when (target) { | ||
is Role -> context.typedMessaging.replyWarning(context.i18n("commands.lock.already_locked_role", channel.name, target.asMention)) | ||
is Member -> context.typedMessaging.replyWarning(context.i18n("commands.lock.already_locked_member", channel.name, target.asMention)) | ||
} | ||
return | ||
} | ||
|
||
val success = { | ||
val name: String = when(target) { | ||
is Role -> target.asMention | ||
is Member -> target.asMention | ||
else -> error("Target must be either a Role or a Member") | ||
} | ||
|
||
if (target is Member) { | ||
context.typedMessaging.replySuccess((context.i18n("commands.lock.success_member", channel.name, target.asMention))) | ||
} else if (target is Role) { | ||
context.typedMessaging.replySuccess((context.i18n("commands.lock.success_role", channel.name, target.asMention))) | ||
} | ||
|
||
} | ||
|
||
val failure = { throwable: Throwable -> | ||
if (throwable is PermissionException) { | ||
context.uiMessaging.sendBotDiscordPermError(throwable.permission) | ||
} else { | ||
context.typedMessaging.replyException("Something went wrong!", throwable) | ||
} | ||
} | ||
|
||
LockManager.lock(channel, target, success, failure) | ||
} | ||
|
||
|
||
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.IPermissionHolder | ||
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.guild.publicRole | ||
} | ||
|
||
// Member and Role are both IPermissionHolder so this should not happen | ||
// This check is here to smart-cast target to IPermissionHolder for later code | ||
if (target !is IPermissionHolder) error("Target must be a IPermissionHolder") | ||
|
||
val unlockFutureData = ScheduledAction.LockActionData(channel.idLong, Status.NEUTRAL, 0, 0) | ||
unlockFutureData.oldPermission = LockManager.getPerm(channel, target).target | ||
unlockFutureData.targetRoleID = target.idLong | ||
|
||
val success = { | ||
ScheduledActionManager.registerScheduledAction(ScheduledAction( | ||
ActionType.UNLOCK, | ||
unlockFutureData, | ||
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) + ")" | ||
|
||
val message = when (target) { | ||
is Role -> context.i18n("commands.templock.success_role", channel.name, target.asMention, textDuration) | ||
is Member -> context.i18n("commands.templock.success_member", channel.name, target.asMention, textDuration) | ||
else -> error("Target should be either Role or Member!") | ||
} | ||
|
||
context.typedMessaging.replySuccess(message) | ||
} | ||
|
||
val failure = { throwable: Throwable -> | ||
if (throwable is PermissionException) { | ||
context.uiMessaging.sendBotDiscordPermError(throwable.permission) | ||
} else { | ||
context.typedMessaging.replyException("Something went wrong!", throwable) | ||
} | ||
} | ||
|
||
LockManager.lock(channel, target, success, failure); | ||
} | ||
|
||
|
||
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 | ||
} | ||
|
||
} |
78 changes: 78 additions & 0 deletions
78
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,78 @@ | ||
package org.cascadebot.cascadebot.commands.moderation | ||
|
||
import javassist.NotFoundException | ||
import net.dv8tion.jda.api.Permission | ||
import net.dv8tion.jda.api.entities.IPermissionHolder | ||
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 | ||
} | ||
|
||
// Member and Role are both IPermissionHolder so this should not happen | ||
// This check is here to smart-cast target to IPermissionHolder for later code | ||
if (target !is IPermissionHolder) error("Target must be a IPermissionHolder") | ||
|
||
val success = { completed: Boolean -> | ||
if (completed) { | ||
when (target) { | ||
is Role -> context.typedMessaging.replySuccess(context.i18n("commands.unlock.success_role", channel.name, target.asMention)) | ||
is Member -> context.typedMessaging.replySuccess(context.i18n("commands.unlock.success_member", channel.name, target.asMention)) | ||
else -> error("Target should be one of Role or Member") | ||
} | ||
} else { | ||
when (target) { | ||
is Role -> context.typedMessaging.replySuccess(context.i18n("commands.unlock.failure_role", channel.name, target.asMention)) | ||
is Member -> context.typedMessaging.replySuccess(context.i18n("commands.unlock.failure_member", channel.name, target.asMention)) | ||
else -> error("Target should be one of Role or Member") | ||
} | ||
} | ||
} | ||
|
||
val failure = { throwable: Throwable -> | ||
if (throwable is PermissionException) { | ||
context.uiMessaging.sendBotDiscordPermError(throwable.permission) | ||
} else { | ||
context.typedMessaging.replyException("Something went wrong!", throwable) | ||
} | ||
} | ||
|
||
LockManager.unlock(context.guild, channel, target, success, failure) | ||
} | ||
|
||
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 | ||
} | ||
|
||
} |
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.