From 49ca19c0390d7313cf2252114b76249a9afb1933 Mon Sep 17 00:00:00 2001 From: Arthur Melton Date: Sat, 6 May 2023 20:02:53 -0400 Subject: [PATCH 1/3] add swap command --- .../de/bigboot/ggtools/fang/CommandContext.kt | 2 + .../de/bigboot/ggtools/fang/commands/Root.kt | 2 + .../ggtools/fang/commands/match/Match.kt | 40 +++++++++++++++++++ .../fang/service/QueueMessageService.kt | 9 +++++ 4 files changed, 53 insertions(+) create mode 100644 src/main/kotlin/de/bigboot/ggtools/fang/commands/match/Match.kt diff --git a/src/main/kotlin/de/bigboot/ggtools/fang/CommandContext.kt b/src/main/kotlin/de/bigboot/ggtools/fang/CommandContext.kt index 1854b03..5fcbe0a 100644 --- a/src/main/kotlin/de/bigboot/ggtools/fang/CommandContext.kt +++ b/src/main/kotlin/de/bigboot/ggtools/fang/CommandContext.kt @@ -2,6 +2,7 @@ package de.bigboot.ggtools.fang import discord4j.core.`object`.entity.Guild import discord4j.core.`object`.entity.Message +import discord4j.core.`object`.entity.User import discord4j.core.`object`.entity.channel.MessageChannel import kotlinx.coroutines.reactive.awaitSingle import org.koin.core.component.KoinComponent @@ -16,6 +17,7 @@ data class CommandContext( suspend fun channel(): MessageChannel = message.channel.awaitSingle() suspend fun guild(): Guild = message.guild.awaitSingle() + suspend fun author(): User = message.authorAsMember.awaitSingle() class Arguments(private val arguments: Map) { operator fun get(key: String) = arguments.getValue(key) diff --git a/src/main/kotlin/de/bigboot/ggtools/fang/commands/Root.kt b/src/main/kotlin/de/bigboot/ggtools/fang/commands/Root.kt index 6a11311..e2567bb 100644 --- a/src/main/kotlin/de/bigboot/ggtools/fang/commands/Root.kt +++ b/src/main/kotlin/de/bigboot/ggtools/fang/commands/Root.kt @@ -5,6 +5,7 @@ import de.bigboot.ggtools.fang.CommandGroupSpec import de.bigboot.ggtools.fang.commands.admin.Admin import de.bigboot.ggtools.fang.commands.queue.Queue import de.bigboot.ggtools.fang.commands.server.Server +import de.bigboot.ggtools.fang.commands.match.Match import de.bigboot.ggtools.fang.utils.createEmbedCompat import de.bigboot.ggtools.fang.utils.formatCommandHelp import de.bigboot.ggtools.fang.utils.formatCommandTree @@ -15,6 +16,7 @@ class Root : CommandGroupSpec("", "") { group(Admin()) group(Queue()) group(Server()) + group(Match()) command("help", "show this help") { onCall { diff --git a/src/main/kotlin/de/bigboot/ggtools/fang/commands/match/Match.kt b/src/main/kotlin/de/bigboot/ggtools/fang/commands/match/Match.kt new file mode 100644 index 0000000..c142cf6 --- /dev/null +++ b/src/main/kotlin/de/bigboot/ggtools/fang/commands/match/Match.kt @@ -0,0 +1,40 @@ +package de.bigboot.ggtools.fang.commands.match + +import de.bigboot.ggtools.fang.CommandGroupBuilder +import de.bigboot.ggtools.fang.CommandGroupSpec +import de.bigboot.ggtools.fang.Config +import de.bigboot.ggtools.fang.service.MatchService +import de.bigboot.ggtools.fang.service.QueueMessageService +import de.bigboot.ggtools.fang.utils.* +import discord4j.common.util.Snowflake +import kotlinx.coroutines.reactive.awaitSingle +import org.koin.core.component.inject + +class Match : CommandGroupSpec("match", "Commands for matches") { + private val matchService by inject() + private val queueMessageService by inject() + + override val build: CommandGroupBuilder.() -> Unit = { + command("swap", "shows the swap commands for the server") { + onCall { + val currentServer = queueMessageService.findUser(author().id.asLong()); + + if (currentServer != null && currentServer.openUrl != null) { + channel().createMessageCompat { + addEmbedCompat { + title("Swap commands") + description("Leiren: `${currentServer.openUrl}?team=0`\nGrenn: `${currentServer.openUrl}?team=1`") + } + }.awaitSingle() + } + else { + channel().createMessageCompat { + addEmbedCompat { + description("You are not in a game with a server up") + } + }.awaitSingle() + } + } + } + } +} diff --git a/src/main/kotlin/de/bigboot/ggtools/fang/service/QueueMessageService.kt b/src/main/kotlin/de/bigboot/ggtools/fang/service/QueueMessageService.kt index 592c55c..0401110 100644 --- a/src/main/kotlin/de/bigboot/ggtools/fang/service/QueueMessageService.kt +++ b/src/main/kotlin/de/bigboot/ggtools/fang/service/QueueMessageService.kt @@ -397,6 +397,15 @@ class QueueMessageService : AutostartService, KoinComponent { )) } + fun findUser(user: Long): MatchRequest? { + matchReuests.forEach { (_, request) -> + if (request.pop.allPlayers.contains(user)) { + return request; + } + } + return null; + } + private suspend fun handleInteraction(event: ComponentInteractionEvent, button: ButtonJoin) { event.deferEdit().awaitSafe() matchService.join(button.queue, event.interaction.user.id.asLong()) From d300b23f4d47681086139adeb85f175873517f3b Mon Sep 17 00:00:00 2001 From: Arthur Melton Date: Sun, 7 May 2023 00:03:28 -0400 Subject: [PATCH 2/3] add 'open' to the start of swap commands --- src/main/kotlin/de/bigboot/ggtools/fang/commands/match/Match.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/de/bigboot/ggtools/fang/commands/match/Match.kt b/src/main/kotlin/de/bigboot/ggtools/fang/commands/match/Match.kt index c142cf6..fdfe658 100644 --- a/src/main/kotlin/de/bigboot/ggtools/fang/commands/match/Match.kt +++ b/src/main/kotlin/de/bigboot/ggtools/fang/commands/match/Match.kt @@ -23,7 +23,7 @@ class Match : CommandGroupSpec("match", "Commands for matches") { channel().createMessageCompat { addEmbedCompat { title("Swap commands") - description("Leiren: `${currentServer.openUrl}?team=0`\nGrenn: `${currentServer.openUrl}?team=1`") + description("Leiren: `open ${currentServer.openUrl}?team=0`\nGrenn: `open ${currentServer.openUrl}?team=1`") } }.awaitSingle() } From 929157ada72962d34697544cb63cb742438942b5 Mon Sep 17 00:00:00 2001 From: Arthur Melton Date: Sun, 7 May 2023 00:07:57 -0400 Subject: [PATCH 3/3] move if statement to inside addEmbedCompat --- .../bigboot/ggtools/fang/commands/match/Match.kt | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/de/bigboot/ggtools/fang/commands/match/Match.kt b/src/main/kotlin/de/bigboot/ggtools/fang/commands/match/Match.kt index fdfe658..a718664 100644 --- a/src/main/kotlin/de/bigboot/ggtools/fang/commands/match/Match.kt +++ b/src/main/kotlin/de/bigboot/ggtools/fang/commands/match/Match.kt @@ -19,21 +19,17 @@ class Match : CommandGroupSpec("match", "Commands for matches") { onCall { val currentServer = queueMessageService.findUser(author().id.asLong()); - if (currentServer != null && currentServer.openUrl != null) { - channel().createMessageCompat { - addEmbedCompat { + channel().createMessageCompat { + addEmbedCompat { + if (currentServer != null && currentServer.openUrl != null) { title("Swap commands") description("Leiren: `open ${currentServer.openUrl}?team=0`\nGrenn: `open ${currentServer.openUrl}?team=1`") } - }.awaitSingle() - } - else { - channel().createMessageCompat { - addEmbedCompat { + else { description("You are not in a game with a server up") } - }.awaitSingle() - } + } + }.awaitSingle() } } }