feat!: made UserData nullable in UserRepository

This commit is contained in:
moonleay 2024-01-18 08:46:51 +01:00
parent 3e8a2d2d83
commit 48671c3b0a
Signed by: moonleay
GPG key ID: 82667543CCD715FB
5 changed files with 12 additions and 9 deletions

View file

@ -61,10 +61,10 @@ object UserRepository {
} }
fun getUserByID(id: ULong): UserData = getUserByID(id.toLong()) fun getUserByID(id: ULong): UserData? = getUserByID(id.toLong())
fun getUserByID(id: Long): UserData { fun getUserByID(id: Long): UserData? {
lateinit var user: UserData var user: UserData? = null
transaction { transaction {
UserTable.select(UserTable.userid eq id).forEach { UserTable.select(UserTable.userid eq id).forEach {
user = UserData( user = UserData(

View file

@ -39,7 +39,7 @@ class AwakeExtension : Extension() {
val u = this.user.asUser() val u = this.user.asUser()
Logger.out("User ${u.username} wants to be awake") Logger.out("User ${u.username} wants to be awake")
if (UserRepository.doesUserExist(u.id.value)){ if (UserRepository.doesUserExist(u.id.value)){
val ud = UserRepository.getUserByID(u.id.value) val ud = UserRepository.getUserByID(u.id.value)!!
if (ud.isAwake) { if (ud.isAwake) {
// User is already awake // User is already awake
if (ud.currentStreak == 0 && ud.lastWakeup + (1000*60*60*12) > System.currentTimeMillis()) { if (ud.currentStreak == 0 && ud.lastWakeup + (1000*60*60*12) > System.currentTimeMillis()) {

View file

@ -60,7 +60,7 @@ class ProfileExtension : Extension() {
} }
return@action return@action
} }
val td = UserRepository.getUserByID(target.id.value) val td = UserRepository.getUserByID(target.id.value)!!
this.respond { this.respond {
this.embeds.add( this.embeds.add(
getProfileMsg(td, target, target) getProfileMsg(td, target, target)
@ -84,7 +84,7 @@ class ProfileExtension : Extension() {
} }
return@action return@action
} }
val td = UserRepository.getUserByID(target.id.value) val td = UserRepository.getUserByID(target.id.value)!!
this.respond { this.respond {
this.embeds.add( this.embeds.add(
getProfileMsg(td, target, user) getProfileMsg(td, target, user)

View file

@ -51,9 +51,12 @@ class TimeExtension : Extension() {
val targetChannelId = this.channel.asChannel().id.value.toLong() val targetChannelId = this.channel.asChannel().id.value.toLong()
lateinit var ud: UserData lateinit var ud: UserData
// This should be correct;
val timeToSleep = timeToWake.toEpochSecond() * 1000 - System.currentTimeMillis()
if (UserRepository.doesUserExist(u.id.value)) { if (UserRepository.doesUserExist(u.id.value)) {
// Update existing user // Update existing user
ud = UserRepository.getUserByID(u.id.value) ud = UserRepository.getUserByID(u.id.value)!!
ud.nextWakeup = timeToWake.toEpochSecond() * 1000 // ms ud.nextWakeup = timeToWake.toEpochSecond() * 1000 // ms
ud.nextWakeupCron = cronJobString ud.nextWakeupCron = cronJobString
ud.isAwake = false ud.isAwake = false
@ -92,7 +95,7 @@ class TimeExtension : Extension() {
this.embeds.add(MessageUtil.getEmbed( this.embeds.add(MessageUtil.getEmbed(
EmbedColor.SUCCESS, EmbedColor.SUCCESS,
"Wakeup time set", "Wakeup time set",
"${u.mention} is now set to be awake at $targetTime.\n" + "${u.mention} is now set to be awake in ${TimeUtil.getTimeFormatedShortend(timeToSleep, false)} at $targetTime.\n" +
"Have a restfull sleep!\n" + "Have a restfull sleep!\n" +
"***You can change your wakeup time by running /time again!***", "***You can change your wakeup time by running /time again!***",
u.username u.username

View file

@ -44,7 +44,7 @@ class WakeupJob(override val jobName: String, override val jobIncoming: String,
override suspend fun jobFunction() { override suspend fun jobFunction() {
Logger.out("WakeupJob, running \"$jobName\"") Logger.out("WakeupJob, running \"$jobName\"")
val user = UserRepository.getUserByID(userId) val user = UserRepository.getUserByID(userId)!!
if (!user.isAwake){ if (!user.isAwake){
// Failed // Failed
val userasuser = Bot.bot.kordRef.getUser(Snowflake(userId))!!.asUser() val userasuser = Bot.bot.kordRef.getUser(Snowflake(userId))!!.asUser()