Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stage: add slash commands #6

Open
wants to merge 16 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 9 additions & 13 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
plugins {
application
id("com.google.cloud.tools.jib") version "2.8.0"
kotlin("jvm") version "1.5.0"
kotlin("kapt") version "1.5.0"
kotlin("plugin.serialization") version "1.5.0"
id("com.google.cloud.tools.jib") version "3.1.1"
kotlin("jvm") version "1.5.10"
kotlin("kapt") version "1.5.10"
kotlin("plugin.serialization") version "1.5.10"
}

group = "to.rxs"
Expand All @@ -17,9 +17,9 @@ repositories {
dependencies {
runtimeOnly(kotlin("scripting-jsr223"))

implementation("org.jetbrains.kotlinx", "kotlinx-coroutines-jdk8", "1.5.0-RC")
implementation("org.jetbrains.kotlinx", "kotlinx-coroutines-jdk8", "1.5.0")

implementation("org.jetbrains.kotlinx", "kotlinx-serialization-core", "1.2.0")
implementation("org.jetbrains.kotlinx", "kotlinx-serialization-core", "1.2.1")

implementation("io.github.pdvrieze.xmlutil", "core-jvm", "0.81.2")
implementation("io.github.pdvrieze.xmlutil", "serialization-jvm", "0.81.2")
Expand All @@ -36,14 +36,10 @@ dependencies {

implementation("org.xerial", "sqlite-jdbc", "3.34.0")

implementation("io.ktor", "ktor-server-netty", "1.5.3")
implementation("io.ktor", "ktor-serialization", "1.5.3")
implementation("io.ktor", "ktor-server-netty", "1.5.4")
implementation("io.ktor", "ktor-serialization", "1.5.4")
DRSchlaubi marked this conversation as resolved.
Show resolved Hide resolved

implementation("dev.kord", "kord-core", "kotlin-1.5-20210505.195343-2") {
version {
strictly("kotlin-1.5-20210505.195343-2")
}
}
implementation("dev.kord", "kord-core", "0.7.x-SNAPSHOT")

implementation("dev.kord.x", "emoji", "0.5.0-SNAPSHOT")

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
12 changes: 12 additions & 0 deletions src/main/kotlin/to/rxs/kommunity/Config.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package to.rxs.kommunity

import dev.kord.common.entity.Snowflake
import org.apache.logging.log4j.Level
import to.rxs.kommunity.util.EnvironmentConfig

Expand All @@ -16,6 +17,7 @@ object Config : EnvironmentConfig("") {
val DISCORD_TOKEN by getEnv()
val NOTIFICATION_SERVER_PORT by getEnv(1337) { it.toInt() }
val GUILD_ID by getEnv()
val ROLES by getEnv { createRolesMap(it) }
val SERVER_NEWS_ROLE by getEnv()
val VIDEO_NEWS_ROLE by getEnv()
val STREAM_NEWS_ROLE by getEnv()
Expand All @@ -31,3 +33,13 @@ enum class Environment {
DEVELOPMENT,
PRODUCTION
}

fun createRolesMap (env: String) : Map<String, Snowflake> {
DRSchlaubi marked this conversation as resolved.
Show resolved Hide resolved
val roles = HashMap<String, Snowflake>()
val parts = env.split(",")
for (part: String in parts) {
val separated = part.split(":")
roles[separated[0]] = Snowflake(separated[1])
}
return roles
}
2 changes: 2 additions & 0 deletions src/main/kotlin/to/rxs/kommunity/Kommunity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import kotlinx.coroutines.launch
import mu.KotlinLogging
import to.rxs.kommunity.core.GameAnimator
import to.rxs.kommunity.io.connect
import to.rxs.kommunity.listeners.interactionListener
import to.rxs.kommunity.listeners.joinListener
import to.rxs.kommunity.listeners.selfMentionListener
import to.rxs.kommunity.youtube.CallbackServer
Expand Down Expand Up @@ -54,6 +55,7 @@ class Kommunity {
kord.apply {
selfMentionListener()
joinListener()
interactionListener()
on<ReadyEvent> {
start()
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import dev.kord.x.commands.kord.argument.TextChannelArgument
import dev.kord.x.commands.kord.module.module
import dev.kord.x.commands.model.command.invoke
import io.ktor.client.request.*
import to.rxs.kommunity.commands.arguments.HastebinArgument
import to.rxs.kommunity.commands.arguments.URLArgument
import to.rxs.kommunity.commands.message.arguments.HastebinArgument
import to.rxs.kommunity.commands.message.arguments.URLArgument
import to.rxs.kommunity.util.httpClient
import to.rxs.kommunity.util.withPermission

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package to.rxs.kommunity.commands.arguments
package to.rxs.kommunity.commands.message.arguments

internal val HastebinArgument = RegexArgument(
"hastebin.com/blah.kotlin",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package to.rxs.kommunity.commands.arguments
package to.rxs.kommunity.commands.message.arguments

import dev.kord.x.commands.argument.SingleWordArgument
import dev.kord.x.commands.argument.result.WordResult
Expand Down
12 changes: 12 additions & 0 deletions src/main/kotlin/to/rxs/kommunity/commands/slash/SlashCommand.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package to.rxs.kommunity.commands.slash

import dev.kord.common.annotation.KordPreview
import dev.kord.core.Kord
import dev.kord.core.entity.interaction.Interaction

@KordPreview
interface SlashCommand {

suspend fun handle(kord: Kord, interaction: Interaction)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package to.rxs.kommunity.commands.slash.impl

import dev.kord.common.annotation.KordPreview
import dev.kord.core.Kord
import dev.kord.core.behavior.interaction.respondPublic
import dev.kord.core.entity.GuildEmoji
import dev.kord.core.entity.ReactionEmoji
import dev.kord.core.entity.interaction.Interaction
import dev.kord.x.emoji.Emojis
import to.rxs.kommunity.Config
import to.rxs.kommunity.commands.slash.SlashCommand
import to.rxs.kommunity.misc.EmojiHelper

@KordPreview
object OptSlashCommand : SlashCommand {

override suspend fun handle(kord: Kord, interaction: Interaction) {
val action = interaction.command.options["action"]!!.value
val guild = kord.getGuild(interaction.data.guildId.value!!)!!
val role = Config.ROLES[interaction.command.options["notification"]!!.value]!!

if (action == "in") {
kord.rest.guild.addRoleToGuildMember(guild.id, interaction.user.id, role, "User opt in command")
} else if (action == "out") {
kord.rest.guild.deleteRoleFromGuildMember(guild.id, interaction.user.id, role, "User opt out command")
}
DRSchlaubi marked this conversation as resolved.
Show resolved Hide resolved

interaction.respondPublic {
embed {
title = "${EmojiHelper.SUCCESS} Success!"
description = "Successfully opted ${
when (action) {
"in" -> "**in** to"
"out" -> "**out** from"
else -> "undefined"
}
} `${interaction.command.options["notification"]!!.value}` notifications!"
}
}
}
}
17 changes: 17 additions & 0 deletions src/main/kotlin/to/rxs/kommunity/listeners/InteractionListener.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package to.rxs.kommunity.listeners

import dev.kord.common.annotation.KordPreview
import dev.kord.core.Kord
import dev.kord.core.event.interaction.InteractionCreateEvent
import dev.kord.core.on
import mu.KotlinLogging
import to.rxs.kommunity.commands.slash.impl.OptSlashCommand

private val log = KotlinLogging.logger {}

@OptIn(KordPreview::class)
fun Kord.interactionListener() = on<InteractionCreateEvent> {
when (interaction.command.rootName) {
"opt" -> OptSlashCommand.handle(kord, interaction)
}
}
DRSchlaubi marked this conversation as resolved.
Show resolved Hide resolved
7 changes: 7 additions & 0 deletions src/main/kotlin/to/rxs/kommunity/misc/EmojiHelper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package to.rxs.kommunity.misc

object EmojiHelper {

const val SUCCESS = "<:check:739205319237959820>"

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package to.rxs.kommunity.youtube
import dev.kord.common.entity.Snowflake
import dev.kord.core.Kord
import io.ktor.client.request.*
import kotlinx.datetime.Instant
import to.rxs.kommunity.Config
import to.rxs.kommunity.util.httpClient
import java.time.Instant

class YoutubeEventListener(private val client: Kord) : YouTubeEventSubscriber {

Expand Down