fix: fixed match command throwing exceptions when being run with false inputs

Signed-off-by: moonleay <contact@moonleay.net>
This commit is contained in:
moonleay 2023-12-07 08:59:06 +01:00
parent 984bcabd0c
commit 52703b4b61
Signed by: moonleay
GPG key ID: 82667543CCD715FB

View file

@ -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"
}