Move stuff around

master
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 {
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'
}
@ -12,6 +12,8 @@ sourceCompatibility = 1.8
repositories {
jcenter()
maven { url "https://dl.bintray.com/kotlin/ktor" }
}
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-slf4j-impl', version: '2.11.1'
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": [
{
"match": "fr0+d",
"time": 1536701874919,
"time": 1536955561058,
"type": "REGEX"
},
{
"match": "regex",
"time": 1536702012766,
"time": 1536955634806,
"type": "RAW"
},
{
"match": "(me)+",
"time": 1536702032376,
"time": 1536955642192,
"type": "REGEX"
},
{
"match": "ye+t",
"time": 1536702476328,
"type": "REGEX"
},
{
"match": "ye*t",
"time": 1536702619087,
"time": 1536955551512,
"type": "REGEX"
}
]

View File

@ -9,4 +9,6 @@ data class Match(val match: String, var time: Long = System.currentTimeMillis(),
enum class MatchType {
REGEX,
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

@ -2,4 +2,7 @@ package nl.voidcorp.yeetbot
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 net.dv8tion.jda.core.*
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 java.io.File
import java.io.FileReader
import java.io.FileWriter
import java.time.Duration
import java.time.Instant
import java.util.*
import java.util.regex.Pattern
import java.util.regex.PatternSyntaxException
import kotlin.concurrent.fixedRateTimer
import kotlin.concurrent.schedule
import kotlin.concurrent.timer
lateinit var config: Config
@ -55,10 +49,35 @@ fun main(args: Array<String>) {
fixedRateTimer("StopTimer", period = 1000) {
if (File("new").exists()) {
File("new").deleteOnExit()
jda.presence.setPresence(OnlineStatus.DO_NOT_DISTURB, Game.watching("a timer"))
File("new").delete()
var tick = false
val t = fixedRateTimer("ColorTick", period = 1000) {
if (tick) {
jda.presence.setPresence(OnlineStatus.DO_NOT_DISTURB, Game.watching("a restart timer!"))
} else {
jda.presence.setPresence(OnlineStatus.IDLE, Game.watching("a restart timer!"))
}
tick = !tick
}
Timer().schedule(1000 * 30) {
Runtime.getRuntime().exec("sudo ")
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) {
logger.fatal("Could not write config!", e)
}
Timer().schedule(1000) {
Runtime.getRuntime().exec("sudo service jservice-yeetbot restart")
}
}
}
@ -66,88 +85,6 @@ fun main(args: Array<String>) {
}
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 {
event.channel.sendMessage(MessageBuilder().append(event.member).append(", the regex `").append(m).append("` is invalid!").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()
}
}
}
}
}
}
fun checkRegex(pattern: String): Boolean {
var exc: PatternSyntaxException? = null
try {