feat: added MatchJob to clean up roles

Signed-off-by: limited_dev <loginakkisativ@gmail.com>
This commit is contained in:
limited_dev 2023-07-12 13:26:27 +02:00
parent 9099d02bf4
commit 9fa7f38228

View file

@ -0,0 +1,90 @@
/*
* lilJudd
* Copyright (C) 2023 moonleay
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.moonleay.lilJudd.jobs
import dev.inmo.krontab.KronScheduler
import dev.kord.common.entity.Snowflake
import kotlinx.coroutines.Job
import net.moonleay.lilJudd.Bot
import net.moonleay.lilJudd.data.entry.MatchPlanningDataData
import net.moonleay.lilJudd.data.tables.MatchPlanningData
import net.moonleay.lilJudd.jobs.component.CronjobType
import net.moonleay.lilJudd.jobs.component.ICronjob
import net.moonleay.lilJudd.jobs.component.JobManager
import net.moonleay.lilJudd.util.Logger
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.deleteWhere
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.transactions.transaction
class MatchJob(
override val jobIncoming: String,
private val tableId: Int,
override val jobName: String,
) : ICronjob {
override val jobType: CronjobType = CronjobType.ONCE
override val continueJob: Boolean = true
override lateinit var cronjobJob: Job
override lateinit var scheduler: KronScheduler
/*
* This job should delete the role, which was created for the match.
* */
override suspend fun jobFunction() {
Logger.out("Running MatchJob \"${this.jobName}\"")
lateinit var mpdd: MatchPlanningDataData
transaction {
for (pnr in MatchPlanningData.select {
MatchPlanningData.id eq (tableId)
}) {
mpdd = MatchPlanningDataData(
pnr[MatchPlanningData.id].value,
pnr[MatchPlanningData.serverid],
pnr[MatchPlanningData.channelid],
pnr[MatchPlanningData.matchtype],
pnr[MatchPlanningData.registererid],
pnr[MatchPlanningData.roleid],
pnr[MatchPlanningData.opponentName],
pnr[MatchPlanningData.messageid],
pnr[MatchPlanningData.timestamp],
pnr[MatchPlanningData.jobstr]
)
}
}
val guild = Bot.bot.kordRef.getGuildOrNull(Snowflake(mpdd.serverid))
if (guild == null) {
Logger.out("Guild not found.")
return
}
val r = guild.getRoleOrNull(Snowflake(mpdd.roleid))
if (r == null) {
Logger.out("Role not found.")
return
}
r.delete()
transaction {
MatchPlanningData.deleteWhere {
MatchPlanningData.id eq (tableId)
}
}
Logger.out("MatchJob \"${this.jobName}\" finished.")
Logger.out("Killing job \"${this.jobName}\"..")
JobManager.killJob(this)
}
}