Skip to content

Commit d3d6084

Browse files
committed
Remove JDA-KTX and continue work on modules
1 parent 1257665 commit d3d6084

File tree

11 files changed

+161
-49
lines changed

11 files changed

+161
-49
lines changed

glyph-bot/build.gradle.kts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ dependencies {
1818
implementation(libs.google.cloud.dialogflow)
1919
implementation(libs.google.cloud.storage)
2020
implementation(libs.jda)
21-
implementation(libs.jda.ktx)
2221
implementation(libs.jsoup)
23-
implementation(libs.kotlinx.coroutines.jdk8)
2422
implementation(libs.ktor.client.content.negotiation)
2523
implementation(libs.ktor.client.java)
2624
implementation(libs.ktor.serialization.kotlinx.json)
@@ -29,6 +27,7 @@ dependencies {
2927
implementation(libs.prettytime)
3028
implementation(libs.typesafe.config)
3129

30+
3231
testImplementation(kotlin("test"))
3332
}
3433

glyph-bot/resources/help.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
Hi!
2-
I'm %s,
3-
an [open source](https://github.com/yttrian/glyph.kt/) Discord bot created by %s.
1+
Hi! I'm %s, an [open source](https://github.com/yttrian/glyph.kt/) Discord bot created by Throudin.
42

53
I use machine learning to process natural language requests you
64
give to me to the best of my current trained ability.
75

86
To see what I can do, be sure to check out my
97
[skills](https://glyph.yttr.org/skills)
108
and suggest new ones you'd like to see, in the
11-
[official server](https://discord.gg/Meet9mF)
12-
or via the anonymous feedback skill.
9+
[official server](https://glyph.yttr.org/server)
10+
or via the feedback command.

glyph-bot/src/Glyph.kt

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ package org.yttr.glyph.bot
22

33
import com.typesafe.config.Config
44
import com.typesafe.config.ConfigFactory
5-
import dev.minn.jda.ktx.interactions.commands.updateCommands
6-
import dev.minn.jda.ktx.jdabuilder.default
7-
import dev.minn.jda.ktx.jdabuilder.intents
85
import io.lettuce.core.RedisClient
6+
import net.dv8tion.jda.api.JDABuilder
97
import net.dv8tion.jda.api.requests.GatewayIntent
108
import org.yttr.glyph.bot.modules.HelpModule
9+
import org.yttr.glyph.bot.modules.SnowstampModule
1110
import org.yttr.glyph.bot.skills.config.ConfigDirector
1211
import org.yttr.glyph.shared.pubsub.redis.RedisAsync
1312

@@ -33,21 +32,28 @@ object Glyph {
3332
redisUrl = conf.getString("data.redis-url")
3433
)
3534

36-
val modules = listOf(HelpModule())
35+
val modules = listOf(
36+
HelpModule(),
37+
SnowstampModule()
38+
)
3739

3840
/**
3941
* Build the bot and run
4042
*/
4143
fun run() {
42-
val jda = default(token = conf.getString("discord-token")) {
43-
intents += GatewayIntent.GUILD_MESSAGES
44-
}
44+
val jda = JDABuilder.createDefault(conf.getString("discord-token"))
45+
.enableIntents(GatewayIntent.GUILD_MEMBERS)
46+
.build()
4547

46-
jda.updateCommands {
47-
for (module in modules) {
48-
module.register(jda, this)
49-
}
48+
val commands = jda.updateCommands()
49+
50+
for (module in modules) {
51+
jda.addEventListener(module)
52+
module.register()
53+
commands.addCommands(module.commands())
5054
}
55+
56+
commands.queue()
5157
}
5258
}
5359

glyph-bot/src/Resources.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.yttr.glyph.bot
2+
3+
object Resources {
4+
fun readText(path: String): String {
5+
val resource = Resources::class.java.getResource(path)
6+
7+
requireNotNull(resource) { "Resource $path not found!" }
8+
9+
return resource.readText()
10+
}
11+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.yttr.glyph.bot.jda
2+
3+
fun interface ElementAppender<T> {
4+
operator fun plusAssign(element: T)
5+
}

glyph-bot/src/jda/ReplyBuilder.kt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.yttr.glyph.bot.jda
2+
3+
import net.dv8tion.jda.api.components.MessageTopLevelComponent
4+
import net.dv8tion.jda.api.entities.MessageEmbed
5+
import net.dv8tion.jda.api.interactions.callbacks.IReplyCallback
6+
import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction
7+
8+
class ReplyBuilder(private val action: ReplyCallbackAction) {
9+
val embeds = ElementAppender<MessageEmbed> { action.addEmbeds(it) }
10+
val components = ElementAppender<MessageTopLevelComponent> { action.addComponents(it) }
11+
12+
var content: String?
13+
get() = action.content
14+
set(value) {
15+
action.setContent(value)
16+
}
17+
18+
var ephemeral: Boolean = false
19+
set(value) {
20+
action.setEphemeral(value)
21+
field = value
22+
}
23+
}
24+
25+
26+
fun IReplyCallback.buildReply(content: String = "", block: ReplyBuilder.() -> Unit): ReplyCallbackAction {
27+
val action = reply(content)
28+
ReplyBuilder(action).block()
29+
return action
30+
}
Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,48 @@
11
package org.yttr.glyph.bot.modules
22

3-
import dev.minn.jda.ktx.events.onCommand
4-
import dev.minn.jda.ktx.interactions.commands.slash
5-
import dev.minn.jda.ktx.messages.Embed
6-
import dev.minn.jda.ktx.messages.reply_
7-
import net.dv8tion.jda.api.JDA
3+
import net.dv8tion.jda.api.EmbedBuilder
4+
import net.dv8tion.jda.api.components.actionrow.ActionRow
5+
import net.dv8tion.jda.api.components.buttons.Button
6+
import net.dv8tion.jda.api.components.buttons.ButtonStyle
7+
import net.dv8tion.jda.api.entities.emoji.Emoji
88
import net.dv8tion.jda.api.events.interaction.command.GenericCommandInteractionEvent
9-
import net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction
9+
import net.dv8tion.jda.api.interactions.commands.build.Commands
10+
import org.yttr.glyph.bot.Resources
11+
import org.yttr.glyph.bot.jda.buildReply
1012

11-
class HelpModule : Module {
12-
override fun register(jda: JDA, commands: CommandListUpdateAction) {
13-
commands.slash(name = "help", description = "Get help using Glyph")
14-
15-
jda.onCommand(name = "help") { event ->
16-
help(event)
17-
}
13+
class HelpModule : Module() {
14+
override fun register() {
15+
onCommand("help") { event -> help(event) }
1816
}
1917

18+
override fun commands() = listOf(
19+
Commands.slash("help", "Get help using Glyph")
20+
)
21+
2022
fun help(event: GenericCommandInteractionEvent) {
2123
val name = event.jda.selfUser.name
22-
val embed = Embed {
23-
title = "Help"
24-
color = EMBED_COLOR
25-
}
2624

27-
event.reply_(embeds = listOf(embed), ephemeral = true).queue()
25+
event.buildReply {
26+
ephemeral = true
27+
28+
embeds += EmbedBuilder()
29+
.setTitle("$name Help")
30+
.setColor(EMBED_COLOR)
31+
.setDescription(Resources.readText("help.md").format(name))
32+
.build()
33+
34+
components += ActionRow.of(
35+
linkButton("https://glyph.yttr.org/skills", "Skills", "🕺"),
36+
linkButton("https://glyph.yttr.org/config", "Configure", "⚙️"),
37+
linkButton("https://ko-fi.com/throudin", "Buy me a Ko-fi", "")
38+
)
39+
}.queue()
2840
}
2941

3042
companion object {
3143
private const val EMBED_COLOR = 0x4687E5
44+
45+
private fun linkButton(url: String, label: String, emoji: String) =
46+
Button.of(ButtonStyle.LINK, url, label, Emoji.fromUnicode(emoji))
3247
}
3348
}

glyph-bot/src/modules/Module.kt

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
package org.yttr.glyph.bot.modules
22

3-
import net.dv8tion.jda.api.JDA
4-
import net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction
5-
6-
interface Module {
7-
/**
8-
* Use [commands] not [jda] to register commands!
9-
*/
10-
fun register(jda: JDA, commands: CommandListUpdateAction)
3+
import net.dv8tion.jda.api.events.interaction.command.GenericCommandInteractionEvent
4+
import net.dv8tion.jda.api.hooks.ListenerAdapter
5+
import net.dv8tion.jda.api.interactions.commands.build.CommandData
6+
7+
abstract class Module : ListenerAdapter() {
8+
private val commandListeners = mutableMapOf<String, (GenericCommandInteractionEvent) -> Unit>()
9+
10+
abstract fun register()
11+
12+
fun onCommand(name: String, function: (GenericCommandInteractionEvent) -> Unit) {
13+
commandListeners[name] = function
14+
}
15+
16+
override fun onGenericCommandInteraction(event: GenericCommandInteractionEvent) {
17+
commandListeners[event.name]?.invoke(event)
18+
}
19+
20+
open fun commands(): List<CommandData> = emptyList()
1121
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.yttr.glyph.bot.modules
2+
3+
import net.dv8tion.jda.api.EmbedBuilder
4+
import net.dv8tion.jda.api.events.interaction.command.GenericCommandInteractionEvent
5+
import net.dv8tion.jda.api.interactions.commands.OptionType
6+
import net.dv8tion.jda.api.interactions.commands.build.CommandData
7+
import net.dv8tion.jda.api.interactions.commands.build.Commands
8+
import net.dv8tion.jda.api.utils.TimeFormat
9+
import net.dv8tion.jda.api.utils.TimeUtil
10+
import org.yttr.glyph.bot.jda.buildReply
11+
import java.awt.Color
12+
13+
class SnowstampModule : Module() {
14+
override fun register() {
15+
onCommand("snowstamp") { event -> snowstamp(event) }
16+
}
17+
18+
override fun commands(): List<CommandData> = listOf(
19+
Commands.slash("snowstamp", "Get a timestamp from a snowflake ID")
20+
.addOption(OptionType.INTEGER, "snowflake", "The snowflake ID to get a timestamp for", true)
21+
)
22+
23+
fun snowstamp(event: GenericCommandInteractionEvent) {
24+
val snowflake = event.getOption("snowflake")?.asLong
25+
26+
if (snowflake == null) {
27+
event.buildReply("Please provide a valid snowflake ID!") { ephemeral = true }.queue()
28+
return
29+
}
30+
31+
val snowflakeTime = TimeUtil.getTimeCreated(snowflake)
32+
33+
event.buildReply {
34+
embeds += EmbedBuilder()
35+
.setTitle(snowflake.toString())
36+
.setDescription(TimeFormat.DATE_TIME_LONG.format(snowflakeTime))
37+
.setColor(Color.WHITE)
38+
.setFooter("Snowstamp")
39+
.setTimestamp(snowflakeTime)
40+
.build()
41+
}.queue()
42+
}
43+
}

glyph-shared/build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ dependencies {
1414
implementation(libs.exposed.jdbc)
1515

1616
compileOnly(libs.jda)
17-
implementation(libs.kotlinx.coroutines.jdk8)
1817
implementation(libs.kotlinx.serialization.core)
1918

2019
api(libs.lettuce.core)

0 commit comments

Comments
 (0)