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:
parent
1cb043640a
commit
b45d8ffd6e
7 changed files with 122 additions and 43 deletions
|
@ -25,9 +25,10 @@ A Discord Bot for Splatoon Teams
|
||||||
- Features
|
- Features
|
||||||
- Time Planner -- Make the bot send messages and reactions into a selected channel in order to make planning easier
|
- 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)
|
- 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)
|
- Game Tracker (Save the results of the last matches)
|
||||||
- Replay Saver (Maybe; will save the replay code to a database)
|
- Replay Saver (Maybe; will save the replay code to a database)
|
||||||
|
|
||||||
|
|
|
@ -32,7 +32,7 @@ import net.moonleay.lilJudd.data.CredentialManager
|
||||||
import net.moonleay.lilJudd.data.DB
|
import net.moonleay.lilJudd.data.DB
|
||||||
import net.moonleay.lilJudd.extensions.FeatureManageExtension
|
import net.moonleay.lilJudd.extensions.FeatureManageExtension
|
||||||
import net.moonleay.lilJudd.extensions.VersionExtension
|
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.Logger
|
||||||
import net.moonleay.lilJudd.util.MessageUtil
|
import net.moonleay.lilJudd.util.MessageUtil
|
||||||
import kotlin.system.exitProcess
|
import kotlin.system.exitProcess
|
||||||
|
@ -68,7 +68,7 @@ object Bot {
|
||||||
|
|
||||||
// Register the TimePlanner thread
|
// Register the TimePlanner thread
|
||||||
val coroutineJob = GlobalScope.launch {
|
val coroutineJob = GlobalScope.launch {
|
||||||
TimePlanner.registerThread()
|
TimeManager.registerThread()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add a shutdown hook to cancel the coroutine when the application is terminated
|
// Add a shutdown hook to cancel the coroutine when the application is terminated
|
||||||
|
|
|
@ -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
|
||||||
|
)
|
|
@ -20,10 +20,9 @@ package net.moonleay.lilJudd.data.tables
|
||||||
|
|
||||||
import org.jetbrains.exposed.dao.id.IntIdTable
|
import org.jetbrains.exposed.dao.id.IntIdTable
|
||||||
|
|
||||||
object PlanningNotifyerRoles : IntIdTable() {
|
object PlanningNotifierRoles : IntIdTable() {
|
||||||
var serverid = varchar("serverid", 50)
|
var serverid = varchar("serverid", 50)
|
||||||
var channelid = varchar("channelid", 50)
|
var channelid = varchar("channelid", 50)
|
||||||
var hastimeroleid = varchar("hastimeroleid", 50)
|
var hastimeroleid = varchar("hastimeroleid", 50)
|
||||||
var hastovoteid = varchar("hastovoteid", 50)
|
|
||||||
var wantstobenotifid = varchar("wantstobenotifid", 50)
|
var wantstobenotifid = varchar("wantstobenotifid", 50)
|
||||||
}
|
}
|
|
@ -29,7 +29,7 @@ import dev.kord.common.Color
|
||||||
import dev.kord.common.entity.Permission
|
import dev.kord.common.entity.Permission
|
||||||
import dev.kord.common.entity.Snowflake
|
import dev.kord.common.entity.Snowflake
|
||||||
import dev.kord.core.behavior.createRole
|
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.data.tables.TimePlanningChannels
|
||||||
import net.moonleay.lilJudd.extensions.component.EnableOrDisable
|
import net.moonleay.lilJudd.extensions.component.EnableOrDisable
|
||||||
import net.moonleay.lilJudd.extensions.component.FeatureEnum
|
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
|
* 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() {
|
override suspend fun setup() {
|
||||||
publicSlashCommand(::FeatureManagerArgs) {
|
publicSlashCommand(::FeatureManagerArgs) {
|
||||||
|
@ -60,7 +60,7 @@ class FeatureManageExtension : Extension() {
|
||||||
MessageUtil.getEmbed(
|
MessageUtil.getEmbed(
|
||||||
Color(0xE0311A),
|
Color(0xE0311A),
|
||||||
"403: Forbidden",
|
"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
|
u.asUser().username + "#" + u.asUser().discriminator
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
@ -116,37 +116,31 @@ class FeatureManageExtension : Extension() {
|
||||||
FeatureEnum.PLANNINGNOTIFIER -> {
|
FeatureEnum.PLANNINGNOTIFIER -> {
|
||||||
var alreadyExists = false
|
var alreadyExists = false
|
||||||
transaction {
|
transaction {
|
||||||
alreadyExists = PlanningNotifyerRoles.select {
|
alreadyExists = PlanningNotifierRoles.select {
|
||||||
(PlanningNotifyerRoles.serverid eq gID) and (PlanningNotifyerRoles.channelid eq cID)
|
(PlanningNotifierRoles.serverid eq gID) and (PlanningNotifierRoles.channelid eq cID)
|
||||||
}.count() > 0
|
}.count() > 0
|
||||||
}
|
}
|
||||||
if (!alreadyExists) {
|
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 {
|
val hasTimeRole = this.guild!!.createRole {
|
||||||
this.name = "Has time [${channel.data.name.value}]"
|
this.name = "available [${channel.data.name.value}]"
|
||||||
this.mentionable = true
|
this.mentionable = true
|
||||||
}
|
}
|
||||||
val htr = hasTimeRole.id.toString()
|
val htr = hasTimeRole.id.toString()
|
||||||
|
|
||||||
val wantsNotifsRole = this.guild!!.createRole {
|
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
|
this.mentionable = true
|
||||||
}
|
}
|
||||||
val wnr = wantsNotifsRole.id.toString()
|
val wnr = wantsNotifsRole.id.toString()
|
||||||
|
|
||||||
transaction {
|
transaction {
|
||||||
PlanningNotifyerRoles.insert {
|
PlanningNotifierRoles.insert {
|
||||||
it[PlanningNotifyerRoles.serverid] = gID
|
it[PlanningNotifierRoles.serverid] = gID
|
||||||
it[PlanningNotifyerRoles.channelid] = cID
|
it[PlanningNotifierRoles.channelid] = cID
|
||||||
it[PlanningNotifyerRoles.hastovoteid] = htvr
|
it[PlanningNotifierRoles.hastimeroleid] = htr
|
||||||
it[PlanningNotifyerRoles.hastimeroleid] = htr
|
it[PlanningNotifierRoles.wantstobenotifid] = wnr
|
||||||
it[PlanningNotifyerRoles.wantstobenotifid] = wnr
|
} get PlanningNotifierRoles.id
|
||||||
} get PlanningNotifyerRoles.id
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.respond {
|
this.respond {
|
||||||
|
@ -154,7 +148,7 @@ class FeatureManageExtension : Extension() {
|
||||||
MessageUtil.getEmbed(
|
MessageUtil.getEmbed(
|
||||||
Color(0x52E01A),
|
Color(0x52E01A),
|
||||||
"200: Success",
|
"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
|
u.asUser().username + "#" + u.asUser().discriminator
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
@ -223,28 +217,27 @@ class FeatureManageExtension : Extension() {
|
||||||
FeatureEnum.PLANNINGNOTIFIER -> {
|
FeatureEnum.PLANNINGNOTIFIER -> {
|
||||||
var alreadyExists = false
|
var alreadyExists = false
|
||||||
transaction {
|
transaction {
|
||||||
alreadyExists = PlanningNotifyerRoles.select {
|
alreadyExists = PlanningNotifierRoles.select {
|
||||||
(PlanningNotifyerRoles.serverid eq gID) and (PlanningNotifyerRoles.channelid eq cID)
|
(PlanningNotifierRoles.serverid eq gID) and (PlanningNotifierRoles.channelid eq cID)
|
||||||
}.count() > 0
|
}.count() > 0
|
||||||
}
|
}
|
||||||
if (alreadyExists) {
|
if (alreadyExists) {
|
||||||
var matchingEntries: List<ResultRow> = mutableListOf()
|
var matchingEntries: List<ResultRow> = mutableListOf()
|
||||||
transaction {
|
transaction {
|
||||||
matchingEntries = PlanningNotifyerRoles.select {
|
matchingEntries = PlanningNotifierRoles.select {
|
||||||
(PlanningNotifyerRoles.serverid eq gID) and
|
(PlanningNotifierRoles.serverid eq gID) and
|
||||||
(PlanningNotifyerRoles.channelid eq cID)
|
(PlanningNotifierRoles.channelid eq cID)
|
||||||
}.toList()
|
}.toList()
|
||||||
}
|
}
|
||||||
for (e in matchingEntries) {
|
for (e in matchingEntries) {
|
||||||
this.guild!!.getRoleOrNull(Snowflake(e[PlanningNotifyerRoles.hastovoteid]))?.delete()
|
this.guild!!.getRoleOrNull(Snowflake(e[PlanningNotifierRoles.hastimeroleid]))?.delete()
|
||||||
this.guild!!.getRoleOrNull(Snowflake(e[PlanningNotifyerRoles.hastimeroleid]))?.delete()
|
this.guild!!.getRoleOrNull(Snowflake(e[PlanningNotifierRoles.wantstobenotifid]))
|
||||||
this.guild!!.getRoleOrNull(Snowflake(e[PlanningNotifyerRoles.wantstobenotifid]))
|
|
||||||
?.delete()
|
?.delete()
|
||||||
}
|
}
|
||||||
|
|
||||||
transaction {
|
transaction {
|
||||||
matchingEntries.forEach { entry ->
|
matchingEntries.forEach { entry ->
|
||||||
PlanningNotifyerRoles.deleteWhere { id eq entry[id] }
|
PlanningNotifierRoles.deleteWhere { id eq entry[id] }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.respond {
|
this.respond {
|
||||||
|
|
|
@ -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 ")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -27,6 +27,8 @@ import dev.kord.core.entity.channel.MessageChannel
|
||||||
import dev.kord.rest.builder.message.create.actionRow
|
import dev.kord.rest.builder.message.create.actionRow
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import net.moonleay.lilJudd.Bot
|
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.data.tables.TimePlanningChannels
|
||||||
import net.moonleay.lilJudd.util.ButtonUtil
|
import net.moonleay.lilJudd.util.ButtonUtil
|
||||||
import net.moonleay.lilJudd.util.Logger
|
import net.moonleay.lilJudd.util.Logger
|
||||||
|
@ -37,7 +39,7 @@ import java.time.ZoneId
|
||||||
import java.time.ZonedDateTime
|
import java.time.ZonedDateTime
|
||||||
|
|
||||||
|
|
||||||
object TimePlanner {
|
object TimeManager {
|
||||||
/* /--------------- Seconds
|
/* /--------------- Seconds
|
||||||
| /------------- Minutes
|
| /------------- Minutes
|
||||||
| | /----------- Hours
|
| | /----------- Hours
|
||||||
|
@ -49,23 +51,43 @@ object TimePlanner {
|
||||||
* * * * * * 0o *w*/
|
* * * * * * 0o *w*/
|
||||||
|
|
||||||
suspend fun registerThread() {
|
suspend fun registerThread() {
|
||||||
Logger.out("Adding scheduler...")
|
Logger.out("Adding message scheduler...")
|
||||||
val scheduler = buildSchedule("0 0 4 * * * 0o 1w") // 0 0 4 * * * 0o 1w // 0o is UTC
|
val scheduler = buildSchedule("0 00 23 * * * 0o *") // 0 0 4 * * * 0o 1w // 0o is UTC
|
||||||
scheduler.doInfinity {
|
scheduler.doInfinity {
|
||||||
Logger.out("Starting to notify...")
|
Logger.out("Starting to notify...")
|
||||||
val channelList = mutableListOf<Snowflake>()
|
|
||||||
|
|
||||||
|
// ChannelID, ServerID
|
||||||
|
val channelList = mutableMapOf<Snowflake, Snowflake>()
|
||||||
|
// ChannelID, Data
|
||||||
|
val roleList = mutableMapOf<Snowflake, PlanningNotifierRolesData>()
|
||||||
|
|
||||||
transaction {
|
transaction {
|
||||||
for (tp in TimePlanningChannels.selectAll()) {
|
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]}.")
|
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)
|
if (Bot.bot.kordRef.getChannel(ch) == null)
|
||||||
continue // TODO: Check if the channel is valid in another shard
|
continue // TODO: Check if the channel is valid in another shard
|
||||||
val c = Bot.bot.kordRef.getChannelOf<MessageChannel>(ch)!!
|
val c = Bot.bot.kordRef.getChannelOf<MessageChannel>(ch)!!
|
||||||
c.createMessage {
|
c.createMessage {
|
||||||
|
if (roleList[ch] != null) {
|
||||||
|
this.content = "<@&${Snowflake(roleList[ch]?.wantstobenotifid!!)}>"
|
||||||
|
}
|
||||||
this.embeds.add(
|
this.embeds.add(
|
||||||
MessageUtil.getEmbed(
|
MessageUtil.getEmbed(
|
||||||
Color(0X4C4645),
|
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)
|
var then = ZonedDateTime.now(ZoneId.of("Europe/Berlin")).withHour(4).withMinute(0).withSecond(0)
|
||||||
repeat(7) {
|
repeat(7) {
|
||||||
c.createMessage {
|
c.createMessage {
|
||||||
|
@ -96,8 +118,10 @@ object TimePlanner {
|
||||||
this.components.addAll(ButtonUtil.getTimePlannerButtons().components)
|
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")
|
Logger.out("Finished sending day $it")
|
||||||
|
|
||||||
delay(1000)
|
delay(1000)
|
||||||
}
|
}
|
||||||
Logger.out("Finished with ${c.data.guildId.value}")
|
Logger.out("Finished with ${c.data.guildId.value}")
|
Loading…
Reference in a new issue