Skip to content

Commit

Permalink
Add force flag to archive command
Browse files Browse the repository at this point in the history
  • Loading branch information
randomnetcat committed Mar 22, 2024
1 parent d1401f1 commit c9a4295
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions src/main/kotlin/org/randomcat/agorabot/commands/Archive.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ import java.time.format.DateTimeFormatter
import kotlin.io.path.ExperimentalPathApi
import kotlin.io.path.copyToRecursively

private val ARCHIVE_PERMISSION = LogicalOrPermission(listOf(GuildScope.command("archive"), BotScope.admin()))
private val ARCHIVE_PERMISSION = GuildScope.command("archive")
private val FORCE_PERMISSION = BotScope.admin()
private val OVERALL_PERMISSION = LogicalOrPermission(listOf(ARCHIVE_PERMISSION, FORCE_PERMISSION))

private val LOGGER = LoggerFactory.getLogger("AgoraBotArchiveCommand")

Expand Down Expand Up @@ -90,11 +92,12 @@ class ArchiveCommand(
matchFirst {
args(RemainingStringArgs("marker_or_id"))
.requires(InGuild)
.permissions(ARCHIVE_PERMISSION) cmd@{ (args) ->
.permissions(OVERALL_PERMISSION) cmd@{ (args) ->
val isStoreLocally = args.contains("store_locally")
val isAll = args.contains("all")
val isForce = args.contains("force")

val rawIds = args - listOf("store_locally", "all")
val rawIds = args - listOf("store_locally", "all", "force")

if (!isStoreLocally) {
respond("Uploading archives is currently not supported")
Expand All @@ -106,6 +109,18 @@ class ArchiveCommand(
return@cmd
}

if (isForce) {
if (!senderHasPermission(FORCE_PERMISSION)) {
respond("Only a bot admin can forcibly archive a server.")
return@cmd
}
} else {
if (!senderHasPermission(ARCHIVE_PERMISSION)) {
respond("You do not have permission: need $ARCHIVE_PERMISSION")
return@cmd
}
}

if (rawIds.isEmpty() && !isAll) {
respond("Must provide IDs or \"all\".")
return@cmd
Expand Down Expand Up @@ -145,6 +160,7 @@ class ArchiveCommand(
TODO("uploading not supported")
}
},
force = isForce,
)
}
}
Expand All @@ -153,16 +169,19 @@ class ArchiveCommand(
private suspend fun BaseCommandExecutionReceiverGuilded.doArchive(
channels: List<GuildChannel>,
storeArchiveResult: suspend BaseCommandExecutionReceiverGuilded.(Path) -> Unit,
force: Boolean,
) {
val member = currentMessageEvent.member ?: error("Member should exist because this is in a Guild")

for (channel in channels) {
if (
!member.hasPermission(channel, DiscordPermission.VIEW_CHANNEL) ||
!member.hasPermission(channel, DiscordPermission.MESSAGE_HISTORY)
) {
respond("You do not have permission to read in the channel <#${channel.id}>.")
return
if (!force) {
for (channel in channels) {
if (
!member.hasPermission(channel, DiscordPermission.VIEW_CHANNEL) ||
!member.hasPermission(channel, DiscordPermission.MESSAGE_HISTORY)
) {
respond("You do not have permission to read in the channel <#${channel.id}>.")
return
}
}
}

Expand Down

0 comments on commit c9a4295

Please sign in to comment.