Skip to content

Commit

Permalink
Complete work on storing original permission for locked channels
Browse files Browse the repository at this point in the history
  • Loading branch information
NoComment1105 committed Apr 10, 2024
1 parent bb61a66 commit 6cc8472
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 11 deletions.
Expand Up @@ -10,6 +10,7 @@ import mu.KotlinLogging
import org.hyacinthbots.lilybot.database.Cleanups.cleanupGuildData
import org.hyacinthbots.lilybot.database.Cleanups.cleanupThreadData
import org.hyacinthbots.lilybot.database.collections.GithubCollection
import org.hyacinthbots.lilybot.database.collections.LockedChannelCollection
import org.hyacinthbots.lilybot.database.collections.LoggingConfigCollection
import org.hyacinthbots.lilybot.database.collections.ModerationConfigCollection
import org.hyacinthbots.lilybot.database.collections.NewsChannelPublishingCollection
Expand Down Expand Up @@ -56,14 +57,16 @@ object Cleanups : KordExKoinComponent {
cleanupsLogger.info("Starting guild cleanup...")
val leaveTimeData = guildLeaveTimeCollection.find().toList()
var deletedGuildData = 0
val now = Clock.System.now()

leaveTimeData.forEach {
// Calculate the time since Lily left the guild.
val leaveDuration = Clock.System.now() - it.guildLeaveTime
val leaveDuration = now - it.guildLeaveTime

if (leaveDuration.inWholeDays > 30) {
// If the bot has been out of the guild for more than 30 days, delete any related data.
GithubCollection().removeDefaultRepo(it.guildId)
LockedChannelCollection().removeAllLockedChannels(it.guildId)
LoggingConfigCollection().clearConfig(it.guildId)
ModerationConfigCollection().clearConfig(it.guildId)
NewsChannelPublishingCollection().clearAutoPublishingForGuild(it.guildId)
Expand Down
Expand Up @@ -7,18 +7,65 @@ import org.hyacinthbots.lilybot.database.entities.LockedChannelData
import org.koin.core.component.inject
import org.litote.kmongo.eq

/**
* This class contains the function for interacting with the [Locked Channel Database][LockedChannelData]. This class
* contains functions for getting, setting and removing locked channels
*
* @since 5.0.0
* @see addLockedChannel
* @see removeLockedChannel
* @see removeAllLockedChannels
* @see getLockedChannel
*/
class LockedChannelCollection : KordExKoinComponent {
private val db: Database by inject()

@PublishedApi
internal val collection = db.mainDatabase.getCollection<LockedChannelData>()

suspend inline fun addLockedChannel(data: LockedChannelData) =
collection.insertOne(data)
/**
* Adds the data about a newly locked channel to the database.
*
* @param data The [LockedChannelData] for the locked channel
*
* @author NoComment1105
* @since 5.0.0
*/
suspend inline fun addLockedChannel(data: LockedChannelData) = collection.insertOne(data)

/**
* Removes a locked channel from the database. This is usually called when a channel is unlocked.
*
* @param inputGuildId The ID of the guild the locked channel is in
* @param inputChannelId The ID of the locked channel itself
*
* @author NoComment1105
* @since 5.0.0
*/
suspend inline fun removeLockedChannel(inputGuildId: Snowflake, inputChannelId: Snowflake) =
collection.deleteOne(LockedChannelData::guildId eq inputGuildId, LockedChannelData::channelId eq inputChannelId)

/**
* Removes all locked channels for a given guild from the database. Used in guild cleanups.
*
* @param inputGuildId The ID of the guild to remove the locked channels from
*
* @author NoComment1105
* @since 5.0.0
*/
suspend inline fun removeAllLockedChannels(inputGuildId: Snowflake) =
collection.deleteMany(LockedChannelData::guildId eq inputGuildId)

/**
* Gets a locked channel based on the input parameters.
*
* @param inputGuildId The ID of the guild the locked channel is in
* @param inputChannelId The ID of the channel to get the locked data for
* @return A [LockedChannelData] object for the given channel
*
* @author NoComment1105
* @since 5.0.0
*/
suspend inline fun getLockedChannel(inputGuildId: Snowflake, inputChannelId: Snowflake): LockedChannelData? =
collection.findOne(LockedChannelData::guildId eq inputGuildId, LockedChannelData::channelId eq inputChannelId)
}
@@ -1,14 +1,21 @@
package org.hyacinthbots.lilybot.database.entities

import dev.kord.common.entity.Permissions
import dev.kord.common.entity.Snowflake
import kotlinx.serialization.Contextual
import kotlinx.serialization.Serializable

/**
* The data for locked channels.
*
* @property guildId The ID of the guild the locked channel is in
* @property channelId The ID of the channel that is locked
* @property allowed The Discord Bit Set code for the allowed permissions, formatted as a string
* @property denied The Discord Bit Set code for the denied permissions, formatted as a string
* @since 5.0.0
*/
@Serializable
data class LockedChannelData(
val guildId: Snowflake,
val channelId: Snowflake,
@Contextual val allowed: Permissions,
@Contextual val denied: Permissions,
val allowed: String,
val denied: String,
)
Expand Up @@ -11,6 +11,7 @@ import com.kotlindiscord.kord.extensions.commands.converters.impl.optionalChanne
import com.kotlindiscord.kord.extensions.extensions.Extension
import com.kotlindiscord.kord.extensions.extensions.ephemeralSlashCommand
import com.kotlindiscord.kord.extensions.types.EphemeralInteractionContext
import dev.kord.common.DiscordBitSet
import dev.kord.common.entity.Permission
import dev.kord.common.entity.Permissions
import dev.kord.core.behavior.channel.asChannelOfOrNull
Expand Down Expand Up @@ -79,8 +80,8 @@ class LockingCommands : Extension() {
LockedChannelData(
guildId = guild!!.id,
channelId = targetChannel.id,
allowed = currentChannelPerms.data.allowed,
denied = currentChannelPerms.data.denied
allowed = currentChannelPerms.data.allowed.code.value,
denied = currentChannelPerms.data.denied.code.value
)
)

Expand Down Expand Up @@ -223,8 +224,8 @@ class LockingCommands : Extension() {
}

targetChannel.editRolePermission(guild!!.id) {
denied = lockedChannel.denied
allowed = lockedChannel.allowed
denied = Permissions.Builder(DiscordBitSet(lockedChannel.denied)).build()
allowed = Permissions.Builder(DiscordBitSet(lockedChannel.allowed)).build()
}

targetChannel.createEmbed {
Expand Down

0 comments on commit 6cc8472

Please sign in to comment.