-
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.
feat: Start discord bot on app start
Also it returns projects when message received
- Loading branch information
Showing
8 changed files
with
92 additions
and
46 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
39 changes: 22 additions & 17 deletions
39
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 |
---|---|---|
@@ -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") | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
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,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
14
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,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) | ||
} |
25 changes: 0 additions & 25 deletions
25
apps/app/src/main/kotlin/com/agilogy/timetracking/app/PingCommand.kt
This file was deleted.
Oops, something went wrong.
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