1
0

hopefully running

This commit is contained in:
Jannik Reimers 2024-10-28 15:20:23 +01:00
parent 629a5cc746
commit b62c04f77b
Signed by: jansel
GPG Key ID: 39C62D7D5233CFD0
9 changed files with 73 additions and 82 deletions

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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" }

View File

@ -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()
}

View File

@ -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<ReadyEvent> {
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<GuildMessageChannel>(tchannelid)
onlineLog?.createMessage("${it.channel} is now live at https://twitch.tv/${it.channel}")
}
}
}
}
}
}
}

View File

@ -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())

View File

@ -1,5 +1,4 @@
import ch.qos.logback.core.joran.spi.ConsoleTarget
import ch.qos.logback.core.ConsoleAppender
def environment = System.getenv("ENVIRONMENT") ?: "production"