Skip to content

Commit

Permalink
Add 'stop' and 'rebuild' subcommands to /gateway bot (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasgrose authored Aug 5, 2023
1 parent a80e981 commit dc97bfc
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 15 deletions.
1 change: 0 additions & 1 deletion .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/main/kotlin/com/rose/gateway/discord/bot/DiscordBot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class DiscordBot : KoinComponent {
/**
* Stops the Discord bot such that it can be restarted
*/
private suspend fun stop() {
suspend fun stop() {
botStatus = BotStatus.STOPPING

bot?.stop()
Expand Down
12 changes: 10 additions & 2 deletions src/main/kotlin/com/rose/gateway/minecraft/CommandRegistry.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,20 @@ object CommandRegistry : KoinComponent {

command("gateway") {
subcommand("bot") {
subcommand("rebuild") {
runner { context -> BotCommands.restart(context) }
}

subcommand("restart") {
runner { context -> BotCommands.restartBot(context) }
runner { context -> BotCommands.restart(context) }
}

subcommand("stop") {
runner { context -> BotCommands.stop(context) }
}

subcommand("status") {
runner { context -> BotCommands.botStatus(context) }
runner { context -> BotCommands.status(context) }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,65 @@ object BotCommands : KoinComponent {
private val pluginScope: PluginCoroutineScope by inject()

/**
* Command that restarts the discord bot
* Command that rebuilds the Discord bot
*
* @param context A command context without arguments
* @return Whether the command succeeded
*/
fun restartBot(context: CommandExecuteContext<NoArgs>): Boolean {
fun rebuild(context: CommandExecuteContext<NoArgs>): Boolean {
sendAndLogMessage(context.bukkit.sender, "Rebuilding the Discord bot. This may take a while...")

pluginScope.launch {
bot.rebuild()
sendAndLogMessage(
context.bukkit.sender,
if (bot.botStatus == BotStatus.RUNNING) {
"Discord bot restarted."
} else {
"Discord bot failed to restart. Check bot status for more info."
},
)
}

return true
}

/**
* Command that restarts the Discord bot
*
* @param context A command context without arguments
* @return Whether the command succeeded
*/
fun restart(context: CommandExecuteContext<NoArgs>): Boolean {
sendAndLogMessage(context.bukkit.sender, "Restarting the Discord bot...")

pluginScope.launch {
bot.restart()
sendAndLogMessage(
context.bukkit.sender,
if (bot.botStatus == BotStatus.RUNNING) {
"Discord bot restarted."
} else {
"Discord bot failed to restart. Check bot status for more info."
},
)
}

return true
}

if (bot.botStatus == BotStatus.RUNNING) {
sendAndLogMessage(context.bukkit.sender, "Discord bot restarted.")
} else {
sendAndLogMessage(
context.bukkit.sender,
"Discord bot failed to restart. Check bot status for more info.",
)
}
/**
* Stops the Discord bot
*
* @param context A command context without arguments
* @return Whether the command succeeded
*/
fun stop(context: CommandExecuteContext<NoArgs>): Boolean {
sendAndLogMessage(context.bukkit.sender, "Stopping the Discord bot...")

pluginScope.launch {
bot.stop()
sendAndLogMessage(context.bukkit.sender, "Discord bot stopped.")
}

return true
Expand All @@ -63,7 +103,7 @@ object BotCommands : KoinComponent {
* @param context A command context without arguments
* @return Whether the command succeeded
*/
fun botStatus(context: CommandExecuteContext<NoArgs>): Boolean {
fun status(context: CommandExecuteContext<NoArgs>): Boolean {
val status = bot.botStatus
context.bukkit.sender.sendMessage(
joinSpace(
Expand Down

0 comments on commit dc97bfc

Please sign in to comment.