Skip to content

Commit

Permalink
Fix Bugs (#40)
Browse files Browse the repository at this point in the history
* Fix Source Command Repo Link and fix typo (#17)

* Fixed repo link

* Fixed typo

* Fix production error handler (#19)

* Add twitch integration only in production (#21)

* Removed docsdex submodule (#22)

* Fix Error handler for slash commands (#23)

* Fix production error handler

* Make @NyCodeGHG happy

* Fix detekt issues

* Replace work around in favour of Kord release

* Respond with Embed on unkown tag (#25)

* Removed docsdex submodule

* Fix embed

* Remove unused method

* Variables in Tags (#26)

* Added calculation parser

* Added check task to ci

* Added calc command

* Added pow functionality

* Fixed formatting

* Added tag variables

* Added http variable

* Add tag with variable creation only by bot owners

* Added floating number support

* Fixed calc command

* Fixed exception handling

* Fix formatting

* Adds DCommand (#37)

* Adds DCommand

* Resolves problems

* Fix when docs missing (#39)

* Fix missing docs

* Fix formatting

Co-authored-by: Michael Rittmeister <michael@rittmeister.in>
Co-authored-by: warrior_zz <63170816+warriorzz@users.noreply.github.com>
  • Loading branch information
3 people authored Feb 2, 2021
1 parent 64e6507 commit 4f314c7
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 11 deletions.
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ services:
- "/etc/timezone:/etc/timezone:ro"
- "/etc/localtime:/etc/localtime:ro"
- ./logs:/user/app/logs
.env_file:
env_file:
- .env
ports:
- 127.0.0.1:8045:80
- 127.0.0.1:8045:80
30 changes: 27 additions & 3 deletions src/main/kotlin/de/nycode/bankobot/BankoBot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,30 +56,38 @@ import dev.kord.x.commands.model.prefix.or
import dev.kord.x.commands.model.processor.BaseEventHandler
import io.ktor.client.*
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.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.coroutineScope
import mu.KotlinLogging
import org.bson.UuidRepresentation
import org.litote.kmongo.coroutine.CoroutineCollection
import org.litote.kmongo.coroutine.CoroutineDatabase
import org.litote.kmongo.coroutine.coroutine
import org.litote.kmongo.reactivestreams.KMongo
import org.litote.kmongo.serialization.registerSerializer
import kotlin.coroutines.CoroutineContext
import kotlin.time.ExperimentalTime
import kotlin.time.seconds

object BankoBot : CoroutineScope {

private var initialized = false
override val coroutineContext: CoroutineContext = Dispatchers.IO + Job()

lateinit var availableDocs: List<String>
var availableDocs: List<String>? = null
private set

@OptIn(KtorExperimentalAPI::class)
private val logger = KotlinLogging.logger { }

@Suppress("MagicNumber")
@OptIn(KtorExperimentalAPI::class, ExperimentalTime::class)
val httpClient = HttpClient(CIO) {
install(JsonFeature) {
val json = kotlinx.serialization.json.Json {
Expand All @@ -88,6 +96,10 @@ object BankoBot : CoroutineScope {
}
serializer = KotlinxSerializer(json)
}
install(HttpTimeout) {
requestTimeoutMillis = 10.seconds.toLongMilliseconds()
connectTimeoutMillis = 10.seconds.toLongMilliseconds()
}
}

val repositories = Repositories()
Expand All @@ -107,15 +119,27 @@ object BankoBot : CoroutineScope {
suspend operator fun invoke() {
require(!initialized) { "Cannot initialize bot twice" }
initialized = true
loadJavadocs()

initializeDatabase()

kord = Kord(Config.DISCORD_TOKEN)

availableDocs = DocDex.allJavadocs().map { it.names }.flatten()
initializeKord()
}

private suspend fun loadJavadocs(): Unit = coroutineScope {
runCatching {
availableDocs = DocDex.allJavadocs().map { it.names }.flatten()
}
.onSuccess {
logger.info("Successfully loaded available javadocs! (${availableDocs?.size})")
}
.onFailure {
logger.error("Unable to load available javadocs!")
}
}

@Suppress("MagicNumber")
private suspend fun initializeDatabase() {
registerSerializer(SnowflakeSerializer)
Expand Down
52 changes: 52 additions & 0 deletions src/main/kotlin/de/nycode/bankobot/commands/general/DCommand.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* MIT License
*
* Copyright (c) 2021 BankoBot Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

package de.nycode.bankobot.commands.general

import de.nycode.bankobot.command.command
import de.nycode.bankobot.command.description
import de.nycode.bankobot.commands.GeneralModule
import dev.kord.core.behavior.channel.MessageChannelBehavior
import dev.kord.core.behavior.channel.createEmbed
import dev.kord.x.commands.annotation.AutoWired
import dev.kord.x.commands.annotation.ModuleName
import dev.kord.x.commands.model.command.invoke

@ModuleName(GeneralModule)
@AutoWired
fun dCommand() = command("dddd") {
alias("d", "dd", "ddd")
description("They don't know :pepeLaugh:")

invoke {
channel.specificCommand()
}
}

private suspend fun MessageChannelBehavior.specificCommand() {
createEmbed {
title = "Imagine using xd in 2k21... oh wait"
}
}
28 changes: 22 additions & 6 deletions src/main/kotlin/de/nycode/bankobot/commands/general/DocsCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ import dev.kord.x.commands.model.command.invoke
const val DocsModule = "Documentation"

private fun <CONTEXT> Argument<String, CONTEXT>.docsFilter() = filter { doc ->
if (doc in BankoBot.availableDocs) {

if (BankoBot.availableDocs == null) {
return@filter FilterResult
.Fail("Die verfügbaren Docs konnten leider nicht geladen werden! Versuche es später erneut!")
}

if (BankoBot.availableDocs?.contains(doc) == true) {
FilterResult.Pass
} else {
FilterResult.Fail("Dieses Doc is unbekannt. (Siehe `xd list-docs`)")
Expand Down Expand Up @@ -91,18 +97,28 @@ fun allDocsCommand() = command("alldocs") {
alias("all-docs", "list-docs", "listdocs", "availabledocs", "available-docs")

invoke {
respondEmbed(Embeds.info(
"Verfügbare Dokumentationen!",
BankoBot.availableDocs.format()
))

val embed = if (BankoBot.availableDocs == null) {
Embeds.error(
"Keine Docs verfügbar!",
"Die Docs konnten leider nicht geladen werden! Versuche es später erneut!"
)
} else {
Embeds.info(
"Verfügbare Dokumentationen!",
BankoBot.availableDocs?.format()
)
}

respondEmbed(embed)
}
}

@AutoWired
@ModuleName(DocsModule)
fun docsCommand() = command("docs") {
description("Zeigt Javadoc in Discord an")
alias("doc", "d")
alias("doc")

invoke(
JavaDocArgument,
Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/de/nycode/bankobot/docdex/DocDex.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@ import de.nycode.bankobot.config.Config
import io.ktor.client.request.*
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlin.time.ExperimentalTime

object DocDex {

/**
* Retrieves a list of all available javadocs.
*/
@OptIn(ExperimentalTime::class)
@Suppress("MagicNumber")
suspend fun allJavadocs(): List<JavaDoc> = BankoBot.httpClient.get(Config.DOCDEX_URL) {
url {
path("javadocs")
Expand Down

0 comments on commit 4f314c7

Please sign in to comment.