chore: progress commit. refactored TimePlanner, added stuff to Feature Manage Extension, added db stuff

Signed-off-by: limited_dev <loginakkisativ@gmail.com>
This commit is contained in:
moonleay 2023-06-05 12:10:25 +02:00 committed by limited_dev
parent 1cb043640a
commit b45d8ffd6e
7 changed files with 122 additions and 43 deletions

View file

@ -25,9 +25,10 @@ A Discord Bot for Splatoon Teams
- Features
- Time Planner -- Make the bot send messages and reactions into a selected channel in order to make planning easier
## Maybe upcoming features
## (Maybe) upcoming features
- Match Planner (Send Notifications some time before a match starts)
- Planning Notifier (Make it possible to ping people, who have time at a specific time)
- Game Tracker (Save the results of the last matches)
- Replay Saver (Maybe; will save the replay code to a database)

View file

@ -32,7 +32,7 @@ import net.moonleay.lilJudd.data.CredentialManager
import net.moonleay.lilJudd.data.DB
import net.moonleay.lilJudd.extensions.FeatureManageExtension
import net.moonleay.lilJudd.extensions.VersionExtension
import net.moonleay.lilJudd.features.TimePlanner
import net.moonleay.lilJudd.features.TimeManager
import net.moonleay.lilJudd.util.Logger
import net.moonleay.lilJudd.util.MessageUtil
import kotlin.system.exitProcess
@ -68,7 +68,7 @@ object Bot {
// Register the TimePlanner thread
val coroutineJob = GlobalScope.launch {
TimePlanner.registerThread()
TimeManager.registerThread()
}
// Add a shutdown hook to cancel the coroutine when the application is terminated

View file

@ -0,0 +1,26 @@
/*
* 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.data.entry
data class PlanningNotifierRolesData(
val serverID: String,
val channelId: String,
val hastimeroleid: String,
val wantstobenotifid: String
)

View file

@ -20,10 +20,9 @@ package net.moonleay.lilJudd.data.tables
import org.jetbrains.exposed.dao.id.IntIdTable
object PlanningNotifyerRoles : IntIdTable() {
object PlanningNotifierRoles : IntIdTable() {
var serverid = varchar("serverid", 50)
var channelid = varchar("channelid", 50)
var hastimeroleid = varchar("hastimeroleid", 50)
var hastovoteid = varchar("hastovoteid", 50)
var wantstobenotifid = varchar("wantstobenotifid", 50)
}

View file

@ -29,7 +29,7 @@ import dev.kord.common.Color
import dev.kord.common.entity.Permission
import dev.kord.common.entity.Snowflake
import dev.kord.core.behavior.createRole
import net.moonleay.lilJudd.data.tables.PlanningNotifyerRoles
import net.moonleay.lilJudd.data.tables.PlanningNotifierRoles
import net.moonleay.lilJudd.data.tables.TimePlanningChannels
import net.moonleay.lilJudd.extensions.component.EnableOrDisable
import net.moonleay.lilJudd.extensions.component.FeatureEnum
@ -45,7 +45,7 @@ class FeatureManageExtension : Extension() {
/*
* Note: This has to be rewritten at some point to better support more features
* and remove this mess of a class.
* and improve this mess of a class.
* */
override suspend fun setup() {
publicSlashCommand(::FeatureManagerArgs) {
@ -60,7 +60,7 @@ class FeatureManageExtension : Extension() {
MessageUtil.getEmbed(
Color(0xE0311A),
"403: Forbidden",
"You cannot edit features, as you don't have the Administratior permission.",
"You cannot edit features, as you don't have the Administrator permission.",
u.asUser().username + "#" + u.asUser().discriminator
)
)
@ -116,37 +116,31 @@ class FeatureManageExtension : Extension() {
FeatureEnum.PLANNINGNOTIFIER -> {
var alreadyExists = false
transaction {
alreadyExists = PlanningNotifyerRoles.select {
(PlanningNotifyerRoles.serverid eq gID) and (PlanningNotifyerRoles.channelid eq cID)
alreadyExists = PlanningNotifierRoles.select {
(PlanningNotifierRoles.serverid eq gID) and (PlanningNotifierRoles.channelid eq cID)
}.count() > 0
}
if (!alreadyExists) {
val hasToVoteRole = this.guild!!.createRole {
this.name = "Hasn't voted [${channel.data.name.value}]"
this.mentionable = true
}
val htvr = hasToVoteRole.id.toString()
val hasTimeRole = this.guild!!.createRole {
this.name = "Has time [${channel.data.name.value}]"
this.name = "available [${channel.data.name.value}]"
this.mentionable = true
}
val htr = hasTimeRole.id.toString()
val wantsNotifsRole = this.guild!!.createRole {
this.name = "Wants to be notified [${channel.data.name.value}]"
this.name = "notifications [${channel.data.name.value}]"
this.mentionable = true
}
val wnr = wantsNotifsRole.id.toString()
transaction {
PlanningNotifyerRoles.insert {
it[PlanningNotifyerRoles.serverid] = gID
it[PlanningNotifyerRoles.channelid] = cID
it[PlanningNotifyerRoles.hastovoteid] = htvr
it[PlanningNotifyerRoles.hastimeroleid] = htr
it[PlanningNotifyerRoles.wantstobenotifid] = wnr
} get PlanningNotifyerRoles.id
PlanningNotifierRoles.insert {
it[PlanningNotifierRoles.serverid] = gID
it[PlanningNotifierRoles.channelid] = cID
it[PlanningNotifierRoles.hastimeroleid] = htr
it[PlanningNotifierRoles.wantstobenotifid] = wnr
} get PlanningNotifierRoles.id
}
this.respond {
@ -154,7 +148,7 @@ class FeatureManageExtension : Extension() {
MessageUtil.getEmbed(
Color(0x52E01A),
"200: Success",
"The feature was enabled in channel ${args.channel.data.name.value} with roles ${hasToVoteRole.mention}, ${hasTimeRole.mention} & ${wantsNotifsRole.mention}.",
"The feature was enabled in channel ${args.channel.data.name.value} with roles ${hasTimeRole.mention} & ${wantsNotifsRole.mention}.",
u.asUser().username + "#" + u.asUser().discriminator
)
)
@ -223,28 +217,27 @@ class FeatureManageExtension : Extension() {
FeatureEnum.PLANNINGNOTIFIER -> {
var alreadyExists = false
transaction {
alreadyExists = PlanningNotifyerRoles.select {
(PlanningNotifyerRoles.serverid eq gID) and (PlanningNotifyerRoles.channelid eq cID)
alreadyExists = PlanningNotifierRoles.select {
(PlanningNotifierRoles.serverid eq gID) and (PlanningNotifierRoles.channelid eq cID)
}.count() > 0
}
if (alreadyExists) {
var matchingEntries: List<ResultRow> = mutableListOf()
transaction {
matchingEntries = PlanningNotifyerRoles.select {
(PlanningNotifyerRoles.serverid eq gID) and
(PlanningNotifyerRoles.channelid eq cID)
matchingEntries = PlanningNotifierRoles.select {
(PlanningNotifierRoles.serverid eq gID) and
(PlanningNotifierRoles.channelid eq cID)
}.toList()
}
for (e in matchingEntries) {
this.guild!!.getRoleOrNull(Snowflake(e[PlanningNotifyerRoles.hastovoteid]))?.delete()
this.guild!!.getRoleOrNull(Snowflake(e[PlanningNotifyerRoles.hastimeroleid]))?.delete()
this.guild!!.getRoleOrNull(Snowflake(e[PlanningNotifyerRoles.wantstobenotifid]))
this.guild!!.getRoleOrNull(Snowflake(e[PlanningNotifierRoles.hastimeroleid]))?.delete()
this.guild!!.getRoleOrNull(Snowflake(e[PlanningNotifierRoles.wantstobenotifid]))
?.delete()
}
transaction {
matchingEntries.forEach { entry ->
PlanningNotifyerRoles.deleteWhere { id eq entry[id] }
PlanningNotifierRoles.deleteWhere { id eq entry[id] }
}
}
this.respond {

View file

@ -0,0 +1,36 @@
/*
* 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.features
import dev.inmo.krontab.buildSchedule
import dev.inmo.krontab.doInfinity
import net.moonleay.lilJudd.util.Logger
object PlanningNotifier {
suspend fun registerThread() {
Logger.out("Adding ping scheduler...")
val scheduler = buildSchedule("0 00 23 * * * 0o *") // 0 0 4 * * * 0o 1w // 0o is UTC
scheduler.doInfinity {
Logger.out("Starting to update roles...")
Logger.out("Done! Until tomorrow! <3 ")
}
}
}

View file

@ -27,6 +27,8 @@ import dev.kord.core.entity.channel.MessageChannel
import dev.kord.rest.builder.message.create.actionRow
import kotlinx.coroutines.delay
import net.moonleay.lilJudd.Bot
import net.moonleay.lilJudd.data.entry.PlanningNotifierRolesData
import net.moonleay.lilJudd.data.tables.PlanningNotifierRoles
import net.moonleay.lilJudd.data.tables.TimePlanningChannels
import net.moonleay.lilJudd.util.ButtonUtil
import net.moonleay.lilJudd.util.Logger
@ -37,7 +39,7 @@ import java.time.ZoneId
import java.time.ZonedDateTime
object TimePlanner {
object TimeManager {
/* /--------------- Seconds
| /------------- Minutes
| | /----------- Hours
@ -49,23 +51,43 @@ object TimePlanner {
* * * * * * 0o *w*/
suspend fun registerThread() {
Logger.out("Adding scheduler...")
val scheduler = buildSchedule("0 0 4 * * * 0o 1w") // 0 0 4 * * * 0o 1w // 0o is UTC
Logger.out("Adding message scheduler...")
val scheduler = buildSchedule("0 00 23 * * * 0o *") // 0 0 4 * * * 0o 1w // 0o is UTC
scheduler.doInfinity {
Logger.out("Starting to notify...")
val channelList = mutableListOf<Snowflake>()
// ChannelID, ServerID
val channelList = mutableMapOf<Snowflake, Snowflake>()
// ChannelID, Data
val roleList = mutableMapOf<Snowflake, PlanningNotifierRolesData>()
transaction {
for (tp in TimePlanningChannels.selectAll()) {
channelList.add(Snowflake(tp[TimePlanningChannels.channelid]))
channelList[Snowflake(tp[TimePlanningChannels.channelid])] =
Snowflake(tp[TimePlanningChannels.serverid])
Logger.out("Have to notify channel with ID ${tp[TimePlanningChannels.channelid]}.")
}
for (pnr in PlanningNotifierRoles.selectAll()) {
roleList[Snowflake(pnr[PlanningNotifierRoles.channelid])] = PlanningNotifierRolesData(
pnr[PlanningNotifierRoles.serverid],
pnr[PlanningNotifierRoles.channelid],
pnr[PlanningNotifierRoles.hastimeroleid],
pnr[PlanningNotifierRoles.wantstobenotifid]
)
Logger.out("Have to ping roles: ${pnr[PlanningNotifierRoles.wantstobenotifid]}}")
}
}
Logger.out("${channelList.count()} Channels to notify!")
for (ch in channelList) {
Logger.out("${channelList.count()} Channels to notify with ${roleList.count()} Roles to ping!")
for (ch in channelList.keys) {
if (Bot.bot.kordRef.getChannel(ch) == null)
continue // TODO: Check if the channel is valid in another shard
val c = Bot.bot.kordRef.getChannelOf<MessageChannel>(ch)!!
c.createMessage {
if (roleList[ch] != null) {
this.content = "<@&${Snowflake(roleList[ch]?.wantstobenotifid!!)}>"
}
this.embeds.add(
MessageUtil.getEmbed(
Color(0X4C4645),
@ -75,7 +97,7 @@ object TimePlanner {
)
)
}
delay(3000)
delay(2000)
var then = ZonedDateTime.now(ZoneId.of("Europe/Berlin")).withHour(4).withMinute(0).withSecond(0)
repeat(7) {
c.createMessage {
@ -96,8 +118,10 @@ object TimePlanner {
this.components.addAll(ButtonUtil.getTimePlannerButtons().components)
}
}
then = then.plusDays(1).withHour(20).withMinute(24).withSecond(0)
then = then.plusDays(1).withHour(4).withMinute(0).withSecond(0)
Logger.out("Finished sending day $it")
delay(1000)
}
Logger.out("Finished with ${c.data.guildId.value}")