From bcb2caa74bcec0e9da885beba856bf5d4ef9bce0 Mon Sep 17 00:00:00 2001 From: NovaFox161 Date: Sun, 27 Nov 2022 19:09:49 -0600 Subject: [PATCH] Add backwards compatibility for suspend/reactive commands --- .../discal/client/commands/SlashCommand.kt | 13 +++++++++++-- .../discal/client/commands/global/TimeCommand.kt | 10 +++++----- .../listeners/discord/SlashCommandListener.kt | 3 ++- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/client/src/main/kotlin/org/dreamexposure/discal/client/commands/SlashCommand.kt b/client/src/main/kotlin/org/dreamexposure/discal/client/commands/SlashCommand.kt index afea62e4..c04553a1 100644 --- a/client/src/main/kotlin/org/dreamexposure/discal/client/commands/SlashCommand.kt +++ b/client/src/main/kotlin/org/dreamexposure/discal/client/commands/SlashCommand.kt @@ -1,7 +1,9 @@ package org.dreamexposure.discal.client.commands -import discord4j.core.`object`.entity.Message import discord4j.core.event.domain.interaction.ChatInputInteractionEvent +import discord4j.core.`object`.entity.Message +import kotlinx.coroutines.reactor.awaitSingle +import kotlinx.coroutines.reactor.mono import org.dreamexposure.discal.core.`object`.GuildSettings import org.dreamexposure.discal.core.utils.MessageSourceLoader import reactor.core.publisher.Mono @@ -11,7 +13,14 @@ interface SlashCommand { val ephemeral: Boolean - fun handle(event: ChatInputInteractionEvent, settings: GuildSettings): Mono + @Deprecated("Use new handleSuspend for K-coroutines") + fun handle(event: ChatInputInteractionEvent, settings: GuildSettings): Mono { + return mono { suspendHandle(event, settings) } + } + + suspend fun suspendHandle(event: ChatInputInteractionEvent, settings: GuildSettings): Message { + return handle(event, settings).awaitSingle() + } fun getMessage(key: String, settings: GuildSettings, vararg args: String): String { val src = MessageSourceLoader.getSourceByPath("command/$name/$name") diff --git a/client/src/main/kotlin/org/dreamexposure/discal/client/commands/global/TimeCommand.kt b/client/src/main/kotlin/org/dreamexposure/discal/client/commands/global/TimeCommand.kt index 1719df68..51fec260 100644 --- a/client/src/main/kotlin/org/dreamexposure/discal/client/commands/global/TimeCommand.kt +++ b/client/src/main/kotlin/org/dreamexposure/discal/client/commands/global/TimeCommand.kt @@ -1,23 +1,23 @@ 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 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.extensions.discord4j.followupEphemeral +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 TimeCommand : SlashCommand { override val name = "time" override val ephemeral = true - override fun handle(event: ChatInputInteractionEvent, settings: GuildSettings): Mono { + override suspend fun suspendHandle(event: ChatInputInteractionEvent, settings: GuildSettings): Message { val calendarNumber = event.getOption("calendar") .flatMap(ApplicationCommandInteractionOption::getValue) .map(ApplicationCommandInteractionOptionValue::asLong) @@ -28,6 +28,6 @@ class TimeCommand : SlashCommand { CalendarEmbed.time(guild, settings, calendarNumber).flatMap { event.followupEphemeral(it) } - }.switchIfEmpty(event.followupEphemeral(getCommonMsg("error.notFound.calendar", settings))) + }.switchIfEmpty(event.followupEphemeral(getCommonMsg("error.notFound.calendar", settings))).awaitSingle() } } diff --git a/client/src/main/kotlin/org/dreamexposure/discal/client/listeners/discord/SlashCommandListener.kt b/client/src/main/kotlin/org/dreamexposure/discal/client/listeners/discord/SlashCommandListener.kt index c7320104..82574f4b 100644 --- a/client/src/main/kotlin/org/dreamexposure/discal/client/listeners/discord/SlashCommandListener.kt +++ b/client/src/main/kotlin/org/dreamexposure/discal/client/listeners/discord/SlashCommandListener.kt @@ -27,7 +27,8 @@ class SlashCommandListener( try { val settings = DatabaseManager.getSettings(event.interaction.guildId.get()).awaitSingle() - command.handle(event, settings).awaitSingleOrNull() + + command.suspendHandle(event, settings) } catch (e: Exception) { LOGGER.error(DEFAULT, "Error handling slash command | $event", e)