Move stuff around

This commit is contained in:
Julius de Jeu 2018-09-15 00:28:17 +02:00
parent 5d423a06b7
commit 71b657f108
6 changed files with 146 additions and 103 deletions

View file

@ -1,6 +1,6 @@
plugins { plugins {
id 'java' id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.2.61' id 'org.jetbrains.kotlin.jvm' version '1.2.70'
id 'com.github.johnrengelman.shadow' version '2.0.4' id 'com.github.johnrengelman.shadow' version '2.0.4'
} }
@ -12,6 +12,8 @@ sourceCompatibility = 1.8
repositories { repositories {
jcenter() jcenter()
maven { url "https://dl.bintray.com/kotlin/ktor" }
} }
dependencies { dependencies {
@ -22,6 +24,7 @@ dependencies {
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.11.1' compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.11.1'
compile group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: '2.11.1' compile group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: '2.11.1'
compile 'com.github.salomonbrys.kotson:kotson:2.5.0' compile 'com.github.salomonbrys.kotson:kotson:2.5.0'
compile "com.sparkjava:spark-kotlin:1.0.0-alpha"
} }

View file

@ -4,27 +4,22 @@
"list": [ "list": [
{ {
"match": "fr0+d", "match": "fr0+d",
"time": 1536701874919, "time": 1536955561058,
"type": "REGEX" "type": "REGEX"
}, },
{ {
"match": "regex", "match": "regex",
"time": 1536702012766, "time": 1536955634806,
"type": "RAW" "type": "RAW"
}, },
{ {
"match": "(me)+", "match": "(me)+",
"time": 1536702032376, "time": 1536955642192,
"type": "REGEX" "type": "REGEX"
}, },
{ {
"match": "ye+t", "match": "ye+t",
"time": 1536702476328, "time": 1536955551512,
"type": "REGEX"
},
{
"match": "ye*t",
"time": 1536702619087,
"type": "REGEX" "type": "REGEX"
} }
] ]

View file

@ -10,3 +10,5 @@ enum class MatchType {
REGEX, REGEX,
RAW RAW
} }
fun MutableList<Match>.contains(match: String):Boolean = this.any { it.match == match }

View file

@ -0,0 +1,103 @@
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()
}
}
}
}
}
}

View file

@ -3,3 +3,6 @@ package nl.voidcorp.yeetbot
import com.google.gson.GsonBuilder import com.google.gson.GsonBuilder
val gson = GsonBuilder().setPrettyPrinting().create() val gson = GsonBuilder().setPrettyPrinting().create()

View file

@ -3,21 +3,15 @@ package nl.voidcorp.yeetbot
import com.github.salomonbrys.kotson.fromJson import com.github.salomonbrys.kotson.fromJson
import net.dv8tion.jda.core.* import net.dv8tion.jda.core.*
import net.dv8tion.jda.core.entities.Game import net.dv8tion.jda.core.entities.Game
import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent
import net.dv8tion.jda.core.hooks.ListenerAdapter
import net.dv8tion.jda.core.utils.PermissionUtil
import org.apache.logging.log4j.LogManager import org.apache.logging.log4j.LogManager
import java.io.File import java.io.File
import java.io.FileReader import java.io.FileReader
import java.io.FileWriter import java.io.FileWriter
import java.time.Duration
import java.time.Instant
import java.util.* import java.util.*
import java.util.regex.Pattern import java.util.regex.Pattern
import java.util.regex.PatternSyntaxException import java.util.regex.PatternSyntaxException
import kotlin.concurrent.fixedRateTimer import kotlin.concurrent.fixedRateTimer
import kotlin.concurrent.schedule import kotlin.concurrent.schedule
import kotlin.concurrent.timer
lateinit var config: Config lateinit var config: Config
@ -55,99 +49,42 @@ fun main(args: Array<String>) {
fixedRateTimer("StopTimer", period = 1000) { fixedRateTimer("StopTimer", period = 1000) {
if (File("new").exists()) { if (File("new").exists()) {
File("new").deleteOnExit() File("new").delete()
jda.presence.setPresence(OnlineStatus.DO_NOT_DISTURB, Game.watching("a timer")) var tick = false
Timer().schedule(1000 * 30) { val t = fixedRateTimer("ColorTick", period = 1000) {
Runtime.getRuntime().exec("sudo ") if (tick) {
} jda.presence.setPresence(OnlineStatus.DO_NOT_DISTURB, Game.watching("a restart timer!"))
}
}
}
class MessageListener : ListenerAdapter() {
override fun onGuildMessageReceived(event: GuildMessageReceivedEvent) {
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 ", "")
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 ", "")
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 { } else {
event.channel.sendMessage(MessageBuilder().append(event.member).append(", the regex `").append(m).append("` is invalid!").build()).queue() jda.presence.setPresence(OnlineStatus.IDLE, Game.watching("a restart timer!"))
} }
tick = !tick
} }
Timer().schedule(1000 * 30) {
t.cancel()
jda.presence.setPresence(OnlineStatus.DO_NOT_DISTURB, Game.watching("myself restart!"))
logger.info("Writing config!")
try {
with(FileWriter("conf.json")) {
gson.toJson(config, this)
close()
} }
} catch (e: Exception) {
} else if (!event.author.isBot) { logger.fatal("Could not write config!", e)
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()
}
} }
Timer().schedule(1000) {
Runtime.getRuntime().exec("sudo service jservice-yeetbot restart")
} }
} }
}
} }
} }
fun checkRegex(pattern: String): Boolean { fun checkRegex(pattern: String): Boolean {
var exc: PatternSyntaxException? = null var exc: PatternSyntaxException? = null
try { try {