diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 4f6a1e4..52ad220 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,17 +4,15 @@
-
-
-
-
+
+
+
-
-
-
-
+
+
+
@@ -101,9 +99,10 @@
@@ -125,15 +124,15 @@
-
-
-
-
+
+
-
+
-
+
+
+
@@ -186,7 +185,7 @@
-
+
@@ -198,12 +197,12 @@
-
+
-
+
@@ -240,33 +239,12 @@
false
-
+
-
-
-
-
-
-
-
-
-
-
-
- false
- true
- false
-
-
-
-
-
-
-
-
+
@@ -313,21 +291,28 @@
+
+
+
+
+
+
+
-
-
-
-
+
+
+
+
+
+
+
-
-
-
diff --git a/src/main/java/org/hmcore/MessageType.java b/src/main/java/org/hmcore/MessageType.java
new file mode 100644
index 0000000..4a1c444
--- /dev/null
+++ b/src/main/java/org/hmcore/MessageType.java
@@ -0,0 +1,15 @@
+package org.hmcore;
+
+public enum MessageType {
+
+ INVALID (-1),
+ BLOGPOST(0),
+ TWITTER(1),
+ JOB_LISTING(2),
+ WEBSITE_CHANGED(3);
+
+ MessageType(int i) {
+ }
+
+ int i;
+}
diff --git a/src/main/java/org/hmcore/TwitterJob.java b/src/main/java/org/hmcore/TwitterJob.java
index 421ec04..d35acef 100644
--- a/src/main/java/org/hmcore/TwitterJob.java
+++ b/src/main/java/org/hmcore/TwitterJob.java
@@ -30,7 +30,7 @@ public class TwitterJob implements Job {
if (!lastTweetID.equalsIgnoreCase(tweetID)) {
lastTweetID = tweetID;
- Channels.INSTANCE.sentToAll(new MessageBuilder().append("https://twitter.com/Hytale/status/").append(tweetID).build());
+ Channels.INSTANCE.sentToAll(new MessageBuilder().append("https://twitter.com/Hytale/status/").append(tweetID).build(), MessageType.TWITTER);
}
} catch (Exception e) {
diff --git a/src/main/kotlin/org/hmcore/Channels.kt b/src/main/kotlin/org/hmcore/Channels.kt
index cc77fc2..3b3b617 100644
--- a/src/main/kotlin/org/hmcore/Channels.kt
+++ b/src/main/kotlin/org/hmcore/Channels.kt
@@ -8,6 +8,7 @@ import kotlinx.serialization.json.Json
import net.dv8tion.jda.api.EmbedBuilder
import net.dv8tion.jda.api.Permission
import net.dv8tion.jda.api.entities.Message
+import org.hmcore.extensions.embed
import org.hmcore.extensions.toWebhook
import java.awt.Color
@@ -18,12 +19,22 @@ object Channels {
var channels: MutableList = refreshChannelsFromDisk()
var serviceChannels: MutableList = refreshServiceChannelsFromDisk()
- fun sentToAll(messageEmbed: Message) {
- messageEmbed.toWebhook().send(WEBHOOKS.blogPostsWebhookUrl)
+ fun sentToAll(messageEmbed: Message, msgType: MessageType) {
+ try {
+ messageEmbed.toWebhook().send(WEBHOOKS.blogPostsWebhookUrl)
+ } catch (e: Exception) {
+ e.printStackTrace()
+ Admin.sendDevMessage(embed {
+ title = "Error"
+ description = e.stackTraceToString()
+ color = Color.red
+ }, e.stackTrace.toString())
+ }
Main.jdas.forEach { jda ->
for (channel_pair in channels) {
try {
+ if(!channel_pair.type.equals(msgType)) continue
val channel = jda.getTextChannelById(channel_pair.id) ?: continue
val customMessage = channel_pair.message?.message ?: ""
@@ -126,7 +137,9 @@ object Channels {
else -> " @${channel.guild.getRoleById(it.mentionedRole ?: "")?.name}"
}
val publish = if (it.autoPublish) " (publish)" else ""
- "**${channel.guild.name}** #${channel.name}${role}${publish}${
+ val type = " " + it.type.toString()
+
+ "**${channel.guild.name}** #${channel.name}${role}${publish}${type}${
if (it.message == null) {
""
} else {
@@ -150,11 +163,11 @@ object Channels {
fun testServerId(id: Long) =
Main.jdas.map { it.getTextChannelById(id) }.firstOrNull()
- fun addChannel(id: Long, role: String?): DiscordChannel? {
- if (channels.find { it.id == id } != null) {
+ fun addChannel(id: Long, msgType: MessageType): DiscordChannel? {
+ if (channels.find { it.id == id && it.type == msgType } != null) {
return null
}
- val out = DiscordChannel(id, role)
+ val out = DiscordChannel(id, msgType)
channels.add(out)
saveChannels()
return out
diff --git a/src/main/kotlin/org/hmcore/DataIO.kt b/src/main/kotlin/org/hmcore/DataIO.kt
index 54d0005..5654706 100644
--- a/src/main/kotlin/org/hmcore/DataIO.kt
+++ b/src/main/kotlin/org/hmcore/DataIO.kt
@@ -9,12 +9,16 @@ import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import java.io.File
+const val channelVersion = 2.0
+
@Serializable
data class DiscordChannel(
val id: Long,
+ var type: MessageType,
var mentionedRole: String? = null,
var autoPublish: Boolean = false,
- var message: CustomMessage? = null
+ var message: CustomMessage? = null,
+ var version: Double? = channelVersion
)
@Serializable
diff --git a/src/main/kotlin/org/hmcore/Main.kt b/src/main/kotlin/org/hmcore/Main.kt
index 6e62986..9a1feae 100644
--- a/src/main/kotlin/org/hmcore/Main.kt
+++ b/src/main/kotlin/org/hmcore/Main.kt
@@ -1,5 +1,7 @@
package org.hmcore
+import kotlinx.serialization.encodeToString
+import kotlinx.serialization.json.Json
import org.hmcore.web.getNewBlogPosts
import net.dv8tion.jda.api.JDA
import net.dv8tion.jda.api.JDABuilder
@@ -9,12 +11,14 @@ import net.dv8tion.jda.api.requests.GatewayIntent
import net.dv8tion.jda.api.utils.ChunkingFilter
import net.dv8tion.jda.api.utils.MemberCachePolicy
import net.dv8tion.jda.api.utils.cache.CacheFlag
+import org.hmcore.web.getNewJobListings
import org.quartz.CronScheduleBuilder.cronSchedule
import org.quartz.JobBuilder.newJob
import org.quartz.JobDetail
import org.quartz.Trigger
import org.quartz.TriggerBuilder.newTrigger
import org.quartz.impl.StdSchedulerFactory
+import java.io.File
import javax.security.auth.login.LoginException
import kotlin.concurrent.timer
@@ -24,6 +28,27 @@ object Main {
@JvmStatic
fun main(args: Array) {
+
+ if(args.isEmpty()) startBot() else
+ when(args[0]) {
+ "serverDataConvert1" -> serverDataConvert1()
+ else -> startBot()
+ }
+
+ }
+
+ fun serverDataConvert1() {
+ var file = File("servers.json")
+ if(!file.exists()) return
+ var content = ""
+ file.bufferedReader().readLines().forEach {
+ content += it
+ .replace(",\"mentionedRole\":", ",\"type\":\"BLOGPOST\",\"mentionedRole\":")
+ }
+ file.writeBytes(content.encodeToByteArray())
+ }
+
+ fun startBot() {
val builder = JDABuilder.createLight(
Admin.token,
GatewayIntent.GUILD_MESSAGES,
@@ -57,9 +82,15 @@ object Main {
}
})
- timer("Updater", daemon = true, initialDelay = 0L, period = Admin.updateMs) {
+ timer("UpdaterBlogpost", daemon = true, initialDelay = 0L, period = Admin.updateMs) {
getNewBlogPosts()?.forEach {
- Channels.sentToAll(MessageBuilder().setEmbed(it.toMessageEmbed()).build())
+ Channels.sentToAll(MessageBuilder().setEmbed(it.toMessageEmbed()).build(), MessageType.BLOGPOST)
+ }
+ }
+
+ timer("UpdaterJob", daemon = true, initialDelay = 0L, period = Admin.updateMs) {
+ getNewJobListings()?.forEach {
+ Channels.sentToAll(MessageBuilder().setEmbed(it.toMessageEmbed()).build(), MessageType.JOB_LISTING)
}
}
@@ -77,9 +108,7 @@ object Main {
.withSchedule(cronSchedule("0 0/5 * 1/1 * ? *"))
.build()
- scheduler.scheduleJob(job, trigger);
-
-
+ scheduler.scheduleJob(job, trigger)
}
private fun configureMemoryUsage(builder: JDABuilder) {
diff --git a/src/main/kotlin/org/hmcore/OwnerCli.kt b/src/main/kotlin/org/hmcore/OwnerCli.kt
index b9793bd..b45b1a1 100644
--- a/src/main/kotlin/org/hmcore/OwnerCli.kt
+++ b/src/main/kotlin/org/hmcore/OwnerCli.kt
@@ -18,10 +18,38 @@ class OwnerCli : ListenerAdapter() {
val command = msg.removePrefix(prefix).split(Regex("\\s+"))
val channelId = event.message.channel.idLong
+ val msgType: MessageType
+
+ if(command.size < 2)
+ msgType = MessageType.INVALID
+ else msgType = when (command[1].lowercase()) {
+ "blogpost" -> MessageType.BLOGPOST
+ "twitter" -> MessageType.TWITTER
+ "job" -> MessageType.JOB_LISTING
+ "website" -> MessageType.WEBSITE_CHANGED
+ else -> MessageType.INVALID
+ }
when (command.first()) {
+ "categories" -> {
+ event.message.channel.sendMessage(EmbedBuilder()
+ .setTitle("Categories")
+ .setColor(Color.YELLOW)
+ .setAuthor(Admin.admin?.name, Admin.admin?.avatarUrl, Admin.admin?.avatarUrl)
+ .setDescription("Valid Categories:\n" +
+ " Blogpost\n" +
+ " Twitter\n" +
+ " Job - (changes of Job listings)\n" +
+ " Website - (if the content of some website or subdomain thats owned by hypixel studios gets changed) - soon\n")
+ .build()).queue()
+ return
+ }
"add" -> {
- val result = Channels.addChannel(channelId, null)
+ if (msgType == MessageType.INVALID) {
+ event.message.channel.sendMessage("Please choose a valid category. List valid categories with ${prefix}categories").queue()
+ return
+ }
+ val result = Channels.addChannel(channelId, msgType)
if (result == null) {
event.message.channel.sendMessage("Already added.").queue()
} else {
@@ -30,7 +58,10 @@ class OwnerCli : ListenerAdapter() {
}
}
"remove" -> {
- val result = Channels.channels.removeAll { it.id == channelId }
+ if (msgType == MessageType.INVALID) {
+ event.message.channel.sendMessage("Please choose a valid category. List valid categories with ${prefix}categories").queue()
+ }
+ val result = Channels.channels.removeAll { it.id == channelId && (it.type == msgType || it.type == MessageType.INVALID) }
Channels.saveChannels()
if (result) {
event.message.channel.sendMessage("Removed.").queue()
@@ -39,31 +70,37 @@ class OwnerCli : ListenerAdapter() {
}
}
"publish" -> {
- val result = Channels.channels.find { it.id == channelId }
+ if (msgType == MessageType.INVALID) {
+ event.message.channel.sendMessage("Please choose a valid category. List valid categories with ${prefix}categories").queue()
+ }
+ val result = Channels.channels.find { it.id == channelId && it.type == msgType}
if (result != null) {
- if (command.size > 1 && listOf("on", "off").contains(command[1])) {
- result.autoPublish = command[1] == "on"
+ if (command.size > 2 && listOf("on", "off").contains(command[2])) {
+ result.autoPublish = command[2] == "on"
Channels.saveChannels()
event.message.channel.sendMessage("Auto publish is now ${command[1]}").queue()
} else {
- event.message.channel.sendMessage("Usage: `${prefix}publish [on|off]`")
+ event.message.channel.sendMessage("Usage: `${prefix}publish [type] [on|off]`")
}
} else {
event.message.channel.sendMessage("Channel not registered.").queue()
}
}
"ping" -> {
- val result = Channels.channels.find { it.id == channelId }
+ if (msgType == MessageType.INVALID) {
+ event.message.channel.sendMessage("Please choose a valid category. List valid categories with ${prefix}categories").queue()
+ }
+ val result = Channels.channels.find { it.id == channelId && it.type == msgType}
if (result != null) {
- if (command.size > 1) {
- val roles = event.message.guild.getRolesByName(command[1], false)
+ if (command.size > 2) {
+ val roles = event.message.guild.getRolesByName(command[2], false)
result.mentionedRole = when {
- command[1] == "everyone" -> {
+ command[2] == "everyone" -> {
event.message.channel.sendMessage("Now pinging everyone.").queue()
"everyone"
}
- command[1] == "none" -> {
+ command[2] == "none" -> {
event.message.channel.sendMessage("Now pinging none.").queue()
null
}
@@ -78,29 +115,35 @@ class OwnerCli : ListenerAdapter() {
}
Channels.saveChannels()
} else {
- event.message.channel.sendMessage("Usage: `${prefix}ping [everyone|none|roleName]`")
+ event.message.channel.sendMessage("Usage: `${prefix}ping [type] [everyone|none|roleName]`")
}
} else {
event.message.channel.sendMessage("Channel is not registered.").queue()
}
}
"setMessage" -> {
- val result = Channels.channels.find { it.id == channelId }
+ if (msgType == MessageType.INVALID) {
+ event.message.channel.sendMessage("Please choose a valid category. List valid categories with ${prefix}categories").queue()
+ }
+ val result = Channels.channels.find { it.id == channelId && it.type == msgType }
if (result != null) {
- if (command.size > 1) {
- val message = event.message.contentRaw.removePrefix("${prefix}setMessage").trim()
+ if (command.size > 2) {
+ val message = command.subList(2, command.size).toString().trim()
result.message = CustomMessage(message)
Channels.saveChannels()
event.message.channel.sendMessage("Set `$message` as message.").queue()
} else {
- event.message.channel.sendMessage("Usage: `${prefix}setMessage [message]`")
+ event.message.channel.sendMessage("Usage: `${prefix}setMessage [type] [message]`")
}
} else {
event.message.channel.sendMessage("Channel is not registered.").queue()
}
}
"resetMessage" -> {
- val result = Channels.channels.find { it.id == channelId }
+ if (msgType == MessageType.INVALID) {
+ event.message.channel.sendMessage("Please choose a valid category. List valid categories with ${prefix}categories").queue()
+ }
+ val result = Channels.channels.find { it.id == channelId && it.type == msgType }
if (result != null) {
result.message = null
Channels.saveChannels()
@@ -132,16 +175,19 @@ class OwnerCli : ListenerAdapter() {
}
"publishMessage" -> {
- val result = Channels.channels.find { it.id == channelId }
+ if (msgType == MessageType.INVALID) {
+ event.message.channel.sendMessage("Please choose a valid category. List valid categories with ${prefix}categories").queue()
+ }
+ val result = Channels.channels.find { it.id == channelId && it.type == msgType }
if (result != null) {
if (result.message != null) {
- if (command.size > 1 && listOf("on", "off").contains(command[1])) {
- result.message?.pushAnnouncement = command[1] == "on"
+ if (command.size > 2 && listOf("on", "off").contains(command[2])) {
+ result.message?.pushAnnouncement = command[2] == "on"
Channels.saveChannels()
event.message.channel.sendMessage("Auto publish (message) is now ${command[1]}").queue()
} else {
- event.message.channel.sendMessage("Usage: `${prefix}publishMessage [on|off]`")
+ event.message.channel.sendMessage("Usage: `${prefix}publishMessage [type] [on|off]`")
}
} else {
event.message.channel.sendMessage("Channel has no custom message.").queue()
@@ -185,22 +231,24 @@ class OwnerCli : ListenerAdapter() {
.setAuthor(Admin.admin?.name, Admin.admin?.avatarUrl, Admin.admin?.avatarUrl)
.setDescription(
"""
- **${prefix}add**
+ **${prefix}add [type]**
Add this channel to the notified list
**${prefix}serviceChannel [add|remove]**
Add or remove this channel to receive service message from the bot developer (recommended)
- **${prefix}remove**
+ **${prefix}remove [type]**
Remove this channel to the notified list
- **${prefix}publish [on|off]**
+ **${prefix}publish [type] [on|off]**
[Community|Partner|Verified only] Auto publish the message if in an announcement channel
- **${prefix}ping [none|everyone|roleName]**
+ **${prefix}ping [type] [none|everyone|roleName]**
What role to ping
- **${prefix}setMessage [message]**
+ **${prefix}setMessage [type] [message]**
Set a custom message to show
- **${prefix}resetMessage**
+ **${prefix}resetMessag [type]e**
Reset the message
**${prefix}info**
Show an overview about all channels registered on this server
+ **${prefix}categories**
+ Show a list of categories available for alert types
**${prefix}report**
Report an issue to the Bot Admin (this will share your user name so they can contact you)
**${prefix}help**
diff --git a/src/test/kotlin/org/hmcore/serialization/EnumTest.kt b/src/test/kotlin/org/hmcore/serialization/EnumTest.kt
new file mode 100644
index 0000000..68f3d6a
--- /dev/null
+++ b/src/test/kotlin/org/hmcore/serialization/EnumTest.kt
@@ -0,0 +1,17 @@
+package org.hmcore.serialization
+
+import kotlinx.serialization.encodeToString
+import kotlinx.serialization.json.Json
+import org.hmcore.MessageType
+import org.junit.Test
+import kotlin.test.assertNotNull
+
+class EnumTest {
+
+ @Test
+ fun `Enum serialization`() {
+ println(Json.encodeToString(MessageType.BLOGPOST))
+ assertNotNull(MessageType.INVALID)
+ }
+
+}
\ No newline at end of file