Skip to content

Commit

Permalink
feat: Start discord bot on app start
Browse files Browse the repository at this point in the history
Also it returns projects when message received
  • Loading branch information
manelp committed Mar 22, 2024
1 parent de3be8f commit 4f1983b
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 46 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.gradle
.idea/
build
.envrc.local
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ package com.agilogy.timetracking.app

import arrow.continuations.ktor.server
import com.agilogy.heroku.postgres.loadHerokuPostgresConfig
import com.agilogy.timetracking.domain.TimeTrackingApp
import com.agilogy.timetracking.driveradapters.httpapi.TimeTrackingApi
import io.ktor.server.netty.Netty
import kotlinx.coroutines.awaitCancellation
import kotlin.time.Duration.Companion.seconds

fun main() = app {
fun startApiServer(timeTrackingApp: TimeTrackingApp) = app {
val port = System.getenv("PORT")?.let { it.toInt() } ?: 8080
val (jdbcUrl, username, password) = loadHerokuPostgresConfig()

val timeTrackingApi = TimeTrackingApi(timeTrackingApp(jdbcUrl, username, password))

val timeTrackingApi = TimeTrackingApi(timeTrackingApp)
server(Netty, port = port, host = "0.0.0.0", preWait = 5.seconds) {
timeTrackingApi.routes()
}
Expand Down
39 changes: 22 additions & 17 deletions apps/app/src/main/kotlin/com/agilogy/timetracking/app/BasicBot.kt
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
package com.agilogy.timetracking.app

import com.agilogy.timetracking.domain.TimeTrackingApp
import net.dv8tion.jda.api.JDABuilder
import net.dv8tion.jda.api.entities.Activity
import net.dv8tion.jda.api.interactions.commands.OptionType
import net.dv8tion.jda.api.interactions.commands.build.Commands

class BasicBot {

companion object {
fun startBot(timeTrackingApp: TimeTrackingApp) = app {
val token = System.getenv("DISCORD_BOT_TOKEN")

//Create a new JDA instance
val jda = JDABuilder.createDefault(token)
.addEventListeners(EventsListener(timeTrackingApp))
.build()

//Register our first command /ping (don't use this method to often ;))
jda.updateCommands().addCommands(
Commands.slash("ping2", "Repeats messages back to you.")
.addOption(OptionType.STRING, "message", "The message to repeat.")).queue()

jda.updateCommands().addCommands(
Commands.slash("projects", "Returns the list of projects configured.")).queue()

@JvmStatic
fun main(args: Array<String>) {
val token = ""

//Create a new JDA instance
val jda = JDABuilder.createDefault(token)
.addEventListeners(PingCommand())
.build()

//Register our first command /ping (don't use this method to often ;))
jda.updateCommands().addCommands(
Commands.slash("ping2", "Repeats messages back to you.")
.addOption(OptionType.STRING, "message", "The message to repeat.")).queue()
//Set the activity to "I'm Ready"
jda.presence.activity = Activity.playing("Agilogy school")
}
class BasicBot {



//Set the activity to "I'm Ready"
jda.presence.activity = Activity.playing("Hola Agilogy")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.agilogy.timetracking.app

import com.agilogy.timetracking.domain.TimeTrackingApp
import kotlinx.coroutines.runBlocking
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
import net.dv8tion.jda.api.events.message.MessageReceivedEvent
import net.dv8tion.jda.api.hooks.ListenerAdapter
import org.slf4j.Logger
import org.slf4j.LoggerFactory

class EventsListener(private val timeTrackingApp: TimeTrackingApp) : ListenerAdapter() {

private val logger: Logger = LoggerFactory.getLogger(javaClass)

//This gets called when a slash command gets used.
override fun onSlashCommandInteraction(event: SlashCommandInteractionEvent) {

//Check if this is our /ping command
if (event.name == "ping2") {
logger.info("Command /ping got used")

//Reply to the user
val startTime = System.currentTimeMillis()
event.reply("Ping ...").setEphemeral(true).queue {
it.editOriginal("Pong: ${System.currentTimeMillis() - startTime}ms").queue()
}
}
}

override fun onMessageReceived(event: MessageReceivedEvent) = runBlocking {

try {
logger.info("Message received: ${event.message.contentRaw} by ${event.author}")

if (!event.author.isBot){
val response = if (event.message.contentRaw.trim().lowercase() == "projectes") {
timeTrackingApp.listProjects().joinToString(", ") { it.name }
} else {
"Hola ${event.author.name}! I don't understand."
}
event.channel.sendMessage(response).queue()
}
} catch (e: Exception) {
logger.error("Unexpected exception", e)
}

}
}
14 changes: 14 additions & 0 deletions apps/app/src/main/kotlin/com/agilogy/timetracking/app/Main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.agilogy.timetracking.app

import com.agilogy.heroku.postgres.loadHerokuPostgresConfig
import com.agilogy.timetracking.driveradapters.httpapi.TimeTrackingApi

fun main() = app {

val (jdbcUrl, username, password) = loadHerokuPostgresConfig()

val timeTrackingApp = timeTrackingApp(jdbcUrl, username, password)

startBot(timeTrackingApp)
startApiServer(timeTrackingApp)
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ interface TimeTrackingApp {

suspend fun listTimeEntries(dateRange: ClosedRange<LocalDate>, developer: DeveloperName?):
List<Tuple5<DeveloperName, ProjectName, LocalDate, ClosedRange<LocalTime>, ZoneId>>

suspend fun listProjects(): List<ProjectName>
}

@JvmInline
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,6 @@ class TimeTrackingAppPrd(private val timeEntriesRepository: TimeEntriesRepositor
res
}
}

override suspend fun listProjects(): List<ProjectName> = listOf(ProjectName("Agilogy school"))
}

0 comments on commit 4f1983b

Please sign in to comment.