Skip to content

Commit

Permalink
added multiple alert types:
Browse files Browse the repository at this point in the history
new aler types:
   - twitter
   - job
  • Loading branch information
UnrealValentin committed Jun 2, 2021
1 parent 126e630 commit cfad5c0
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 88 deletions.
81 changes: 33 additions & 48 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions src/main/java/org/hmcore/MessageType.java
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 1 addition & 1 deletion src/main/java/org/hmcore/TwitterJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void execute(JobExecutionContext context) {
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) {
Expand Down
25 changes: 19 additions & 6 deletions src/main/kotlin/org/hmcore/Channels.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -18,12 +19,22 @@ object Channels {
var channels: MutableList<DiscordChannel> = refreshChannelsFromDisk()
var serviceChannels: MutableList<ServiceChannel> = 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 ?: ""

Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/main/kotlin/org/hmcore/DataIO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 34 additions & 5 deletions src/main/kotlin/org/hmcore/Main.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand All @@ -24,6 +28,27 @@ object Main {

@JvmStatic
fun main(args: Array<String>) {

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,
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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) {
Expand Down
Loading

0 comments on commit cfad5c0

Please sign in to comment.