Skip to content

Commit

Permalink
bot updates to go live
Browse files Browse the repository at this point in the history
  • Loading branch information
FireBall1725 committed May 15, 2024
1 parent 9038819 commit 6e41fc7
Show file tree
Hide file tree
Showing 17 changed files with 444 additions and 143 deletions.
3 changes: 2 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ dependencies {
implementation("com.fasterxml.jackson.module", "jackson-module-kotlin", "2.16.+")
implementation("com.fasterxml.jackson.dataformat", "jackson-dataformat-yaml", "2.16.+")

implementation("org.postgresql:postgresql:42.7.1")
implementation("org.postgresql:postgresql:42.7.3")
implementation("org.ktorm:ktorm-core:3.6.0")

testImplementation("org.jetbrains.kotlin:kotlin-test-junit5")
testImplementation(libs.junit.jupiter.engine)
Expand Down
93 changes: 44 additions & 49 deletions app/src/main/kotlin/ca/fireball1725/lcs/discordbot/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ package ca.fireball1725.lcs.discordbot
import ca.fireball1725.lcs.discordbot.data.Configuration
import ca.fireball1725.lcs.discordbot.data.config.BotConfig
import ca.fireball1725.lcs.discordbot.helpers.Database
import ca.fireball1725.lcs.discordbot.helpers.DatabaseNew
import ca.fireball1725.lcs.discordbot.mcserver.Pterodactyl
import ca.fireball1725.lcs.discordbot.mcserver.Server
import ca.fireball1725.lcs.discordbot.services.BotPermissions
import ca.fireball1725.lcs.discordbot.tasks.InviteProcessor
import ca.fireball1725.lcs.discordbot.tasks.JsonStatsProcessor
import ca.fireball1725.lcs.discordbot.tasks.MembersProcessor
import com.google.gson.Gson
import dev.kord.common.annotation.KordPreview
import dev.kord.core.entity.Guild
import dev.kord.core.entity.InviteWithMetadata
import dev.kord.core.event.guild.InviteCreateEvent
import dev.kord.gateway.Intents
import dev.kord.gateway.PrivilegedIntent
import dev.kord.x.emoji.Emojis
Expand All @@ -25,6 +28,7 @@ import kotlinx.coroutines.runBlocking
import me.jakejmattson.discordkt.dsl.CommandException
import me.jakejmattson.discordkt.dsl.ListenerException
import me.jakejmattson.discordkt.dsl.bot
import me.jakejmattson.discordkt.dsl.listeners
import me.jakejmattson.discordkt.locale.Language
import java.awt.Color
import java.io.Reader
Expand All @@ -39,9 +43,10 @@ private val botConfig: BotConfig = loadBotConfig(configPath)

private val pterodactyl: Pterodactyl = Pterodactyl(botConfig.pterodactylToken, botConfig.pterodactylUrl)

private val servers: MutableMap<String, Server> = mutableMapOf()

private val database: Database = Database()
private val databasenew = DatabaseNew().connect()

private var invites = mutableMapOf<String, InviteWithMetadata>()

private var guilds: List<Guild>? = null

Expand All @@ -51,36 +56,6 @@ suspend fun main(args: Array<String>) {
// Connect to the database
database.connect()

// todo: Load the servers from database
// todo: really need to do this...

// servers["b1107111"] =
// Server(
// "b1107111",
// "SMP Season 1",
// whitelistCameraEnabled = true,
// backupDownloadEnabled = true,
// )

servers["80966603"] =
Server(
"80966603",
"Creative Test Server",
)

servers["7f01a766"] =
Server(
"7f01a766",
"Vault Hunters",
)

// servers["e932250f"] =
// Server(
// "e932250f",
// "FTB Arcanum Institution",
// )

// println(App().greeting)
bot(botConfig.discordToken) {
val configuration = data("config/config.json") { Configuration() }

Expand Down Expand Up @@ -143,6 +118,9 @@ suspend fun main(args: Array<String>) {
onStart {
guilds = kord.guilds.toList()
println("Guilds: ${guilds!!.joinToString { it.name }}")

invites = guilds!![0].invites.toList().associateBy { it.code }.toMutableMap()
println("Invites: ${invites.size} invites found")
}

// Configure the locale for this bot.
Expand All @@ -153,14 +131,26 @@ suspend fun main(args: Array<String>) {
}

// Schedule processing json files
Timer().scheduleAtFixedRate(10000, 1000 * 60 * 15) { // 15 minutes
JsonStatsProcessor().processJsonStats()
if (botConfig.tasks.jsonProcessorEnabled) {
Timer().scheduleAtFixedRate(10000, 1000 * 60 * 15) { // 15 minutes
JsonStatsProcessor().processJsonStats()
}
}

// Schedule member updates
Timer().scheduleAtFixedRate(10000, 1000 * 60 * 15) {// 15 minutes
runBlocking {
MembersProcessor().updateMembersTable()
if (botConfig.tasks.memberProcessorEnabled) {
Timer().scheduleAtFixedRate(10000, 1000 * 60 * 15) { // 15 minutes
runBlocking {
MembersProcessor().updateMembersTable()
}
}
}

if (botConfig.tasks.inviteProcessorEnabled) {
Timer().scheduleAtFixedRate(10000, 1000 * 60 * 60) { // 1 hour
runBlocking {
InviteProcessor().checkForInviteCodes()
}
}
}
}
Expand All @@ -179,22 +169,27 @@ fun getPterodactyl(): Pterodactyl {
return pterodactyl
}

fun getServers(): MutableMap<String, Server> {
return servers
fun getDatabase(): Database {
return database
}

fun getGuilds(): List<Guild>? {
return guilds
}

fun getServer(serverId: String): Server? {
return if (serverId.contains(serverId)) {
servers[serverId]
} else {
null
fun inviteListener() = listeners {
on<InviteCreateEvent> {
updateInvites()
}
}

fun getDatabase(): Database {
return database
suspend fun updateInvites() {
invites = guilds!![0].invites.toList().associateBy { it.code }.toMutableMap()
}
fun getInvites(): Map<String, InviteWithMetadata> {
return invites
}

fun getGuilds(): List<Guild>? {
return guilds
fun getNewDatabase(): org.ktorm.database.Database {
return databasenew
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
package ca.fireball1725.lcs.discordbot.commands

import ca.fireball1725.lcs.discordbot.data.pterodactyl.GetWorldBackupDataAttributes
import ca.fireball1725.lcs.discordbot.getDatabase
import ca.fireball1725.lcs.discordbot.getPterodactyl
import ca.fireball1725.lcs.discordbot.getServer
import ca.fireball1725.lcs.discordbot.helpers.MathHelper
import ca.fireball1725.lcs.discordbot.helpers.ServerHelper
import java.time.LocalDateTime
Expand All @@ -17,35 +17,36 @@ import java.time.format.DateTimeFormatter

class DownloadBackup {
fun getWorldBackup(server: String): String? {
val serverId = ServerHelper().getServerIdFromPrettyName(server) ?: return null
val server = getServer(serverId)

val backups = getPterodactyl().getServerBackup(serverId)

var lastBackup: GetWorldBackupDataAttributes? = null
backups!!.worldBackupData.forEach {
lastBackup = it.worldBackupDataAttributes
if (it.worldBackupDataAttributes.isSuccessful != null && it.worldBackupDataAttributes.isSuccessful!!) {
return@forEach
}
}

if (lastBackup == null) {
return null
}

val downloadUrl = getPterodactyl().getDownloadBackup(serverId, lastBackup!!.uuid!!)

val pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX")
val timeStamp = LocalDateTime.parse(lastBackup!!.completedAt, pattern)
val unixTimeStamp = timeStamp.toEpochSecond(ZoneOffset.UTC)

var resultString = ""
resultString += "# ${server!!.getServerPrettyName()} World Backup\n"
resultString += "Backup completed at: <t:$unixTimeStamp:f>\n"
resultString += "Filesize: ${MathHelper().humanReadableFileSize(lastBackup!!.bytes!!)}\n"
resultString += "[Download](<$downloadUrl>)\n"
resultString += "_(download link is one time use and is valid for 15 minutes)_"
return resultString
// val serverId = ServerHelper().getServerIdFromPrettyName(server) ?: return null
// val server = getServer(serverId)
//
// val backups = getPterodactyl().getServerBackup(serverId)
//
// var lastBackup: GetWorldBackupDataAttributes? = null
// backups!!.worldBackupData.forEach {
// lastBackup = it.worldBackupDataAttributes
// if (it.worldBackupDataAttributes.isSuccessful != null && it.worldBackupDataAttributes.isSuccessful!!) {
// return@forEach
// }
// }
//
// if (lastBackup == null) {
// return null
// }
//
// val downloadUrl = getPterodactyl().getDownloadBackup(serverId, lastBackup!!.uuid!!)
//
// val pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX")
// val timeStamp = LocalDateTime.parse(lastBackup!!.completedAt, pattern)
// val unixTimeStamp = timeStamp.toEpochSecond(ZoneOffset.UTC)
//
// var resultString = ""
// resultString += "# ${server!!.getServerPrettyName()} World Backup\n"
// resultString += "Backup completed at: <t:$unixTimeStamp:f>\n"
// resultString += "Filesize: ${MathHelper().humanReadableFileSize(lastBackup!!.bytes!!)}\n"
// resultString += "[Download](<$downloadUrl>)\n"
// resultString += "_(download link is one time use and is valid for 15 minutes)_"
// return resultString
return ""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ data class BotConfig(
@SerializedName("backup_download")
val backupDownload: BotConfigConfigBackupDownload,
@SerializedName("database_configuration")
val databaseConfiguration: BotDatabaseConfiguration
val databaseConfiguration: BotDatabaseConfiguration,
@SerializedName("tasks")
val tasks: BotTaskConfiguration,
@SerializedName("invite_configuration")
val inviteConfiguration: BotInviteConfiguration
)

data class BotConfigConfigWhitelist(
Expand All @@ -47,3 +51,19 @@ data class BotDatabaseConfiguration(
@SerializedName("database_password")
val databasePassword: String,
)

data class BotTaskConfiguration(
@SerializedName("json_processor")
val jsonProcessorEnabled: Boolean,
@SerializedName("member_processor")
val memberProcessorEnabled: Boolean,
@SerializedName("invite_processor")
val inviteProcessorEnabled: Boolean,
)

data class BotInviteConfiguration(
@SerializedName("new_member_info_channel_id")
val newMemberInfoChannelId: Long,
@SerializedName("number_invites_on_hand")
val readyInviteCount: Int,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Created for the Lost Crafters SMP (https://www.lostcrafterssmp.com)
* Licensed under the GNU Affero General Public License v3.0
* See LICENSE.txt for full license information
*/

package ca.fireball1725.lcs.discordbot.data.databasenew

import org.ktorm.schema.Table
import org.ktorm.schema.boolean
import org.ktorm.schema.long
import org.ktorm.schema.uuid
import org.ktorm.schema.varchar

class MemberInvitesTable {
object MemberInvites : Table<Nothing>("member_invites") {
val inviteId = uuid("invite_uuid").primaryKey()
val inviteCode = varchar("invite_code")
val memberId = uuid("member_uuid")
val inviteUsed = boolean("invite_used")
val inviteUsedBy = long("invite_used_by")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Created for the Lost Crafters SMP (https://www.lostcrafterssmp.com)
* Licensed under the GNU Affero General Public License v3.0
* See LICENSE.txt for full license information
*/

package ca.fireball1725.lcs.discordbot.data.databasenew

import org.ktorm.schema.Table
import org.ktorm.schema.uuid
import org.ktorm.schema.varchar

class membersdb {
object members : Table<Nothing>("members") {
val member_id = uuid("member_id").primaryKey()
val display_name = varchar("display_username")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Created for the Lost Crafters SMP (https://www.lostcrafterssmp.com)
* Licensed under the GNU Affero General Public License v3.0
* See LICENSE.txt for full license information
*/

package ca.fireball1725.lcs.discordbot.data.invitebot

import com.google.gson.annotations.SerializedName

data class InviteBotMessage(
@SerializedName("member" ) var member : InviteBotMessageMember? = InviteBotMessageMember(),
@SerializedName("source_invite_code" ) var sourceInviteCode : String? = null,
@SerializedName("join_source_type" ) var joinSourceType : Int? = null,
@SerializedName("inviter_id" ) var inviterId : String? = null
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Created for the Lost Crafters SMP (https://www.lostcrafterssmp.com)
* Licensed under the GNU Affero General Public License v3.0
* See LICENSE.txt for full license information
*/

package ca.fireball1725.lcs.discordbot.data.invitebot

import com.google.gson.annotations.SerializedName

data class InviteBotMessageMember(
@SerializedName("avatar" ) var avatar : String? = null,
@SerializedName("communication_disabled_until" ) var communicationDisabledUntil : String? = null,
@SerializedName("flags" ) var flags : Int? = null,
@SerializedName("joined_at" ) var joinedAt : String? = null,
@SerializedName("nick" ) var nick : String? = null,
@SerializedName("pending" ) var pending : Boolean? = null,
@SerializedName("premium_since" ) var premiumSince : String? = null,
@SerializedName("roles" ) var roles : ArrayList<String> = arrayListOf(),
@SerializedName("unusual_dm_activity_until" ) var unusualDmActivityUntil : String? = null,
@SerializedName("user" ) var user : InviteBotMessageUser? = InviteBotMessageUser(),
@SerializedName("mute" ) var mute : Boolean? = null,
@SerializedName("deaf" ) var deaf : Boolean? = null
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Created for the Lost Crafters SMP (https://www.lostcrafterssmp.com)
* Licensed under the GNU Affero General Public License v3.0
* See LICENSE.txt for full license information
*/

package ca.fireball1725.lcs.discordbot.data.invitebot

import com.google.gson.annotations.SerializedName

data class InviteBotMessageUser(
@SerializedName("id" ) var id : String? = null,
@SerializedName("username" ) var username : String? = null,
@SerializedName("avatar" ) var avatar : String? = null,
@SerializedName("discriminator" ) var discriminator : String? = null,
@SerializedName("public_flags" ) var publicFlags : Int? = null,
@SerializedName("flags" ) var flags : Int? = null,
@SerializedName("bot" ) var bot : Boolean? = null,
@SerializedName("banner" ) var banner : String? = null,
@SerializedName("accent_color" ) var accentColor : String? = null,
@SerializedName("global_name" ) var globalName : String? = null,
@SerializedName("avatar_decoration_data" ) var avatarDecorationData : String? = null,
@SerializedName("banner_color" ) var bannerColor : String? = null,
@SerializedName("clan" ) var clan : String? = null
)
Loading

0 comments on commit 6e41fc7

Please sign in to comment.