-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor linkCal command to kotlin coroutines
- Loading branch information
1 parent
ca715f6
commit 59b1d5b
Showing
3 changed files
with
50 additions
and
122 deletions.
There are no files selected for viewing
31 changes: 19 additions & 12 deletions
31
...nt/src/main/kotlin/org/dreamexposure/discal/client/commands/global/LinkCalendarCommand.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,45 @@ | ||
package org.dreamexposure.discal.client.commands.global | ||
|
||
import discord4j.core.event.domain.interaction.ChatInputInteractionEvent | ||
import discord4j.core.`object`.command.ApplicationCommandInteractionOption | ||
import discord4j.core.`object`.command.ApplicationCommandInteractionOptionValue | ||
import discord4j.core.`object`.entity.Message | ||
import discord4j.core.event.domain.interaction.ChatInputInteractionEvent | ||
import kotlinx.coroutines.reactor.awaitSingle | ||
import kotlinx.coroutines.reactor.awaitSingleOrNull | ||
import org.dreamexposure.discal.client.commands.SlashCommand | ||
import org.dreamexposure.discal.client.message.embed.CalendarEmbed | ||
import org.dreamexposure.discal.core.`object`.GuildSettings | ||
import org.dreamexposure.discal.core.business.EmbedService | ||
import org.dreamexposure.discal.core.extensions.discord4j.followup | ||
import org.dreamexposure.discal.core.extensions.discord4j.getCalendar | ||
import org.dreamexposure.discal.core.`object`.GuildSettings | ||
import org.dreamexposure.discal.core.utils.getCommonMsg | ||
import org.springframework.stereotype.Component | ||
import reactor.core.publisher.Mono | ||
|
||
@Component | ||
class LinkCalendarCommand : SlashCommand { | ||
class LinkCalendarCommand( | ||
private val embedService: EmbedService, | ||
) : SlashCommand { | ||
override val name = "linkcal" | ||
override val ephemeral = false | ||
|
||
@Deprecated("Use new handleSuspend for K-coroutines") | ||
override fun handle(event: ChatInputInteractionEvent, settings: GuildSettings): Mono<Message> { | ||
|
||
override suspend fun suspendHandle(event: ChatInputInteractionEvent, settings: GuildSettings): Message { | ||
val showOverview = event.getOption("overview") | ||
.flatMap(ApplicationCommandInteractionOption::getValue) | ||
.map(ApplicationCommandInteractionOptionValue::asBoolean) | ||
.orElse(true) | ||
|
||
val calendarNumber = event.getOption("calendar") | ||
.flatMap(ApplicationCommandInteractionOption::getValue) | ||
.map(ApplicationCommandInteractionOptionValue::asLong) | ||
.map(Long::toInt) | ||
.orElse(1) | ||
|
||
return event.interaction.guild.flatMap { guild -> | ||
CalendarEmbed.link(guild, settings, calendarNumber, showOverview) | ||
.flatMap(event::followup) | ||
}.switchIfEmpty(event.followup(getCommonMsg("error.notFound.calendar", settings))) | ||
val calendar = event.interaction.guild.flatMap { | ||
it.getCalendar(calendarNumber) | ||
}.awaitSingleOrNull() | ||
if (calendar == null) { | ||
return event.followup(getCommonMsg("error.notFound.calendar", settings)).awaitSingle() | ||
} | ||
|
||
return event.followup(embedService.linkCalendarEmbed(calendarNumber, settings, showOverview)).awaitSingle() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters