From 52703b4b6110c56ed0f1f6e8431c54472096dd5d Mon Sep 17 00:00:00 2001 From: moonleay Date: Thu, 7 Dec 2023 08:59:06 +0100 Subject: [PATCH] fix: fixed match command throwing exceptions when being run with false inputs Signed-off-by: moonleay --- .../lilJudd/extensions/MatchExtension.kt | 70 +++++++++++-------- 1 file changed, 42 insertions(+), 28 deletions(-) diff --git a/src/main/kotlin/net/moonleay/lilJudd/extensions/MatchExtension.kt b/src/main/kotlin/net/moonleay/lilJudd/extensions/MatchExtension.kt index 9f2674d..118d368 100644 --- a/src/main/kotlin/net/moonleay/lilJudd/extensions/MatchExtension.kt +++ b/src/main/kotlin/net/moonleay/lilJudd/extensions/MatchExtension.kt @@ -20,12 +20,10 @@ package net.moonleay.lilJudd.extensions import com.kotlindiscord.kord.extensions.commands.Arguments import com.kotlindiscord.kord.extensions.commands.application.slash.converters.impl.enumChoice -import com.kotlindiscord.kord.extensions.commands.converters.impl.optionalString import com.kotlindiscord.kord.extensions.commands.converters.impl.string import com.kotlindiscord.kord.extensions.extensions.Extension import com.kotlindiscord.kord.extensions.extensions.publicSlashCommand import com.kotlindiscord.kord.extensions.types.respond -import dev.kord.core.behavior.channel.createMessage import dev.kord.core.behavior.createRole import dev.kord.rest.builder.message.create.actionRow import net.moonleay.lilJudd.data.database.entry.MatchPlanningDataData @@ -54,7 +52,46 @@ class MatchExtension : Extension() { val m = this.member!! val gID = this.guild!!.id.value val cID = this.channel.id.value - val opponent = args.opponent ?: "?" + val opponent = args.opponent + if (!TimeUtil.validateDateString(args.timeStamp)) { + this.respond { + this.embeds.add( + MessageUtil.getEmbed( + EmbedColor.ERROR, + "400: Bad Request", + "The given timestamp is invalid.\n" + + "Please use the format \"dd.MM.yyyy HH:mm\".", + m.asUser().username + ) + ) + } + return@action + } + // filter time to date: + val zdt = TimeUtil.getDateFromString(args.timeStamp) + // get the string for the cronjob + val jobString = TimeUtil.getCronjobStringFromDate(zdt) + // create the role + val role = this.guild!!.createRole { + this.name = + "${args.matchType.readableName} Vs ${opponent} At ${zdt.dayOfMonth}/${zdt.month}/${zdt.year} ${zdt.hour}:${zdt.minute}" + this.mentionable = true + } + // Check if the role was created successfully + if (role == null) { + this.respond { + this.embeds.add( + MessageUtil.getEmbed( + EmbedColor.ERROR, + "500: Internal Error", + "Could not find created role.\n" + + "It seems, that said role could not be created.", + m.asUser().username + ) + ) + } + return@action + } val msg = this.respond { this.embeds.add( MessageUtil.getEmbedWithTable( @@ -73,30 +110,6 @@ class MatchExtension : Extension() { this.actionRow { this.components.addAll(EmbedUtil.getMatchButtons().components) } - } // filter time to date: - val zdt = TimeUtil.getDateFromString(args.timeStamp) - // get the string for the cronjob - val jobString = TimeUtil.getCronjobStringFromDate(zdt) - // create the role - val role = this.guild!!.createRole { - this.name = - "${args.matchType.readableName} Vs ${opponent} At ${zdt.dayOfMonth}/${zdt.month}/${zdt.year} ${zdt.hour}:${zdt.minute}" - this.mentionable = true - } - // Check if the role was created successfully - if (role == null) { - this.channel.createMessage { - this.embeds.add( - MessageUtil.getEmbed( - EmbedColor.ERROR, - "500: Internal Error", - "Could not find created role.\n" + - "It seems, that said role could not be created.", - "system message" - ) - ) - } - return@action } val tID = MatchPlanningDataRepository.write( MatchPlanningDataData( @@ -114,6 +127,7 @@ class MatchExtension : Extension() { ) if (tID == null || tID <= 0) { return@action // Not saved to db + // TODO: Add error message } JobManager.addJob( MatchJob( @@ -140,7 +154,7 @@ class MatchExtension : Extension() { this.description = "The timestamp of the match. Format \"dd.MM.yyyy HH:mm\"." } - val opponent by optionalString { + val opponent by string { this.name = "opponent" this.description = "The opponent" }