Skip to content

Commit

Permalink
Use enum entries and Kotlin 2.0 language version
Browse files Browse the repository at this point in the history
  • Loading branch information
russellbanks committed May 17, 2023
1 parent 3431410 commit 28aec20
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 22 deletions.
3 changes: 3 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
Expand Down Expand Up @@ -28,6 +29,7 @@ dependencies {
implementation(libs.kotlinx.coroutines.core)

// KotlinX DateTime - https://github.com/Kotlin/kotlinx-datetime
implementation(libs.kotlinx.datetime)

// SLF4J (Required by Kord) - https://github.com/qos-ch/slf4j
implementation(libs.slf4j.simple)
Expand All @@ -48,6 +50,7 @@ sqldelight {
tasks.withType<KotlinCompile> {
compilerOptions {
jvmTarget.set(JvmTarget.JVM_17)
languageVersion.set(KotlinVersion.KOTLIN_2_0)
}
}

Expand Down
17 changes: 5 additions & 12 deletions src/main/kotlin/extensions/ConfigureExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import extensions.voicestateupdate.Action
class ConfigureExtension: Extension() {
override val name = "configure"

@OptIn(ExperimentalStdlibApi::class)
override suspend fun setup() {
ephemeralSlashCommand {
name = "configure"
Expand All @@ -70,7 +71,7 @@ class ConfigureExtension: Extension() {
icon = guild.asGuild().icon?.cdnUrl?.toUrl()
}
color = Color(EnvironmentVariables.accentColor()[0], EnvironmentVariables.accentColor()[1], EnvironmentVariables.accentColor()[2])
for (action in actionList) {
for (action in Action.entries) {
field(
buildString {
append(action.name.lowercase().replaceFirstChar(Char::titlecase))
Expand All @@ -86,7 +87,7 @@ class ConfigureExtension: Extension() {
}
components {
if (member != null) {
for (action in actionList) {
for (action in Action.entries) {
publicActionButton(action, guild)
}
}
Expand Down Expand Up @@ -129,6 +130,7 @@ class ConfigureExtension: Extension() {
}
}

@OptIn(ExperimentalStdlibApi::class)
private suspend fun ComponentContainer.publicActionButton(action: Action, guild: GuildBehavior) {
publicButton {
label = action.name.lowercase().replaceFirstChar(Char::titlecase)
Expand All @@ -144,7 +146,7 @@ class ConfigureExtension: Extension() {
icon = guild.asGuild().icon?.cdnUrl?.toUrl()
}
color = Color(EnvironmentVariables.accentColor()[0], EnvironmentVariables.accentColor()[1], EnvironmentVariables.accentColor()[2])
for (actionItem in actionList) {
for (actionItem in Action.entries) {
field(
buildString {
append(actionItem.name.lowercase().replaceFirstChar(Char::titlecase))
Expand All @@ -163,14 +165,6 @@ class ConfigureExtension: Extension() {
}
}

private val actionList = setOf(
Action.JOIN,
Action.LEAVE,
Action.SWITCH,
Action.STREAM,
Action.VIDEO
)

private suspend fun getActionToggle(action: Action, guild: GuildBehavior): Boolean {
val guildPrefs = Dao.get(guild)
return when (action) {
Expand All @@ -179,7 +173,6 @@ class ConfigureExtension: Extension() {
Action.SWITCH -> guildPrefs.switch
Action.STREAM -> guildPrefs.stream
Action.VIDEO -> guildPrefs.video
Action.UNKNOWN -> error("Unknown")
}
}
}
3 changes: 1 addition & 2 deletions src/main/kotlin/extensions/voicestateupdate/Action.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,5 @@ enum class Action(val phrase: String, val emoji: DiscordEmoji.Generic) {
LEAVE("left", Emojis.door),
SWITCH("switched to", Emojis.repeat),
STREAM("is live in", Emojis.redCircle),
VIDEO("turned their video on in", Emojis.camera),
UNKNOWN("unknown", Emojis.greyQuestion)
VIDEO("turned their video on in", Emojis.camera)
}
18 changes: 10 additions & 8 deletions src/main/kotlin/extensions/voicestateupdate/VoiceStateExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,19 @@ class VoiceStateExtension: Extension() {

val member = event.state.getMember()
if (shouldSendEmbed(action, guildPrefs, member)) {
guildPrefs.channelId?.let { Snowflake(it) }?.let {
MessageChannelBehavior(it, kord).createEmbed {
guildPrefs.channelId?.let(::Snowflake)?.let { snowflake ->
MessageChannelBehavior(snowflake, kord).createEmbed {
color = Color(EnvironmentVariables.accentColor()[0], EnvironmentVariables.accentColor()[1], EnvironmentVariables.accentColor()[2])
title = "${member.displayName} ${action.phrase} ${channel?.asChannel()?.name}"
title = "${member.displayName} ${action?.phrase} ${channel?.asChannel()?.name}"
timestamp = Clock.System.now()
author {
name = member.displayName
icon = member.avatar?.cdnUrl?.toUrl()
}
footer {
text = action.emoji.unicode
action?.emoji?.unicode?.let {
text = it
}
}
}
}
Expand All @@ -69,24 +71,24 @@ class VoiceStateExtension: Extension() {
}
}

private suspend fun getAction(event: VoiceStateUpdateEvent): Action = when {
private suspend fun getAction(event: VoiceStateUpdateEvent): Action? = when {
event.old?.getChannelOrNull() == null -> Action.JOIN
event.old?.getChannelOrNull() != event.state.getChannelOrNull()
&& event.state.getChannelOrNull() != null -> Action.SWITCH
event.state.getChannelOrNull() == null -> Action.LEAVE
event.old?.isSelfStreaming == false && event.state.isSelfStreaming -> Action.STREAM
else -> Action.UNKNOWN
else -> null
}

private fun shouldSendEmbed(action: Action, guildPrefs: GuildPrefs, member: Member): Boolean {
private fun shouldSendEmbed(action: Action?, guildPrefs: GuildPrefs, member: Member): Boolean {
return when {
member.isBot -> false
action == Action.JOIN && !guildPrefs.joinPref -> false
action == Action.LEAVE && !guildPrefs.leave -> false
action == Action.SWITCH && !guildPrefs.switch -> false
action == Action.STREAM && !guildPrefs.stream -> false
action == Action.VIDEO && !guildPrefs.video -> false
action == Action.UNKNOWN -> false
action == null -> false
else -> true
}
}
Expand Down

0 comments on commit 28aec20

Please sign in to comment.