Skip to content

Commit

Permalink
More event logging (#410)
Browse files Browse the repository at this point in the history
* Start work on audit-log event based logging

* Refactor it all because yes

* Create action transferring database to move important information over to the event action

* Log Kicks, send quick actions to db, generalise channel creation logs

* Start work on channel updates

* Add the rest of the channel update fields

* Fully set up channel edit logging

* Add scheduled event logging

* Invite events and Role events

* Thread creates and deletes logging + updated kordex

* Log timeouts created by lily

* Fix timeout logging, yeehaw actual code is done now

* Add docs

* Fix package references

* Remove the logging, however funny it was to me it is not needed

* Complete merge

* Add configuration, split down into two event categories

* Update migrations for utility config

* Reduce code duplication and rename some packages

* Fix formatting of applied tags
  • Loading branch information
NoComment1105 authored Aug 29, 2024
1 parent 7d2b6ff commit 378f224
Show file tree
Hide file tree
Showing 33 changed files with 1,673 additions and 426 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ build/
out/
classes/

# kotlin
.kotlin/

# eclipse

*.launch
Expand Down
26 changes: 15 additions & 11 deletions src/main/kotlin/org/hyacinthbots/lilybot/LilyBot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,22 @@ import org.hyacinthbots.lilybot.extensions.moderation.commands.LockingCommands
import org.hyacinthbots.lilybot.extensions.moderation.commands.ModUtilities
import org.hyacinthbots.lilybot.extensions.moderation.commands.ModerationCommands
import org.hyacinthbots.lilybot.extensions.moderation.commands.Report
import org.hyacinthbots.lilybot.extensions.moderation.events.ModerationEvents
import org.hyacinthbots.lilybot.extensions.threads.AutoThreading
import org.hyacinthbots.lilybot.extensions.threads.ModThreadInviting
import org.hyacinthbots.lilybot.extensions.threads.ThreadControl
import org.hyacinthbots.lilybot.extensions.utils.commands.GalleryChannel
import org.hyacinthbots.lilybot.extensions.utils.commands.Github
import org.hyacinthbots.lilybot.extensions.utils.commands.GuildAnnouncements
import org.hyacinthbots.lilybot.extensions.utils.commands.InfoCommands
import org.hyacinthbots.lilybot.extensions.utils.commands.NewsChannelPublishing
import org.hyacinthbots.lilybot.extensions.utils.commands.PublicUtilities
import org.hyacinthbots.lilybot.extensions.utils.commands.Reminders
import org.hyacinthbots.lilybot.extensions.utils.commands.RoleMenu
import org.hyacinthbots.lilybot.extensions.utils.commands.StartupHooks
import org.hyacinthbots.lilybot.extensions.utils.commands.StatusPing
import org.hyacinthbots.lilybot.extensions.utils.commands.Tags
import org.hyacinthbots.lilybot.extensions.utility.commands.GalleryChannel
import org.hyacinthbots.lilybot.extensions.utility.commands.Github
import org.hyacinthbots.lilybot.extensions.utility.commands.GuildAnnouncements
import org.hyacinthbots.lilybot.extensions.utility.commands.InfoCommands
import org.hyacinthbots.lilybot.extensions.utility.commands.NewsChannelPublishing
import org.hyacinthbots.lilybot.extensions.utility.commands.PublicUtilities
import org.hyacinthbots.lilybot.extensions.utility.commands.Reminders
import org.hyacinthbots.lilybot.extensions.utility.commands.RoleMenu
import org.hyacinthbots.lilybot.extensions.utility.commands.StartupHooks
import org.hyacinthbots.lilybot.extensions.utility.commands.StatusPing
import org.hyacinthbots.lilybot.extensions.utility.commands.Tags
import org.hyacinthbots.lilybot.extensions.utility.events.UtilityEvents
import org.hyacinthbots.lilybot.internal.BuildInfo
import org.hyacinthbots.lilybot.utils.BOT_TOKEN
import org.hyacinthbots.lilybot.utils.ENVIRONMENT
Expand Down Expand Up @@ -177,6 +179,7 @@ suspend fun main() {
add(::MemberLogging)
add(::MessageDelete)
add(::MessageEdit)
add(::ModerationEvents)
add(::ModThreadInviting)
add(::ModUtilities)
add(::ModerationCommands)
Expand All @@ -189,6 +192,7 @@ suspend fun main() {
add(::StatusPing)
add(::Tags)
add(::ThreadControl)
add(::UtilityEvents)

/*
The welcome channel extension allows users to designate a YAML file to create a channel with
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package org.hyacinthbots.lilybot.database.collections

import dev.kord.common.entity.Snowflake
import dev.kordex.core.koin.KordExKoinComponent
import org.hyacinthbots.lilybot.database.Database
import org.hyacinthbots.lilybot.database.entities.ActionData
import org.hyacinthbots.lilybot.database.entities.ModerationActionData
import org.hyacinthbots.lilybot.extensions.moderation.utils.ModerationAction
import org.koin.core.component.inject
import org.litote.kmongo.eq

/**
* This class contains the function for interacting with the [Moderation Action Database][ModerationActionData]. This
* class contains functions for getting, setting, removing and ignoring actions
*
* @since 5.0.0
* @see addAction
* @see removeAction
* @see getAction
* @see declareActionToIgnore
* @see shouldIgnoreAction
*/
class ModerationActionCollection : KordExKoinComponent {
private val db: Database by inject()

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

/**
* Adds an action that occurred.
*
* @param action The type of action you're adding
* @param guildId The ID of the guild the action occurred in
* @param targetUserId The ID of the user this action happened to
* @param data The [ActionData] for the action
* @param ignore Whether to ignore the action or not. Defaults to false
* @author NoComment1105
* @since 5.0.0
*/
suspend inline fun addAction(
action: ModerationAction,
guildId: Snowflake,
targetUserId: Snowflake,
data: ActionData,
ignore: Boolean = false
) = collection.insertOne(ModerationActionData(action, guildId, targetUserId, data, ignore))

/**
* Removes an action that occurred.
*
* @param type The type of action you're removing
* @param guildId The ID of the guild the action occurred in
* @param targetUserId The ID of the user this action happened to
* @author NoComment1105
* @since 5.0.0
*/
suspend inline fun removeAction(type: ModerationAction, guildId: Snowflake, targetUserId: Snowflake) =
collection.deleteOne(
ModerationActionData::actionType eq type,
ModerationActionData::guildId eq guildId,
ModerationActionData::targetUserId eq targetUserId
)

/**
* Gets an action that occurred.
*
* @param type The type of action you're looking for
* @param guildId The ID of the guild the action occurred in
* @param targetUserId The ID of the user this action happened to
* @return The [data][ModerationActionData] for the event. Can be null if there is no action.
* @author NoComment1105
* @since 5.0.0
*/
suspend inline fun getAction(
type: ModerationAction,
guildId: Snowflake,
targetUserId: Snowflake
): ModerationActionData? =
collection.findOne(
ModerationActionData::actionType eq type,
ModerationActionData::guildId eq guildId,
ModerationActionData::targetUserId eq targetUserId
)

/**
* Sets an action as ignored. Convenience function more than anything
*
* @param type The type of action you're looking for
* @param guildId The ID of the guild the action occurred in
* @param targetUserId The ID of the user this action happened to
* @author NoComment1105
* @since 5.0.0
*/
suspend inline fun declareActionToIgnore(type: ModerationAction, guildId: Snowflake, targetUserId: Snowflake) =
addAction(type, guildId, targetUserId, ActionData(null, null, null, null, null, null, null), true)

/**
* Checks if an action should be ignored or not. Convenience function more than anything.
*
* @param type The type of action you're looking for
* @param guildId The ID of the guild the action occurred in
* @param targetUserId The ID of the user this action happened to
* @return True if the action should be ignored, false if otherwise
* @author NoComment1105
* @since 5.0.0
*/
suspend inline fun shouldIgnoreAction(
type: ModerationAction,
guildId: Snowflake,
targetUserId: Snowflake
): Boolean? =
collection.findOne(
ModerationActionData::actionType eq type,
ModerationActionData::guildId eq guildId,
ModerationActionData::targetUserId eq targetUserId
)?.ignore
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,9 @@ data class ModerationConfigData(
@Serializable
data class UtilityConfigData(
val guildId: Snowflake,
val utilityLogChannel: Snowflake?
val utilityLogChannel: Snowflake?,
val logChannelUpdates: Boolean,
val logEventUpdates: Boolean,
val logInviteUpdates: Boolean,
val logRoleUpdates: Boolean
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.hyacinthbots.lilybot.database.entities

import dev.kord.common.entity.Snowflake
import kotlinx.datetime.DateTimePeriod
import kotlinx.datetime.Instant
import kotlinx.serialization.Serializable
import org.hyacinthbots.lilybot.extensions.moderation.utils.ModerationAction

/**
* The data for Moderation action.
*
* @property actionType The type of action you're adding
* @property guildId The ID of the guild the action occurred in
* @property targetUserId The ID of the user this action happened to
* @property data The [ActionData] for the action
* @property ignore Whether to ignore the action or not. Defaults to false
* @since 5.0.0
*/
@Serializable
data class ModerationActionData(
val actionType: ModerationAction,
val guildId: Snowflake,
val targetUserId: Snowflake,
val data: ActionData,
val ignore: Boolean = false
)

/**
* Further, more in-depth data about a [moderation action][ModerationActionData].
*
* @property actioner The ID of the user that requested the action
* @property deletedMessages The amount of messages deleted in the action
* @property timeData The [TimeData] for the action
* @property reason The reason for the action
* @property dmOutcome The outcome of trying to send a DM to the user
* @property dmOverride Whether the DM sending function was override
* @property imageUrl The URL for the image attached to the action
* @since 5.0.0
*/
@Serializable
data class ActionData(
val actioner: Snowflake?,
val deletedMessages: Int?,
val timeData: TimeData?,
val reason: String?,
val dmOutcome: Boolean?,
val dmOverride: Boolean?,
val imageUrl: String?
)

/**
* Further, more in-depth data about the [time data for actions][ActionData.timeData].
*
* @property durationDtp The Duration as a [DateTimePeriod]
* @property durationInst The Duration as an [Instant]
* @property start The start [Instant] of the action
* @property end The end [Instant] of the action
* @since 5.0.0
*/
@Serializable
data class TimeData(
val durationDtp: DateTimePeriod?,
val durationInst: Instant?,
val start: Instant? = null,
val end: Instant? = null,
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.hyacinthbots.lilybot.database.migrations.config

import org.hyacinthbots.lilybot.database.entities.ModerationConfigData
import org.hyacinthbots.lilybot.database.entities.UtilityConfigData
import org.litote.kmongo.coroutine.CoroutineDatabase
import org.litote.kmongo.exists
import org.litote.kmongo.setValue
Expand All @@ -12,4 +13,22 @@ suspend fun configV7(db: CoroutineDatabase) {
setValue(ModerationConfigData::autoInviteModeratorRole, null)
)
}
with(db.getCollection<UtilityConfigData>("utilityConfigData")) {
updateMany(
UtilityConfigData::logChannelUpdates exists false,
setValue(UtilityConfigData::logChannelUpdates, false)
)
updateMany(
UtilityConfigData::logEventUpdates exists false,
setValue(UtilityConfigData::logEventUpdates, false)
)
updateMany(
UtilityConfigData::logInviteUpdates exists false,
setValue(UtilityConfigData::logInviteUpdates, false)
)
updateMany(
UtilityConfigData::logRoleUpdates exists false,
setValue(UtilityConfigData::logRoleUpdates, false)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ suspend fun mainV10(db: CoroutineDatabase) {
}
db.createCollection("lockedChannelData")
db.createCollection("temporaryBanData")
db.createCollection("moderationActionCacheData")
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.hyacinthbots.lilybot.extensions.config.commands.configClearCommand
import org.hyacinthbots.lilybot.extensions.config.commands.configViewCommand
import org.hyacinthbots.lilybot.extensions.logging.config.loggingCommand
import org.hyacinthbots.lilybot.extensions.moderation.config.moderationCommand
import org.hyacinthbots.lilybot.extensions.utils.config.utilityCommand
import org.hyacinthbots.lilybot.extensions.utility.config.utilityCommand

class ConfigExtension : Extension() {
override val name: String = "config"
Expand Down
Loading

0 comments on commit 378f224

Please sign in to comment.