From b62c04f77b676630d0e60afb6c7c1752cbe9d8cb Mon Sep 17 00:00:00 2001 From: Jannik Reimers Date: Mon, 28 Oct 2024 15:20:23 +0100 Subject: [PATCH] hopefully running --- .env.example | 8 +-- README.md | 63 +------------------ build.gradle.kts | 2 + gradle.properties | 1 - gradle/libs.versions.toml | 10 ++- src/main/kotlin/dev/jansel/feixiao/App.kt | 16 ++--- .../jansel/feixiao/extensions/EventHooks.kt | 44 +++++++++++++ .../kotlin/dev/jansel/feixiao/utils/_Utils.kt | 10 +++ src/main/resources/logback.groovy | 1 - 9 files changed, 73 insertions(+), 82 deletions(-) create mode 100644 src/main/kotlin/dev/jansel/feixiao/extensions/EventHooks.kt create mode 100644 src/main/kotlin/dev/jansel/feixiao/utils/_Utils.kt diff --git a/.env.example b/.env.example index a77ad0b..3665c7f 100644 --- a/.env.example +++ b/.env.example @@ -1,7 +1,5 @@ -# THIS IS A TEMPLATE FILE. MAKE A COPY AND RENAME TO `.env` TO USE. - -# https://discord.com/developers TOKEN=BOT_TOKEN - -# ID of the server to use for testing TEST_SERVER=123 +TEST_CHANNEL=456 +TWITCH_CLIENT_ID=TWITCH_CLIENT_ID +TWITCH_CLIENT_SECRET=TWITCH_CLIENT_SECRET diff --git a/README.md b/README.md index eeae177..18b9af7 100644 --- a/README.md +++ b/README.md @@ -1,62 +1,3 @@ -# KordEx Bot Template +# Codename Feixiao -This repository contains a basic KordEx example bot for you to use as a dev.jansel.template for your own KordEx bots. This -includes the following: - -- A basic extension that allows you to slap other people, using both chat commands and slash commands. -- A basic bot configuration that enables slash commands and shows you how to conditionally provide a different - chat command prefix for different guilds. -- A Gradle Kotlin build script that uses the KordEx Gradle plugin and Detekt for linting (with a - fairly strict configuration) – this uses Gradle 7's new version catalogue feature, for easy configuration of - dependencies. -- GitHub CI scripts that build the bot and publish its artefacts. -- A reasonable `.gitignore` file, including one in the `.idea` folder that ignores files that you shouldn't commit – - if you're using IDEA yourself, you should install the Ignore plugin to handle changes to this for you. -- A Groovy-based Logback config, so you have reasonable logging out of the box. -- Automatic generation of a Dockerfile via a `createDockerFile` task, also run at build time. - -**Note:** This dev.jansel.template includes a `.editorconfig` file that defaults to using tabs for indentation in almost all file -types. This is because tabs are more accessible for the blind, or those with impaired vision. We won't accept -feedback or PRs targeting this approach, though you can always change it in your projects. - -## Potential Changes - -- The `.yml` files in `.github/` are used to configure GitHub apps. If you're not using them, you can remove them. -- The provided `LICENSE` file contains The Unlicense, which makes this repository public domain. You will probably want - to change this—we suggest looking at [Choose a License](https://choosealicense.com/) if you're not sure where to - start. -- In the `build.gradle.kts`: - - Set the `group` and `version` properties as appropriate. - - In the `kordEx` and `tasks.jar` block, update the main class path/name as appropriate. - - In the `kordEx` block, update the KordEx version if needed. -- In the `settings.gradle.kts`, update the name of the root project as appropriate. -- The bundled Detekt config is pretty strict—you can check over `detekt.yml` if you want to change it, but you need to - follow the to-dos in that file regardless. -- The Logback configuration is in `src/main/resources/logback.groovy`. If the logging setup doesn't suit, you can change - it there. - -## Bundled Bot - -- `App.kt` includes a basic bot, which uses environment variables (or variables in a `.env` file) for the testing guild - ID (`TEST_SERVER`) and the bot's token (`TOKEN`). You can specify these either directly as environment variables, or - as `KEY=value` pairs in a file named `.env`. Some example code is also included that shows one potential way of - providing different command prefixes for different servers. -- `TestExtension.kt` includes an example extension that creates a `slap` command - this command works as both a - message command and slash command, and allows you to slap other users with whatever you wish, defaulting to a - `large, smelly trout`. - -To test the bot, we recommend using a `.env` file that looks like the following: - -```dotenv -TOKEN=abc... -TEST_SERVER=123... -``` - -Create this file, fill it out, and run the `run` gradle task for testing in development. - -## Further Reading - -To learn more about KordEx and how to work with it, [please browse the documentation](https://docs.kordex.dev). - -For more information on the KordEx Gradle plugin and what you can do with it, -[please read this README](https://github.com/Kord-Extensions/gradle-plugins#kordex-plugin). +Sourcefiles for codename Feixiao diff --git a/build.gradle.kts b/build.gradle.kts index f8e8218..e0d72b7 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -18,6 +18,8 @@ dependencies { implementation(libs.kotlin.stdlib) implementation(libs.kx.ser) + implementation(libs.kx.coroutines) + implementation(libs.twitch4j) // Logging dependencies implementation(libs.groovy) diff --git a/gradle.properties b/gradle.properties index bc68b39..ec46fe0 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,7 +1,6 @@ # Gradle props org.gradle.jvmargs=-XX:MaxMetaspaceSize=1024m org.gradle.parallel=true - kotlin.incremental=true ksp.incremental=false ksp.useKSP2=true diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 090ccfc..ffef6cc 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,20 +1,24 @@ [versions] -detekt = "1.23.6" # Note: Plugin versions must be updated in the settings.gradle.kts too kotlin = "2.0.20" # Note: Plugin versions must be updated in the settings.gradle.kts too groovy = "3.0.22" jansi = "2.4.1" -kx-ser = "1.7.2" +kx-ser = "1.7.3" logback = "1.5.7" logback-groovy = "1.14.5" logging = "7.0.0" +twitch4j = "1.22.0" +events4j = "0.12.2" +kx-coroutines = "1.9.0" [libraries] -detekt = { module = "io.gitlab.arturbosch.detekt:detekt-formatting", version.ref = "detekt" } groovy = { module = "org.codehaus.groovy:groovy", version.ref = "groovy" } jansi = { module = "org.fusesource.jansi:jansi", version.ref = "jansi" } kotlin-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib-jdk8" } kx-ser = { module = "org.jetbrains.kotlinx:kotlinx-serialization-core", version.ref = "kx-ser" } +kx-coroutines = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kx-coroutines" } logback = { module = "ch.qos.logback:logback-classic", version.ref = "logback" } logback-groovy = { module = "io.github.virtualdogbert:logback-groovy-config", version.ref = "logback-groovy" } logging = { module = "io.github.oshai:kotlin-logging", version.ref = "logging" } +twitch4j = { module = "com.github.twitch4j:twitch4j", version.ref = "twitch4j" } +events4j = { module = "com.github.philippheuer.events4j:events4j-handler-simple", version.ref = "events4j" } diff --git a/src/main/kotlin/dev/jansel/feixiao/App.kt b/src/main/kotlin/dev/jansel/feixiao/App.kt index 6698f4e..f959cb0 100644 --- a/src/main/kotlin/dev/jansel/feixiao/App.kt +++ b/src/main/kotlin/dev/jansel/feixiao/App.kt @@ -3,23 +3,17 @@ */ package dev.jansel.feixiao -import dev.kord.common.entity.Snowflake -import dev.kordex.core.ExtensibleBot -import dev.kordex.core.utils.env +import dev.jansel.feixiao.extensions.EventHooks import dev.jansel.feixiao.extensions.MessageEvents - -val TEST_SERVER_ID = Snowflake( - env("TEST_SERVER").toLong() // Get the test server ID from the env vars or a .env file -) - -private val TOKEN = env("TOKEN") // Get the bot' token from the env vars or a .env file +import dev.jansel.feixiao.utils.* +import dev.kordex.core.ExtensibleBot suspend fun main() { - val bot = ExtensibleBot(TOKEN) { + val bot = ExtensibleBot(token) { extensions { add(::MessageEvents) + add(::EventHooks) } } - bot.start() } diff --git a/src/main/kotlin/dev/jansel/feixiao/extensions/EventHooks.kt b/src/main/kotlin/dev/jansel/feixiao/extensions/EventHooks.kt new file mode 100644 index 0000000..67ba6a8 --- /dev/null +++ b/src/main/kotlin/dev/jansel/feixiao/extensions/EventHooks.kt @@ -0,0 +1,44 @@ +package dev.jansel.feixiao.extensions + +import com.github.twitch4j.TwitchClientBuilder +import com.github.twitch4j.events.ChannelGoLiveEvent +import dev.jansel.feixiao.utils.tchannelid +import dev.jansel.feixiao.utils.tserverid +import dev.jansel.feixiao.utils.twitchcid +import dev.jansel.feixiao.utils.twitchcs +import dev.kord.core.behavior.getChannelOf +import dev.kord.core.entity.channel.GuildMessageChannel +import dev.kord.core.event.gateway.ReadyEvent +import dev.kordex.core.extensions.Extension +import dev.kordex.core.extensions.event +import kotlinx.coroutines.launch +import kotlinx.coroutines.runBlocking + +class EventHooks : Extension() { + override val name = "eventhooks" + + override suspend fun setup() { + event { + action { + println("Bot is ready!") + + val twitchClient = TwitchClientBuilder.builder() + .withEnableHelix(true) + .withClientId(twitchcid) + .withClientSecret(twitchcs) + .build() + twitchClient.clientHelper.enableStreamEventListener("janselosu") + // Register a listener for when the channel goes live + twitchClient.eventManager.onEvent(ChannelGoLiveEvent::class.java) { + runBlocking { + launch { + val onlineLog = + kord.getGuildOrNull(tserverid)?.getChannelOf(tchannelid) + onlineLog?.createMessage("${it.channel} is now live at https://twitch.tv/${it.channel}") + } + } + } + } + } + } +} diff --git a/src/main/kotlin/dev/jansel/feixiao/utils/_Utils.kt b/src/main/kotlin/dev/jansel/feixiao/utils/_Utils.kt new file mode 100644 index 0000000..3b6413b --- /dev/null +++ b/src/main/kotlin/dev/jansel/feixiao/utils/_Utils.kt @@ -0,0 +1,10 @@ +package dev.jansel.feixiao.utils + +import dev.kord.common.entity.Snowflake +import dev.kordex.core.utils.env + +val twitchcid = env("TWITCH_CLIENT_ID") +val twitchcs = env("TWITCH_CLIENT_SECRET") +val token = env("TOKEN") +val tserverid = Snowflake(env("TEST_SERVER").toLong()) +val tchannelid = Snowflake(env("TEST_CHANNEL").toLong()) diff --git a/src/main/resources/logback.groovy b/src/main/resources/logback.groovy index 5f5bce9..5d33c83 100644 --- a/src/main/resources/logback.groovy +++ b/src/main/resources/logback.groovy @@ -1,5 +1,4 @@ import ch.qos.logback.core.joran.spi.ConsoleTarget -import ch.qos.logback.core.ConsoleAppender def environment = System.getenv("ENVIRONMENT") ?: "production"