first version

This commit is contained in:
InsanusMokrassar 2020-11-12 11:52:09 +06:00
parent 9b46894d99
commit e93704f511
13 changed files with 151 additions and 55 deletions

1
.gitignore vendored
View File

@ -10,4 +10,5 @@ build/
out/ out/
local.properties local.properties
config.json
secret.gradle secret.gradle

37
bot/build.gradle Normal file
View File

@ -0,0 +1,37 @@
plugins {
id 'org.jetbrains.kotlin.jvm'
id "org.jetbrains.kotlin.plugin.serialization"
id "org.jetbrains.kotlin.kapt"
id 'application'
}
project.group="$group"
project.version="$version"
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines_version"
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:$kotlin_serialisation_runtime_version"
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
implementation "org.jetbrains.exposed:exposed-jdbc:$kotlin_exposed_version"
implementation "dev.inmo:tgbotapi:$tgbotapi_version"
implementation "dev.inmo:micro_utils.repos.exposed:$microutils_version"
implementation "com.github.matfax.klassindex:library:$klassindex_version"
kapt "com.github.matfax.klassindex:processor:$klassindex_version"
implementation "org.xerial:sqlite-jdbc:$sqlite_version"
implementation project(":plugin")
}
application {
mainClassName = 'dev.inmo.plagubot.AppKt'
}
kapt {
arguments {
arg("com.github.matfax.klassindex.IndexSubclasses", "dev.inmo.plagubot.Plugin")
}
}

View File

@ -1,16 +1,20 @@
package dev.inmo.plagubot package dev.inmo.plagubot
import dev.inmo.micro_utils.coroutines.safelyWithoutExceptions import dev.inmo.micro_utils.coroutines.safelyWithoutExceptions
import dev.inmo.plagubot.config.Config
import dev.inmo.plagubot.config.configSerialFormat
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands
import dev.inmo.tgbotapi.extensions.api.telegramBot import dev.inmo.tgbotapi.extensions.api.telegramBot
import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.startGettingFlowsUpdatesByLongPolling import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.startGettingFlowsUpdatesByLongPolling
import dev.inmo.tgbotapi.types.botCommandsLimit import dev.inmo.tgbotapi.types.botCommandsLimit
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlinx.serialization.InternalSerializationApi
import java.io.File import java.io.File
/** /**
* This method by default expects one argument in [args] field: path to config * This method by default expects one argument in [args] field: path to config
*/ */
@InternalSerializationApi
suspend fun main(args: Array<String>) { suspend fun main(args: Array<String>) {
val (configPath) = args val (configPath) = args
val file = File(configPath) val file = File(configPath)
@ -37,5 +41,4 @@ suspend fun main(args: Array<String>) {
} }
} }
} }
} }

View File

@ -0,0 +1,23 @@
package dev.inmo.plagubot
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.updateshandlers.FlowsUpdatesFilter
import kotlinx.coroutines.CoroutineScope
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import org.jetbrains.exposed.sql.Database
@Serializable
@SerialName("Hello")
data class HelloPlugin(
val parameter: String
) : Plugin {
override suspend fun invoke(
bot: TelegramBot,
database: Database,
updatesFilter: FlowsUpdatesFilter,
scope: CoroutineScope
) {
println(parameter)
}
}

View File

@ -0,0 +1,42 @@
package dev.inmo.plagubot.config
import com.github.matfax.klassindex.KlassIndex
import dev.inmo.plagubot.Plugin
import kotlinx.serialization.*
import kotlinx.serialization.json.Json
import kotlinx.serialization.modules.*
import kotlin.reflect.KClass
@InternalSerializationApi
internal inline fun <T : Plugin> KClass<T>.includeIn(builder: PolymorphicModuleBuilder<Plugin>) = builder.subclass(this, serializer())
@InternalSerializationApi
internal val configSerialFormat: StringFormat
get() = Json {
ignoreUnknownKeys = true
serializersModule = SerializersModule {
polymorphic(Plugin::class) {
KlassIndex.getSubclasses(Plugin::class).flatMap { kclass ->
kclass.includeIn(this)
kclass.annotations.mapNotNull { it as? SerialName }.map {
it.value to kclass.serializer()
} + listOfNotNull(
kclass.simpleName ?.let {
it to kclass.serializer()
}
)
}.toMap().let {
default { requiredType ->
it[requiredType]
}
}
}
}
}
@Serializable
data class Config(
val plugins: List<@Contextual Plugin>,
val database: DatabaseConfig,
val botToken: String
)

View File

@ -9,7 +9,7 @@ import java.sql.Connection
@Serializable @Serializable
data class DatabaseConfig( data class DatabaseConfig(
val url: String, val url: String = "jdbc:sqlite:file:test?mode=memory&cache=shared",
val driver: String = JDBC::class.qualifiedName!!, val driver: String = JDBC::class.qualifiedName!!,
val username: String = "", val username: String = "",
val password: String = "", val password: String = "",
@ -37,12 +37,4 @@ data class DatabaseConfig(
database // init database database // init database
} }
} }
@Deprecated(
"Deprecated due to the replacement by lateinit database field with the same functionality",
ReplaceWith("database")
)
fun connect(): Database {
return database
}
} }

View File

@ -9,30 +9,11 @@ buildscript {
} }
} }
plugins { allprojects {
id 'org.jetbrains.kotlin.jvm' version "$kotlin_version" repositories {
id "org.jetbrains.kotlin.plugin.serialization" version "$kotlin_version"
id 'application'
}
project.group="dev.inmo"
project.version="$version"
repositories {
jcenter() jcenter()
mavenCentral() mavenCentral()
} mavenLocal()
maven { url 'https://jitpack.io' }
dependencies { }
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines_version"
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:$kotlin_serialisation_runtime_version"
implementation "dev.inmo:tgbotapi:$tgbotapi_version"
implementation "dev.inmo:micro_utils.repos.exposed:$microutils_version"
api "org.xerial:sqlite-jdbc:$sqlite_version"
}
application {
mainClassName = 'dev.inmo.plagubot.AppKt'
} }

View File

@ -7,8 +7,11 @@ kotlin.incremental=true
kotlin_version=1.4.10 kotlin_version=1.4.10
kotlin_coroutines_version=1.4.1 kotlin_coroutines_version=1.4.1
kotlin_serialisation_runtime_version=1.0.1 kotlin_serialisation_runtime_version=1.0.1
kotlin_exposed_version=0.28.1
tgbotapi_version=0.30.3 tgbotapi_version=0.30.3
microutils_version=0.3.2 microutils_version=0.3.2
klassindex_version=4.1.0-rc.1
sqlite_version=3.30.1 sqlite_version=3.30.1
group=dev.inmo
version=0.0.1 version=0.0.1

15
plugin/build.gradle Normal file
View File

@ -0,0 +1,15 @@
plugins {
id 'org.jetbrains.kotlin.jvm'
id "org.jetbrains.kotlin.plugin.serialization"
}
project.group="$group"
project.version="$version"
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:$kotlin_serialisation_runtime_version"
implementation "dev.inmo:tgbotapi:$tgbotapi_version"
implementation "dev.inmo:micro_utils.repos.exposed:$microutils_version"
}

View File

@ -1 +1,3 @@
include ":bot", ":plugin"
rootProject.name = 'plagubot' rootProject.name = 'plagubot'

View File

@ -1,19 +0,0 @@
package dev.inmo.plagubot
import dev.inmo.plagubot.config.DatabaseConfig
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.serialization.*
import kotlinx.serialization.json.Json
val configSerialFormat: StringFormat
get() = Json {
ignoreUnknownKeys = true
}
@Serializable
data class Config(
val plugins: List<@Contextual Plugin>,
val database: DatabaseConfig,
val botToken: String
)

16
template.config.json Normal file
View File

@ -0,0 +1,16 @@
{
"database": {
"url": "jdbc:sqlite:file:test?mode=memory&cache=shared IT IS JUST EXAMPLE",
"driver": "org.sqlite.JDBC",
"username": "OPTIONAL username",
"password": "OPTIONAL password",
"initAutomatically": false
},
"botToken": "1234567890:ABCDEFGHIJKLMNOP_qrstuvwxyz12345678",
"plugins": [
{
"type": "Hello",
"parameter": "Example"
}
]
}