Skip to content

Commit

Permalink
Feature/improvments (#74)
Browse files Browse the repository at this point in the history
* Bump dependencies
- Fix detekt issues
- Enable detekt autocorrect

* Enable detekt for sub-projects

* Fix slash command registration

* Implement tags user ping (Fix #60)

* Fix tag-list command ignoring range (Fix #65)

* Make paginator context-aware to use buttons in slash command context

* Run detekt

* Bump version

* Fix detekt
  • Loading branch information
DRSchlaubi authored Jul 18, 2021
1 parent 84633d2 commit c2218d9
Show file tree
Hide file tree
Showing 47 changed files with 675 additions and 319 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ jobs:
~/build
~/.gradle
- name: Build with Gradle
run: ./gradlew compileKotlin detektMain check
run: ./gradlew compileKotlin detekt check
6 changes: 3 additions & 3 deletions autohelp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = "me.schlaubi"
version = "2.0.0-RC.4"
version = "2.0.0-RC.5"

repositories {
mavenCentral()
Expand All @@ -15,8 +15,8 @@ repositories {

dependencies {
api("dev.schlaubi.forp", "forp-analyze-api-jvm", "1.0-SNAPSHOT")
api("dev.kord.x", "emoji", "0.5.0-SNAPSHOT")
implementation("io.github.microutils", "kotlin-logging", "1.12.0")
api("dev.kord.x", "emoji", "0.5.0")
implementation("io.github.microutils", "kotlin-logging", "2.0.10")
}

kotlin {
Expand Down
10 changes: 8 additions & 2 deletions autohelp/kord/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,23 @@ repositories {

dependencies {
api(project(":autohelp"))
api("dev.kord", "kord-core", "kotlin-1.5-SNAPSHOT")
api("dev.kord", "kord-core", "0.7.3")
}

tasks {
withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions {
jvmTarget = "11"
jvmTarget = "15"
}
}
}

java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(15)) // kapt dies on JDK 16
}
}

kotlin {
explicitApi()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public class KordEventSource(kord: Kord) : EventSource<KordReceivedMessage> {
override val events: Flow<KordReceivedMessage> = kord.events
.filterIsInstance<MessageCreateEvent>()
.map { KordReceivedMessage(it.message) }

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public class KordUpdateMessage(public val kordMessage: Message) : ReceivedMessag
get() = kordMessage.data.guildId.value!!.value
override val channelId: Long
get() = kordMessage.data.channelId.value
override val authorId: Long
get() = kordMessage.author?.id?.value!!
override val authorId: Long?
get() = kordMessage.author?.id?.value
override val content: String? = null
override val files: List<ReceivedMessage.ReceivedFile>
get() = kordMessage.embeds.mapNotNull {
Expand All @@ -62,4 +62,4 @@ public class KordUpdateMessage(public val kordMessage: Message) : ReceivedMessag
}

override suspend fun react(emoji: DiscordEmoji): Unit = kordMessage.addReaction(emoji)
}
}
2 changes: 1 addition & 1 deletion autohelp/src/main/kotlin/me/schlaubi/autohelp/Builder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,4 @@ public class ContextBuilder<T : ReceivedMessage> {
public fun filter(eventFilter: EventFilter<in T>): Unit = +eventFilter

internal fun build(dispatcher: CoroutineContext): EventContext<T> = EventContext(sources, filters, dispatcher)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@ public data class Embed(
public data class Footer(public val name: String, public val url: String?, public val avatar: String?)
}

@Suppress("LongMethod")
@OptIn(ExperimentalStdlibApi::class)
internal fun DiscordConversation.toEmbed(htmlRenderer: HtmlRenderer, loadingEmote: String): Embed {
internal fun DiscordConversation.toEmbed(
htmlRenderer: HtmlRenderer,
loadingEmote: String
): Embed {
val fields = buildList {
val stackTrace = exception
if (stackTrace != null) {
Expand Down Expand Up @@ -110,7 +114,8 @@ internal fun DiscordConversation.toEmbed(htmlRenderer: HtmlRenderer, loadingEmot
add(
Embed.Field(
"Ursache - Code",
"Ich konnte keinen Code finden, bitte schicke die komplette Klasse, in der der Fehler auftritt (Am besten via hastebin)",
"Ich konnte keinen Code finden, bitte schicke die komplette" +
" Klasse, in der der Fehler auftritt (Am besten via hastebin)",
false
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import me.schlaubi.autohelp.tags.TagSupplier
import kotlin.coroutines.CoroutineContext
import kotlin.time.Duration

@Suppress("LongParameterList")
internal class AutoHelpImpl(
override val analyzer: StackTraceAnalyzer, contexts: List<EventContext<*>>,
override val coroutineContext: CoroutineContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ internal suspend fun ReceivedMessage.handle(autoHelp: AutoHelpImpl) {
}
}

@Suppress("MagicNumber")
if (found) {
autoHelp.launch {
withTimeout(Duration.ofSeconds(10)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ public class EventContext<T : ReceivedMessage>(
.filter { event ->
filters.all { it.validateEvent(event) }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ public fun interface TagSupplier {
* Finds a tag that explains [exception] or `null` if none was found.
*/
public fun findTagForException(exception: StackTrace): String?
}
}
88 changes: 45 additions & 43 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,21 @@ import io.gitlab.arturbosch.detekt.Detekt
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm") version "1.5.0"
kotlin("kapt") version "1.5.0"
kotlin("plugin.serialization") version "1.5.0"
id("io.gitlab.arturbosch.detekt") version "1.15.0"
kotlin("jvm") version "1.5.21"
kotlin("kapt") version "1.5.21"
kotlin("plugin.serialization") version "1.5.21"
id("io.gitlab.arturbosch.detekt") version "1.17.1"
application
antlr
}

group = "de.nycode"
version = "1.2.0-hotifx.1"
version = "1.3.0"

repositories {
mavenCentral()
maven("https://jitpack.io")
maven("https://oss.sonatype.org/content/repositories/snapshots")
maven("https://kotlin.bintray.com/kotlinx/")
maven("https://schlaubi.jfrog.io/artifactory/lavakord")
maven("https://schlaubi.jfrog.io/artifactory/forp")
}
Expand All @@ -62,49 +61,40 @@ application {

dependencies {
runtimeOnly(kotlin("scripting-jsr223"))
implementation("org.jetbrains.kotlinx", "kotlinx-datetime", "0.1.1")
implementation("org.jetbrains.kotlinx", "kotlinx-serialization-json", "1.0.0") {
version {
strictly("1.0.0")
}
}
implementation("org.jetbrains.kotlinx", "kotlinx-datetime", "0.2.1")
implementation("org.jetbrains.kotlinx", "kotlinx-serialization-json", "1.2.1")

implementation("dev.kord", "kord-core", "kotlin-1.5-SNAPSHOT") {
version {
strictly("kotlin-1.5-SNAPSHOT")
}
}
implementation("dev.kord.x", "emoji", "0.5.0-SNAPSHOT")
implementation("dev.kord", "kord-core", "0.7.3")
implementation("dev.kord.x", "emoji", "0.5.0")
implementation("dev.kord.x", "commands-runtime-kord", "0.4.0-SNAPSHOT")
kapt("dev.kord.x", "commands-processor", "0.4.0-SNAPSHOT")

val ktorVersion = "1.4.1"
implementation("io.ktor", "ktor-client", ktorVersion)
implementation("io.ktor", "ktor-client-cio", ktorVersion)
implementation("io.ktor", "ktor-client-json", ktorVersion)
implementation("io.ktor", "ktor-serialization", ktorVersion)
implementation(platform("io.ktor:ktor-bom:1.6.1"))
implementation("io.ktor", "ktor-client")
implementation("io.ktor", "ktor-client-cio")
implementation("io.ktor", "ktor-client-json")
implementation("io.ktor", "ktor-serialization")
implementation("io.ktor", "ktor-server-core")
implementation("io.ktor", "ktor-server-cio")

implementation("io.ktor", "ktor-server-core", ktorVersion)
implementation("io.ktor", "ktor-server-cio", ktorVersion)

implementation("io.github.microutils", "kotlin-logging", "1.12.0")
implementation("io.github.microutils", "kotlin-logging", "2.0.10")
implementation("io.github.cdimascio", "dotenv-kotlin", "6.2.2")

implementation("org.litote.kmongo", "kmongo-coroutine-serialization", "4.2.3")
implementation("org.litote.kmongo", "kmongo-coroutine-serialization", "4.2.8")
implementation("ch.qos.logback", "logback-classic", "1.2.3")
implementation("io.sentry", "sentry", "3.1.0")
implementation("io.sentry", "sentry-logback", "3.2.0")
implementation("io.sentry", "sentry", "5.0.1")
implementation("io.sentry", "sentry-logback", "5.0.1")

implementation("com.vladsch.flexmark", "flexmark-html2md-converter", "0.60.2")
implementation("com.vladsch.flexmark", "flexmark-html2md-converter", "0.62.2")

implementation("dev.schlaubi.lavakord", "kord", "1.0.0-SNAPSHOT")
implementation("dev.schlaubi.lavakord", "kord", "2.0.0")

implementation("org.ow2.asm", "asm", "9.1")
implementation("org.ow2.asm", "asm-tree", "9.1")
implementation("org.ow2.asm", "asm", "9.2")
implementation("org.ow2.asm", "asm-tree", "9.2")

detektPlugins("io.gitlab.arturbosch.detekt", "detekt-formatting", "1.15.0")
detektPlugins("io.gitlab.arturbosch.detekt", "detekt-formatting", "1.17.1")

antlr("org.antlr", "antlr4", "4.9.1")
antlr("org.antlr", "antlr4", "4.9.2")

implementation(project(":autohelp:kord"))
implementation("dev.schlaubi.forp", "forp-analyze-client", "1.0-SNAPSHOT")
Expand All @@ -113,8 +103,8 @@ dependencies {
testImplementation(kotlin("test-junit5"))
testRuntimeOnly("org.junit.jupiter", "junit-jupiter-engine", "5.6.0")
testRuntimeOnly("org.slf4j", "slf4j-simple", "1.7.30")
testImplementation("org.jetbrains.kotlinx", "kotlinx-coroutines-test", "1.4.2")
testImplementation("com.willowtreeapps.assertk", "assertk-jvm", "0.23")
testImplementation("org.jetbrains.kotlinx", "kotlinx-coroutines-test", "1.5.1")
testImplementation("com.willowtreeapps.assertk", "assertk-jvm", "0.24")
}

// Kotlin dsl
Expand All @@ -126,11 +116,6 @@ tasks {
}
}

withType<Detekt> {
// Target version of the generated JVM bytecode. It is used for type resolution.
this.jvmTarget = "1.8"
}

generateGrammarSource {
outputDirectory =
File("${project.buildDir}/generated-src/antlr/main/de/nycode/bankobot/variables")
Expand All @@ -141,3 +126,20 @@ tasks {
useJUnitPlatform()
}
}

detekt {
autoCorrect = true
}

subprojects {
apply(plugin = "io.gitlab.arturbosch.detekt")

tasks {
withType<Detekt>().configureEach {
// Target version of the generated JVM bytecode. It is used for type resolution.
this.jvmTarget = "15"

autoCorrect = true
}
}
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.1.1-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
4 changes: 1 addition & 3 deletions src/main/kotlin/de/nycode/bankobot/BankoBot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ import io.ktor.client.engine.cio.*
import io.ktor.client.features.*
import io.ktor.client.features.json.*
import io.ktor.client.features.json.serializer.*
import io.ktor.util.*
import kapt.kotlin.generated.configure
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.toList
Expand All @@ -88,7 +87,6 @@ import org.litote.kmongo.serialization.registerSerializer
import kotlin.coroutines.CoroutineContext
import kotlin.time.Duration
import kotlin.time.ExperimentalTime
import kotlin.time.seconds

object BankoBot : CoroutineScope {

Expand All @@ -101,7 +99,7 @@ object BankoBot : CoroutineScope {
private val logger = KotlinLogging.logger { }

@Suppress("MagicNumber")
@OptIn(KtorExperimentalAPI::class, ExperimentalTime::class)
@OptIn(ExperimentalTime::class)
val httpClient = HttpClient(CIO) {
install(JsonFeature) {
val json = kotlinx.serialization.json.Json {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,13 @@ import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withTimeout
import kotlin.time.Duration
import kotlin.time.ExperimentalTime
import kotlin.time.seconds

@Suppress("MagicNumber")
@OptIn(ExperimentalTime::class)
private val timeout = 5.seconds
private val timeout = Duration.seconds(5)

/**
* Implementation of [ContextConverter] which sends typing before replying to a command.
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/de/nycode/bankobot/command/Context.kt
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ interface EditableMessage {

fun KordCommandEvent.toContext(): Context = DelegatedKordCommandContext(this)

private class DelegatedKordCommandContext(private val delegate: KordCommandEvent) : Context, KordEvent by delegate {
internal class DelegatedKordCommandContext(private val delegate: KordCommandEvent) : Context, KordEvent by delegate {
override val command: Command<*> get() = delegate.command
override val commands: Map<String, Command<*>> get() = delegate.commands
override val processor: CommandProcessor get() = delegate.processor
Expand Down
14 changes: 8 additions & 6 deletions src/main/kotlin/de/nycode/bankobot/command/ErrorHandlers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,16 @@ private val kordHandler = KordErrorHandler()

const val ERROR_MARKER = "[ERROR]"

private typealias ErrorHandlerInterface = ErrorHandler<MessageCreateEvent, MessageCreateEvent, Context>

private typealias RejectedArgument = ErrorHandler.RejectedArgument<MessageCreateEvent,
MessageCreateEvent,
Context>

@Suppress("UnnecessaryAbstractClass", "UNCHECKED_CAST")
abstract class AbstractErrorHandler :
ErrorHandler<MessageCreateEvent, MessageCreateEvent, Context> {
override suspend fun CommandProcessor.rejectArgument(
rejection: ErrorHandler.RejectedArgument<MessageCreateEvent,
MessageCreateEvent,
Context>,
) {
ErrorHandlerInterface {
override suspend fun CommandProcessor.rejectArgument(rejection: RejectedArgument) {
if (rejection.message == "Expected more input but reached end.") {
rejection.event.message.channel.createEmbed(Embeds.command(rejection.command, this))
} else with(kordHandler) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ import dev.kord.rest.builder.interaction.ApplicationCommandCreateBuilder
import dev.kord.rest.builder.interaction.ApplicationCommandsCreateBuilder
import dev.kord.x.commands.model.command.CommandBuilder

private val NAME_REGEX = "^[\\w-]{1,32}\$".toRegex()

/**
* Class containing all necessary information to register a slash command
* @property name the name
Expand All @@ -61,6 +63,8 @@ class SlashCommandCreator(
@OptIn(KordPreview::class)
fun CommandBuilder<*, *, *>.toSlashCommand(): SlashCommandCreator {
val description = description ?: "<unknown description>"
require(name.matches(NAME_REGEX)) { "$name is not a valid name" }
require(name.none(Char::isUpperCase)) { "$name has upper case characters" }
val creator: ApplicationCommandCreateBuilder.() -> Unit = {
arguments.forEach {
if (it is SlashArgument<*, *>) {
Expand Down
Loading

0 comments on commit c2218d9

Please sign in to comment.