Skip to content

Commit

Permalink
Add bot AutoMod
Browse files Browse the repository at this point in the history
  • Loading branch information
Nickster258 committed Oct 12, 2023
1 parent 84b993c commit 9a821ba
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/main/kotlin/org/openredstone/chad/Chad.kt
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,13 @@ fun main(args: Array<String>) = runBlocking {
logger.info("Starting listeners...")

startDiscordListeners(
logger,
discordApi,
CommandExecutor(chadConfig.commandChar, commands),
chadConfig.disableSpoilers,
chadConfig.botAutomod.enableBotAutomod,
chadConfig.botAutomod.automodChannelId,
chadConfig.botAutomod.regexes,
chadConfig.welcomeChannelId,
chadConfig.greetings,
chadConfig.ingameBotRoleId,
Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/org/openredstone/chad/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ data class ChadConfig(
val fractalDeets: FractalConfig,
val authorizedDiscordRoles: List<String>,
val logging: LoggingConfig,
val botAutomod: AutomodConfig,
val greetings: List<String>,
val insults: List<String>,
)
Expand All @@ -29,6 +30,8 @@ object ChadSpec : ConfigSpec("") {
val chad by required<ChadConfig>()
}

data class AutomodConfig(val enableBotAutomod: Boolean, val automodChannelId: Long, val regexes: List<String>)

data class FractalConfig(val size: Int, val maxIterations: Int, val messiness: Int, val zoom: Double)

data class NotificationRoleConfig(val name: String, val role: String, val description: String)
Expand Down
51 changes: 51 additions & 0 deletions src/main/kotlin/org/openredstone/chad/DiscordListener.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import khttp.post
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.future.await
import kotlinx.coroutines.launch
import mu.KLogger
import mu.KotlinLogging
import org.javacord.api.DiscordApi
import org.javacord.api.entity.message.Message
import org.javacord.api.entity.message.embed.EmbedBuilder
import org.javacord.api.entity.permission.Role
import org.javacord.api.event.message.MessageCreateEvent
import org.openredstone.chad.commands.CommandExecutor
Expand All @@ -15,10 +17,20 @@ import org.openredstone.chad.commands.Sender

val spoilerLogger = KotlinLogging.logger("Spoiler listener")

internal fun Message.getUrl(): String {
val server = this.server.toNullable()?.id ?: throw Exception("Oof")
val channel = this.channel.id
return "https://discord.com/channels/${server}/${channel}/${this.id}"
}

fun startDiscordListeners(
logger: KLogger,
discordApi: DiscordApi,
executor: CommandExecutor,
disableSpoilers: Boolean,
botAutomod: Boolean,
automodChannelId: Long,
automodRegexes: List<String>,
welcomeChannel: Long,
greetings: List<String>,
ingameBotRole: String,
Expand All @@ -30,6 +42,9 @@ fun startDiscordListeners(
if (disableSpoilers) {
startSpoilerListener(discordApi, coroutineScope)
}
if (botAutomod) {
startBotAutomod(logger, discordApi, automodChannelId, automodRegexes)
}
if (greetings.isNotEmpty()) {
startJoinListener(discordApi, welcomeChannel, greetings, coroutineScope)
}
Expand All @@ -45,6 +60,42 @@ private fun startJoinListener(discordApi: DiscordApi, welcomeChannel: Long, gree
}
}

private fun startBotAutomod(
logger: KLogger,
discordApi: DiscordApi,
automodChannelId: Long,
automodRegexes: List<String>
) {
val automodChannel =
discordApi.getChannelById(automodChannelId).toNullable()?.asServerTextChannel()?.toNullable() ?: run {
logger.error { "Could not locate AutoMod channel" }
return
}
val compiledRegexes = automodRegexes.map { Regex(it, RegexOption.IGNORE_CASE) }
fun onBotMessage(event: MessageCreateEvent) {
val user = event.messageAuthor.asUser().toNullable() ?: return
if (discordApi.clientId == user.id || !user.isBot) {
return
}
val body = event.message.content
val matches = compiledRegexes.filter { it.containsMatchIn(body) }
if (matches.isNotEmpty()) {
val messageUrl = event.message.getUrl()
logger.warn("Flagged message for bot AutoMod! (Matches ${matches}) $messageUrl")
val embed = EmbedBuilder().setDescription(body)
matches.forEach { embed.addField("Matches", it.pattern) }
automodChannel.sendMessage("Flagged a message $messageUrl", embed)
}
}
discordApi.addMessageCreateListener { event ->
try {
onBotMessage(event)
} catch (e: Exception) {
logger.error(e) { "onBotMessage error" }
}
}
}

private fun startDiscordCommandListener(
discordApi: DiscordApi,
executor: CommandExecutor,
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/org/openredstone/chad/commands/Command.kt
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class CommandExecutor(private val commandChar: Char, private val commands: Comma
if (commandResponse != null) {
sql.insertHistory(
name,
args.joinToString(", "),
args,
commandResponse.reply,
service,
senderToInsert
Expand Down

0 comments on commit 9a821ba

Please sign in to comment.