2018-09-15 00:28:17 +02:00
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
2018-09-18 11:42:57 +02:00
import java.io.FileWriter
2018-09-15 00:28:17 +02:00
import java.time.Duration
import java.time.Instant
2018-09-18 11:42:57 +02:00
import java.util.regex.Pattern
2018-09-15 00:28:17 +02:00
class MessageListener : ListenerAdapter ( ) {
2018-09-18 11:42:57 +02:00
val escapeEx = " [[ \\ p{Punct}&&[^:]]| \\ s|#]* "
2018-09-15 00:28:17 +02:00
override fun onGuildMessageReceived ( event : GuildMessageReceivedEvent ) {
val msg = event . message . contentRaw
val guild = event . guild
2018-09-18 11:42:57 +02:00
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 ( " % " ) ) {
2018-09-15 00:28:17 +02:00
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 ( )
}
}
2018-09-18 11:42:57 +02:00
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 ( )
}
2018-09-15 00:28:17 +02:00
}
} else if ( ! event . author . isBot ) {
val map = config . guildWords . getOrPut ( guild . idLong ) { GuildInfo ( ) } . list
for ( match in map ) {
2018-09-18 11:42:57 +02:00
/*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
} * /
2018-09-15 00:28:17 +02:00
if ( match . time <= System . currentTimeMillis ( ) ) {
val past = Instant . ofEpochMilli ( match . time )
val now = Instant . ofEpochMilli ( System . currentTimeMillis ( ) )
val time = Duration . between ( past , now )
2018-09-18 11:42:57 +02:00
val txt = match . match . toRegex ( ) . toPattern ( ) . matcher ( msg )
/ * if ( txt . regionStart ( ) > 1 && txt . regionEnd ( ) + 1 < msg . length ) {
if ( txt . reg )
} * /
2018-09-15 00:28:17 +02:00
match . time = now . toEpochMilli ( ) + 1000 * 60 * 30
event . channel . sendMessage ( MessageBuilder ( ) . append ( event . member )
2018-09-18 11:42:57 +02:00
. append ( " said the forbidden word ` " ) . append ( msg . substring ( txt . regionStart ( ) , txt . regionEnd ( ) ) )
2018-09-15 00:28:17 +02:00
. append ( " `! \n I have reset the counter for this word and will wait 30 minutes so you can spam it as much as you want. \n You 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 ( )
}
2018-09-18 11:42:57 +02:00
} / * else if ( match . type == MatchType . REGEX && msg . contains ( match . match . toRegex ( ) ) ) {
2018-09-15 00:28:17 +02:00
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 )
2018-09-18 11:42:57 +02:00
* //*if (txt.regionStart() > 1 && txt.regionEnd() + 1 < msg.length) {
if ( txt . reg )
} * //*
2018-09-15 00:28:17 +02:00
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 ( " `! \n I have reset the counter for this word and will wait 30 minutes so you can spam it as much as you want. \n You 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 ( )
}
2018-09-18 11:42:57 +02:00
} * /
2018-09-15 00:28:17 +02:00
}
}
}
}