-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
100 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.gradle | ||
.idea/ | ||
build | ||
.envrc.local |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 3 additions & 5 deletions
8
apps/app/src/main/kotlin/com/agilogy/timetracking/app/ApiServer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
apps/app/src/main/kotlin/com/agilogy/timetracking/app/BasicBot.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
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 | ||
|
||
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() | ||
|
||
// Set the activity to "I'm Ready" | ||
jda.presence.activity = Activity.playing("Agilogy school") | ||
} | ||
class BasicBot |
45 changes: 45 additions & 0 deletions
45
apps/app/src/main/kotlin/com/agilogy/timetracking/app/EventsListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
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) | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
apps/app/src/main/kotlin/com/agilogy/timetracking/app/Main.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.agilogy.timetracking.app | ||
|
||
import com.agilogy.heroku.postgres.loadHerokuPostgresConfig | ||
|
||
fun main() = app { | ||
val (jdbcUrl, username, password) = loadHerokuPostgresConfig() | ||
|
||
val timeTrackingApp = timeTrackingApp(jdbcUrl, username, password) | ||
|
||
startBot(timeTrackingApp) | ||
startApiServer(timeTrackingApp) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters