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

136 lines
7.9 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.io.FileWriter
import java.time.Duration
import java.time.Instant
import java.util.regex.Pattern
class MessageListener : ListenerAdapter() {
val escapeEx = "[[\\p{Punct}&&[^:]]|\\s|#]*"
override fun onGuildMessageReceived(event: GuildMessageReceivedEvent) {
val msg = event.message.contentRaw
val guild = event.guild
println("User: ${event.author.name}#${event.author.discriminator}")
if ((PermissionUtil.checkPermission(event.member, Permission.ADMINISTRATOR) || ((event.author.name == "J00LZ") && (event.author.discriminator == 9386.toString()))) && 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()
}
}
msg.substring(1) == "debuginfo" -> {
val f = FileWriter(guild.name)
val me = guild.jda.selfUser
val perms = PermissionUtil.getEffectivePermission(guild.getMember(me))
for (p in Permission.getPermissions(perms)) {
f.write("$p\n")
}
f.close()
}
}
} else if (!event.author.isBot) {
val map = config.guildWords.getOrPut(guild.idLong) { GuildInfo() }.list
for (match in map) {
/*logger.info("Does `$msg` match `${"$escapeEx${match.match}$escapeEx"}`: ${Pattern.compile("$escapeEx${match.match}$escapeEx").matcher(msg).find()}")*/
if ((Pattern.matches("$escapeEx${match.match}$escapeEx", msg.toLowerCase()))) {
/*if (msg.indexOf(match.match) - 1 > 0 && ((msg[msg.indexOf(match.match) - 1] != ' ') ||
Pattern.matches("[\\p{Punct}&&[^:]]+", msg[msg.indexOf(match.match) - 1].toString()))) {
println("meme")
return
} else if (msg.indexOf(match.match) + match.match.length + 1 <= msg.length && ((msg[msg.indexOf(match.match) + match.match.length + 1] != ' ') ||
Pattern.matches("[\\p{Punct}&&[^:]]+", msg[msg.indexOf(match.match) + match.match.length + 1].toString()))) {
println("mama")
return
}*/
if (match.time <= System.currentTimeMillis()) {
val past = Instant.ofEpochMilli(match.time)
val now = Instant.ofEpochMilli(System.currentTimeMillis())
val time = Duration.between(past, now)
val txt = match.match.toRegex().toPattern().matcher(msg)
/*if (txt.regionStart() > 1 && txt.regionEnd() + 1 < msg.length) {
if (txt.reg)
}*/
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()
}
} /*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)
*//*if (txt.regionStart() > 1 && txt.regionEnd() + 1 < msg.length) {
if (txt.reg)
}*//*
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()
}
}*/
}
}
}
}