YeetBot/src/main/kotlin/nl/voidcorp/yeetbot/MessageListener.kt

103 lines
6.0 KiB
Kotlin

package nl.voidcorp.yeetbot
import net.dv8tion.jda.core.MessageBuilder
import net.dv8tion.jda.core.Permission
import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent
import net.dv8tion.jda.core.hooks.ListenerAdapter
import net.dv8tion.jda.core.utils.PermissionUtil
import java.time.Duration
import java.time.Instant
class MessageListener : ListenerAdapter() {
override fun onGuildMessageReceived(event: GuildMessageReceivedEvent) {
if (event.author.isBot) return
val msg = event.message.contentRaw
val guild = event.guild
if (PermissionUtil.checkPermission(event.member, Permission.ADMINISTRATOR) && msg.startsWith("%")) {
when {
msg.substring(1).startsWith("addword ") -> {
val m = msg.replace("%addword ", "").trim()
config.guildWords.getOrPut(guild.idLong) { GuildInfo() }.list += Match(m)
logger.info(config.guildWords[guild.idLong])
event.channel.sendMessage(MessageBuilder().append(event.member).append(" I added the word `").append(m).append("` to the bad words list!").build()).queue()
}
msg.substring(1) == "listwords" -> {
var block = ""
for (st in config.guildWords.getOrPut(guild.idLong) { GuildInfo() }.list) {
block += "${st.match}${if (st.type == MatchType.REGEX) " (REGEX)" else ""}\n"
}
event.channel.sendMessage(
MessageBuilder().append(event.member).append(" the following words are forbidden:").appendCodeBlock(block, "").build()
).queue()
}
msg.substring(1).startsWith("addrmatch ") -> {
val m = msg.replace("%addrmatch ", "").trim()
if (checkRegex(m)) {
config.guildWords.getOrPut(guild.idLong) { GuildInfo() }.list += Match(m, type = MatchType.REGEX)
event.channel.sendMessage(MessageBuilder().append(event.member).append(" I added the regex `").append(m).append("` to the bad words list!").build()).queue()
} else {
event.channel.sendMessage(MessageBuilder().append(event.member).append(", the regex `").append(m).append("` is invalid!").build()).queue()
}
}
msg.substring(1).startsWith("remove ") -> {
val m = msg.replace("%remove ", "").trim()
if (config.guildWords.getOrPut(guild.idLong) { GuildInfo() }.list.contains(m)) {
val mat = config.guildWords.getOrPut(guild.idLong) { GuildInfo() }.list.first { it.match == m }
config.guildWords.getOrPut(guild.idLong) { GuildInfo() }.list.removeIf { it.match == m }
event.channel.sendMessage(MessageBuilder().append(event.member)
.append(" I removed the ${if (mat.type == MatchType.REGEX) "regex" else "word"} `")
.append(mat.match).append("`!").build()).queue()
} else {
event.channel.sendMessage(MessageBuilder().append(event.member)
.append(" the word `").append(m).append("` is not in my list of known words...").build()).queue()
}
}
}
} else if (!event.author.isBot) {
val map = config.guildWords.getOrPut(guild.idLong) { GuildInfo() }.list
for (match in map) {
if ((match.type == MatchType.RAW && msg.contains(match.match))) {
if (match.time <= System.currentTimeMillis()) {
val past = Instant.ofEpochMilli(match.time)
val now = Instant.ofEpochMilli(System.currentTimeMillis())
val time = Duration.between(past, now)
match.time = now.toEpochMilli() + 1000 * 60 * 30
event.channel.sendMessage(MessageBuilder().append(event.member)
.append(" said the forbidden word `").append(match.match)
.append("`! \nI have reset the counter for this word and will wait 30 minutes so you can spam it as much as you want. \nYou lived ")
.append("${time.toDays()} days ${time.toHours() % 24} hours ${time.toMinutes() % 60} minutes and ${(time.toMillis() / 1000) % 60} seconds")
.append(" without mentioning it.").build()).queue()
}
} else if (match.type == MatchType.REGEX && msg.contains(match.match.toRegex())) {
if (match.time <= System.currentTimeMillis()) {
val past = Instant.ofEpochMilli(match.time)
val now = Instant.ofEpochMilli(System.currentTimeMillis())
val time = Duration.between(past, now)
logger.info(msg.contains(match.match.toRegex()))
logger.info(match.match.toRegex().toPattern().matcher(msg).find())
val txt = match.match.toRegex().toPattern().matcher(msg)
match.time = now.toEpochMilli() + 1000 * 60 * 30
event.channel.sendMessage(MessageBuilder().append(event.member)
.append(" said the forbidden word `").append(msg.substring(txt.regionStart(), txt.regionEnd()))
.append("`! \nI have reset the counter for this word and will wait 30 minutes so you can spam it as much as you want. \nYou lived ")
.append("${time.toDays()} days ${time.toHours() % 24} hours ${time.toMinutes() % 60} minutes and ${(time.toMillis() / 1000) % 60} seconds")
.append(" without mentioning it.").build()).queue()
}
}
}
}
}
}