Skip to content

Commit 832c5a9

Browse files
committed
Merge branch 'command-check-and-call-extras'
2 parents f58d99a + c931d0d commit 832c5a9

File tree

5 files changed

+55
-5
lines changed

5 files changed

+55
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ dependencies {
3030
implementation("net.dv8tion:JDA:4.2.1_253") {
3131
exclude(module = "opus-java") // optional, for if you don't plan to use voice chat
3232
}
33-
implementation("com.github.yttrian:koncorda:0.3.0")
33+
implementation("com.github.yttrian:koncorda:0.3.1")
3434
}
3535
```
3636

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
22

33
group = "org.yttr"
4-
version = "0.3.0"
4+
version = "0.3.1"
55

66
plugins {
77
id("tanvd.kosogor") version "1.0.10" apply true
Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
11
package org.yttr.koncorda.command
22

33
import kotlinx.coroutines.future.await
4+
import net.dv8tion.jda.api.MessageBuilder
5+
import net.dv8tion.jda.api.entities.Message
6+
import net.dv8tion.jda.api.entities.MessageEmbed
47
import net.dv8tion.jda.api.events.message.MessageReceivedEvent
58

69
data class CommandCall(val event: MessageReceivedEvent, val args: List<String>) {
710
/**
8-
* Respond to the event.
11+
* Respond to the event with text.
912
*/
1013
suspend fun MessageReceivedEvent.respond(content: String) {
11-
if (content.isNotBlank()) channel.sendMessage(content).submit().await()
14+
if (content.isNotBlank()) respond(MessageBuilder().setContent(content).build())
15+
}
16+
17+
/**
18+
* Respond to the event with an embed.
19+
*/
20+
suspend fun MessageReceivedEvent.respond(embed: MessageEmbed) {
21+
respond(MessageBuilder().setEmbed(embed).build())
22+
}
23+
24+
/**
25+
* Respond the the event with a complex message.
26+
*/
27+
suspend fun MessageReceivedEvent.respond(message: Message) {
28+
channel.sendMessage(message).submit().await()
1229
}
1330
}

koncorda/src/main/kotlin/command/check/CommandCheck.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,23 @@ import org.yttr.koncorda.command.CommandCall
55
interface CommandCheck {
66
suspend fun check(call: CommandCall): Boolean
77
}
8+
9+
infix fun CommandCheck.or(right: CommandCheck): CommandCheck = composite(this, right, Boolean::or)
10+
11+
infix fun CommandCheck.and(right: CommandCheck): CommandCheck = composite(this, right, Boolean::and)
12+
13+
operator fun CommandCheck.not(): CommandCheck = object : CommandCheck {
14+
override suspend fun check(call: CommandCall): Boolean = !this@not.check(call)
15+
}
16+
17+
private fun composite(
18+
left: CommandCheck,
19+
right: CommandCheck,
20+
predicate: (Boolean, Boolean) -> Boolean
21+
): CommandCheck {
22+
return object : CommandCheck {
23+
override suspend fun check(call: CommandCall): Boolean {
24+
return predicate(left.check(call), right.check(call))
25+
}
26+
}
27+
}

koncorda/src/test/kotlin/TestBot.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import command.HelpCommand
22
import command.TestCommand
33
import command.check.Useless
4+
import net.dv8tion.jda.api.MessageBuilder
45
import net.dv8tion.jda.api.requests.GatewayIntent
56
import org.yttr.koncorda.await
67
import org.yttr.koncorda.command.check.Impossible
8+
import org.yttr.koncorda.command.check.and
9+
import org.yttr.koncorda.command.check.not
10+
import org.yttr.koncorda.command.check.or
711
import org.yttr.koncorda.commands
812
import org.yttr.koncorda.koncorda
913
import org.yttr.koncorda.onReady
@@ -30,7 +34,16 @@ fun main() {
3034
event.respond("Hello ${event.author}!")
3135
}
3236
check(Useless) {
33-
leaf("world") { event.respond("Yay!") }
37+
leaf("world") { event.respond(MessageBuilder().setContent("Yay!").build()) }
38+
}
39+
check(Useless and Impossible) {
40+
leaf("everyone") { event.respond("Huh?") }
41+
}
42+
check(Useless and !Impossible) {
43+
leaf("everybody") { event.respond("Hmm.") }
44+
}
45+
check(Useless or Impossible) {
46+
leaf("everything") { event.respond("Hmm.") }
3447
}
3548
}
3649

0 commit comments

Comments
 (0)