Skip to content
This repository has been archived by the owner on Sep 24, 2022. It is now read-only.

🔐 Lock commands #241

Open
wants to merge 19 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.bson.Document;
import org.bson.codecs.configuration.CodecRegistries;
import org.bson.codecs.configuration.CodecRegistry;
import org.cascadebot.cascadebot.data.managers.LockPermissionState;
import org.cascadebot.cascadebot.utils.lists.WeightedList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -42,7 +43,7 @@ public class DatabaseManager {
"org.cascadebot.cascadebot.utils.lists",
"org.cascadebot.cascadebot.scheduler",
"org.cascadebot.shared"
).register(WeightedList.WeightPair.class).build())
).register(WeightedList.WeightPair.class, LockPermissionState.class).build())
);

@Getter
Expand Down
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
}

}
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
}

}
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() {
weeryan17 marked this conversation as resolved.
Show resolved Hide resolved
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
}

}
Loading