Skip to content

Commit

Permalink
Remove unused implementations of the CommandContextCompat
Browse files Browse the repository at this point in the history
  • Loading branch information
MrPowerGamerBR committed Feb 8, 2025
1 parent 8ee2b07 commit 6a7198b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 168 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import net.perfectdreams.loritta.cinnamon.pudding.tables.loricoolcards.LoriCoolC
import net.perfectdreams.loritta.common.utils.GACampaigns
import net.perfectdreams.loritta.i18n.I18nKeysData
import net.perfectdreams.loritta.morenitta.LorittaBot
import net.perfectdreams.loritta.morenitta.commands.CommandContext
import net.perfectdreams.loritta.morenitta.interactions.CommandContextCompat
import net.perfectdreams.loritta.morenitta.interactions.UnleashedContext
import net.perfectdreams.loritta.morenitta.loricoolcards.StickerAlbumTemplate
Expand Down Expand Up @@ -326,7 +325,6 @@ object SonhosUtils {
}
}

suspend fun checkIfEconomyIsDisabled(context: CommandContext) = checkIfEconomyIsDisabled(CommandContextCompat.LegacyMessageCommandContextCompat(context))
suspend fun checkIfEconomyIsDisabled(context: DiscordCommandContext) = checkIfEconomyIsDisabled(CommandContextCompat.LegacyDiscordCommandContextCompat(context))

suspend fun checkIfEconomyIsDisabled(context: UnleashedContext): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ import net.perfectdreams.loritta.common.achievements.AchievementType
import net.perfectdreams.loritta.common.locale.BaseLocale
import net.perfectdreams.loritta.common.utils.text.TextUtils.convertMarkdownLinksWithLabelsToPlainLinks
import net.perfectdreams.loritta.morenitta.LorittaBot
import net.perfectdreams.loritta.morenitta.commands.CommandContext
import net.perfectdreams.loritta.morenitta.dao.ServerConfig
import net.perfectdreams.loritta.morenitta.interactions.commands.ApplicationCommandContext
import net.perfectdreams.loritta.morenitta.interactions.components.ComponentContext
import net.perfectdreams.loritta.morenitta.platform.discord.legacy.commands.DiscordCommandContext

/**
* A command context that provides compatibility with slash commands and message commands, for ease of migration while maintaining both commands.
*
* Originally this was used to migrate legacy message commands to interaction commands, however this was fully replaced by InteraKTions Unleashed, which
* supports both interaction commands and legacy commands.
*
* Nowadays, this is only used by some of the legacy commands, which is why all other [CommandContextCompat] implementations are not present anymore.
*
* Ephemeral message state is ignored when using it with normal non-interactions commands. Don't use it to show sensitive information!
*/
interface CommandContextCompat {
Expand All @@ -40,112 +42,6 @@ interface CommandContextCompat {
suspend fun reply(ephemeral: Boolean, builder: suspend InlineMessage<MessageCreateData>.() -> Unit = {}): InteractionMessage
suspend fun giveAchievementAndNotify(achievementType: AchievementType, achievedAt: Instant = Clock.System.now())

/**
* Compatibility layer for Loritta's InteraKTions Unleashed command contexts
*/
class InteractionsCommandContextCompat(val context: UnleashedContext) : CommandContextCompat {
override val user: User
get() = context.user

override val guild: Guild
get() = context.guild

override val guildOrNull: Guild?
get() = context.guildOrNull

override val config: ServerConfig
get() = context.config

override val locale: BaseLocale
get() = context.locale

override val i18nContext: I18nContext
get() = context.i18nContext

override val messageChannel: MessageChannel
get() = context.channelOrNull ?: error("Command wasn't used in a message channel!")

override val loritta: LorittaBot
get() = context.loritta

override suspend fun deferChannelMessage(ephemeral: Boolean) {
context.deferChannelMessage(ephemeral)
}

override suspend fun reply(ephemeral: Boolean, builder: suspend InlineMessage<MessageCreateData>.() -> Unit): InteractionMessage {
return context.reply(
ephemeral
) {
// We need to do this because "builder" is suspendable, because we can't inline this function due to it being in an interface
builder()
}
}

override suspend fun giveAchievementAndNotify(achievementType: AchievementType, achievedAt: Instant) {
AchievementUtils.giveAchievementToUserAndNotifyThem(
context.loritta,
this,
context.i18nContext,
UserSnowflake.fromId(user.idLong),
achievementType,
achievedAt
)
}
}

/**
* Compatibility layer for Loritta's InteraKTions Unleashed component contexts
*/
class ComponentContextCompat(val context: ComponentContext) : CommandContextCompat {
override val user: User
get() = context.user

override val guild: Guild
get() = context.guild

override val guildOrNull: Guild?
get() = context.guildOrNull

override val config: ServerConfig
get() = context.config

override val locale: BaseLocale
get() = context.locale

override val i18nContext: I18nContext
get() = context.i18nContext

override val messageChannel: MessageChannel
get() = (context.event.channel as? MessageChannel) ?: error("Command wasn't used in a message channel!")

override val loritta: LorittaBot
get() = context.loritta

override suspend fun deferChannelMessage(ephemeral: Boolean) {
context.deferChannelMessage(ephemeral)
}

override suspend fun reply(ephemeral: Boolean, builder: suspend InlineMessage<MessageCreateData>.() -> Unit): InteractionMessage {
return context.reply(
ephemeral
) {
// We need to do this because "builder" is suspendable, because we can't inline this function due to it being in an interface
builder()
}
}

override suspend fun giveAchievementAndNotify(achievementType: AchievementType, achievedAt: Instant) {
AchievementUtils.giveAchievementToUserAndNotifyThem(
context.loritta,
this,
context.i18nContext,
UserSnowflake.fromId(user.idLong),
achievementType,
achievedAt
)
}
}

/**
* Compatibility layer for Loritta's Discord Command Framework command contexts
*/
Expand Down Expand Up @@ -202,61 +98,4 @@ interface CommandContextCompat {
)
}
}

/**
* Compatibility layer for Loritta's legacy command framework command contexts
*/
class LegacyMessageCommandContextCompat(val context: CommandContext) : CommandContextCompat {
override val user: User
get() = context.userHandle

override val guild: Guild
get() = context.guild

override val guildOrNull: Guild?
get() = context.guildOrNull

override val config: ServerConfig
get() = context.config

override val locale: BaseLocale
get() = context.locale

override val i18nContext: I18nContext
get() = context.i18nContext

override val messageChannel: MessageChannel
get() = context.message.channel

override val loritta: LorittaBot
get() = context.loritta

override suspend fun deferChannelMessage(ephemeral: Boolean) {
// noop
}

override suspend fun reply(ephemeral: Boolean, builder: suspend InlineMessage<MessageCreateData>.() -> Unit): InteractionMessage {
val inlineBuilder = MessageCreate {
// We need to do this because "builder" is suspendable, because we can't inline this function due to it being in an interface
builder()

// We are going to replace any links with labels with just links, since Discord does not support labels with links if it isn't a webhook or an interaction
content = content?.convertMarkdownLinksWithLabelsToPlainLinks()
}

// This isn't a real follow-up interaction message, but we do have the message data, so that's why we are using it
return InteractionMessage.FollowUpInteractionMessage(context.sendMessage(inlineBuilder))
}

override suspend fun giveAchievementAndNotify(achievementType: AchievementType, achievedAt: Instant) {
AchievementUtils.giveAchievementToUserAndNotifyThem(
context.loritta,
this,
context.i18nContext,
UserSnowflake.fromId(user.idLong),
achievementType,
achievedAt
)
}
}
}

0 comments on commit 6a7198b

Please sign in to comment.