Skip to content

Commit 8b669eb

Browse files
authored
Merge pull request #2 from Joshua154/updates
Updates
2 parents f03c6fc + e2f64ff commit 8b669eb

17 files changed

Lines changed: 967 additions & 26 deletions

File tree

build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
plugins {
22
kotlin("jvm") version "1.7.21"
3+
kotlin("plugin.serialization") version "1.4.20"
34
id("com.github.johnrengelman.shadow") version("7.1.2")
45
application
56
}
@@ -15,7 +16,7 @@ repositories {
1516
dependencies {
1617
implementation("net.dv8tion:JDA:5.0.0-alpha.22")
1718
implementation("org.reflections:reflections:0.10.2")
18-
implementation("mysql:mysql-connector-java:8.0.30")
19+
implementation("com.beust:klaxon:5.5")
1920
}
2021

2122
application {

src/main/kotlin/de/joshua/DiscordBot.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class DiscordBot {
3535

3636
val jda = builder.build().awaitReady()
3737

38-
val guild = jda.guilds.filter { it.id == "639126503409319946" }[0]
38+
val guild = jda.guilds.filter { it.id == "777522801368236043" }[0]
3939

4040
commandHandler.sendCommands(guild)
4141
// commandHandler.sendCommands(jda.guilds)
@@ -47,3 +47,4 @@ class DiscordBot {
4747
println("Bot is running")
4848
}
4949
}
50+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package de.joshua.api.buttons
22

33
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent
4+
import net.dv8tion.jda.api.interactions.components.ItemComponent
45

56
interface Button {
67

78
fun id(): String
89
fun onExecute(event: ButtonInteractionEvent)
10+
fun getButton(): ItemComponent
911
}

src/main/kotlin/de/joshua/api/commands/CommandHandler.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,17 @@ class CommandHandler() : ListenerAdapter() {
5454

5555
slashCommands.forEach {
5656
updateCommands.addCommands(it.commandData()).queueAfter(1, TimeUnit.SECONDS)
57-
println(it.commandData().name + " on " + guild.name)
57+
//println(it.commandData().name + " on " + guild.name)
5858
}
5959

6060
userContext.forEach {
6161
updateCommands.addCommands(it.commandData()).queueAfter(1, TimeUnit.SECONDS)
62-
println(it.commandData().name + " on " + guild.name)
62+
//println(it.commandData().name + " on " + guild.name)
6363
}
6464

6565
messageContext.forEach {
6666
updateCommands.addCommands(it.commandData()).queueAfter(1, TimeUnit.SECONDS)
67-
println(it.commandData().name + " on " + guild.name)
67+
//println(it.commandData().name + " on " + guild.name)
6868
}
6969

7070
updateCommands.queueAfter(5, TimeUnit.SECONDS)
@@ -79,17 +79,17 @@ class CommandHandler() : ListenerAdapter() {
7979

8080
slashCommands.forEach {
8181
updateCommands.addCommands(it.commandData()).queueAfter(1, TimeUnit.SECONDS)
82-
println(it.commandData().name + " on " + guild.name)
82+
//println(it.commandData().name + " on " + guild.name)
8383
}
8484

8585
userContext.forEach {
8686
updateCommands.addCommands(it.commandData()).queueAfter(1, TimeUnit.SECONDS)
87-
println(it.commandData().name + " on " + guild.name)
87+
//println(it.commandData().name + " on " + guild.name)
8888
}
8989

9090
messageContext.forEach {
9191
updateCommands.addCommands(it.commandData()).queueAfter(1, TimeUnit.SECONDS)
92-
println(it.commandData().name + " on " + guild.name)
92+
//println(it.commandData().name + " on " + guild.name)
9393
}
9494

9595
updateCommands.queueAfter(5, TimeUnit.SECONDS)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package de.joshua.dnd
2+
3+
import de.joshua.api.commands.slashcommands.SlashCommand
4+
import de.joshua.api.commands.slashcommands.SlashCommands
5+
import net.dv8tion.jda.api.EmbedBuilder
6+
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
7+
import net.dv8tion.jda.api.interactions.commands.OptionType
8+
import net.dv8tion.jda.api.interactions.commands.build.OptionData
9+
import net.dv8tion.jda.internal.interactions.CommandDataImpl
10+
import java.time.Instant
11+
12+
@SlashCommands
13+
class BodyPartCommand : SlashCommand {
14+
override fun commandData(): CommandDataImpl {
15+
return CommandDataImpl("bodypart", "roles a d100 Test")
16+
.addOptions(
17+
OptionData(OptionType.INTEGER, "times", "Amount of roles"),
18+
OptionData(OptionType.USER, "user", "User to roll for"),
19+
OptionData(OptionType.BOOLEAN, "private", "Roles a dice privately")
20+
)
21+
}
22+
23+
override fun onExecute(event: SlashCommandInteractionEvent) {
24+
val ephemeral = event.getOption("private") != null && event.getOption("private")?.asBoolean!!
25+
26+
var user = event.user
27+
if(event.getOption("user") != null){
28+
user = event.getOption("user")?.asUser!!
29+
}
30+
31+
if (event.getOption("times") != null && event.getOption("times")?.asInt!! > 1) {
32+
val rolls = List(event.getOption("times")?.asInt!!) { Dice.getBodyPart(Dice.rollDice()) }
33+
34+
val embed = EmbedBuilder()
35+
.setAuthor(
36+
user.name,
37+
null,
38+
user.avatarUrl
39+
)
40+
.setTimestamp(Instant.now())
41+
.setTitle("Rolled ${rolls.size} times")
42+
.setDescription(rolls.joinToString { "\n" + it})
43+
44+
event.replyEmbeds(embed.build()).setActionRow(Dice.getActionRow()).queue()
45+
} else {
46+
val embed = EmbedBuilder()
47+
.setAuthor(
48+
user.name,
49+
null,
50+
user.avatarUrl
51+
)
52+
.setTitle(Dice.getBodyPart(Dice.rollDice()))
53+
.setTimestamp(Instant.now())
54+
55+
event.replyEmbeds(embed.build()).setEphemeral(ephemeral).setActionRow(Dice.getActionRow()).queue()
56+
}
57+
}
58+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package de.joshua.dnd
2+
3+
import de.joshua.api.commands.slashcommands.SlashCommand
4+
import de.joshua.api.commands.slashcommands.SlashCommands
5+
import net.dv8tion.jda.api.EmbedBuilder
6+
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
7+
import net.dv8tion.jda.api.interactions.commands.Command
8+
import net.dv8tion.jda.api.interactions.commands.OptionType
9+
import net.dv8tion.jda.api.interactions.commands.build.OptionData
10+
import net.dv8tion.jda.internal.interactions.CommandDataImpl
11+
import java.time.Instant
12+
13+
@SlashCommands
14+
class CritCommand : SlashCommand {
15+
override fun commandData(): CommandDataImpl {
16+
return CommandDataImpl("crit", "crit")
17+
.addOptions(
18+
OptionData(OptionType.USER, "user", "User to roll for"),
19+
OptionData(OptionType.BOOLEAN, "private", "Roles a crit privately"),
20+
OptionData(OptionType.STRING, "location", "location").addChoices(
21+
Command.Choice("head", "head"),
22+
Command.Choice("arm", "arm"),
23+
Command.Choice("body", "body"),
24+
Command.Choice("leg", "leg")
25+
)
26+
)
27+
}
28+
29+
override fun onExecute(event: SlashCommandInteractionEvent) {
30+
val ephemeral = event.getOption("private") != null && event.getOption("private")?.asBoolean!!
31+
32+
var user = event.user
33+
if(event.getOption("user") != null){
34+
user = event.getOption("user")?.asUser!!
35+
}
36+
37+
var crit: CritEntry? = Dice.getCrit()
38+
if(event.getOption("location") != null){
39+
crit = Dice.getCrit(getBodyPartNumber(event.getOption("location")?.asString!!))
40+
}
41+
if(crit == null){
42+
event.reply("Error")
43+
return
44+
}
45+
46+
47+
val embed = EmbedBuilder()
48+
.setAuthor(
49+
user.name,
50+
null,
51+
user.avatarUrl
52+
)
53+
.setTitle("${crit.description} (${crit.intensity})")
54+
.addField("Location", "${Dice.getBodyPart(crit.location)} (${crit.location})", false)
55+
.addField("Wounds", crit.wounds, false)
56+
.addField("Additional Effects", crit.additionalEffects, false)
57+
.setTimestamp(Instant.now())
58+
59+
60+
event.replyEmbeds(embed.build()).setEphemeral(ephemeral).setActionRow(Dice.getActionRow()).queue()
61+
}
62+
}
63+
64+
/*
65+
66+
`**Location**:
67+
${finalString} (_${roll}_)
68+
69+
**Wounds**:
70+
${list[`${num}`].wounds}
71+
72+
**Additional Effects**:
73+
${list[`${num}`].additionalEffects}`
74+
75+
*/

src/main/kotlin/de/joshua/dnd/Dice.kt

Lines changed: 112 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
package de.joshua.dnd
22

3+
import com.beust.klaxon.JsonReader
4+
import com.beust.klaxon.Klaxon
5+
import de.joshua.DiscordBot
6+
import de.joshua.dnd.buttons.BodyPartButton
7+
import de.joshua.dnd.buttons.CritButton
8+
import de.joshua.dnd.buttons.DiceButton
9+
import de.joshua.dnd.buttons.d10Button
10+
import net.dv8tion.jda.api.interactions.components.ItemComponent
311
import java.awt.Color
12+
import java.io.StringReader
13+
import java.net.URL
14+
415

516
class Dice {
617
companion object {
7-
fun rollDice(max: Int): Int {
8-
return (Math.random() * max).toInt()
18+
fun rollDice(max: Int = 100): Int {
19+
return (Math.random() * max + 1).toInt()
920
}
1021

22+
23+
1124
fun getColor(per: Double): Color {
1225
val percent = per / 100
1326
val best = Color.decode("#066e32")
@@ -25,6 +38,80 @@ class Dice {
2538
if (list.size < 2) return false
2639
return list.size == 3 || list[0] == list[1]
2740
}
41+
42+
fun getCrit(location: Int = this.rollDice(), intensity: Int = this.rollDice()): CritEntry {
43+
44+
var file: URL? = null
45+
46+
if(location <= 9){
47+
file = DiscordBot::class.java.getResource("/crit/head.json")
48+
}else if(location <= 24){
49+
file = DiscordBot::class.java.getResource("/crit/arm.json")
50+
}else if(location <= 44){
51+
file = DiscordBot::class.java.getResource("/crit/arm.json")
52+
}else if(location <= 79){
53+
file = DiscordBot::class.java.getResource("/crit/body.json")
54+
}else if(location <= 89){
55+
file = DiscordBot::class.java.getResource("/crit/leg.json")
56+
}else if(location <= 100){
57+
file = DiscordBot::class.java.getResource("/crit/leg.json")
58+
}
59+
60+
if(file == null) return CritEntry(0, "", "Error", "", "")
61+
62+
63+
val klaxon = Klaxon()
64+
val result = arrayListOf<TableEntry>()
65+
JsonReader(StringReader(file.readText())).use { reader ->
66+
reader.beginArray {
67+
while (reader.hasNext()) {
68+
/*val person = klaxon.parse<TableEntry>(reader)
69+
if (person != null) {
70+
result.add(person)
71+
}*/
72+
klaxon.parse<TableEntry>(reader)?.let { result.add(it) }
73+
}
74+
}
75+
}
76+
77+
78+
var index = -1
79+
for (i in result.size - 1 downTo 0) {
80+
if (result.get(i).max <= intensity) {
81+
index = i
82+
break
83+
}
84+
}
85+
86+
87+
return CritEntry(location, intensity.toString(), result[index].description, result[index].wounds, result[index].additionalEffects)
88+
}
89+
90+
fun getBodyPart(number: Int): String {
91+
if(number <= 9){
92+
return "Head"
93+
}else if(number <= 24){
94+
return "Left Arm"
95+
}else if(number <= 44){
96+
return "Right Arm"
97+
}else if(number <= 79){
98+
return "Body"
99+
}else if(number <= 89){
100+
return "Left Leg"
101+
}else if(number <= 100){
102+
return "Right Leg"
103+
}
104+
return "Error"
105+
}
106+
107+
fun getActionRow(): List<ItemComponent> {
108+
val list = mutableListOf<ItemComponent>()
109+
list.add(DiceButton().getButton())
110+
list.add(d10Button().getButton())
111+
list.add(BodyPartButton().getButton())
112+
list.add(CritButton().getButton())
113+
return list
114+
}
28115
}
29116
}
30117

@@ -37,8 +124,28 @@ fun Int.toDigits(base: Int = 10): List<Int> = sequence {
37124
}
38125
}.toList()
39126

40-
class TableEntry(
127+
fun getBodyPartNumber(bodyPart: String): Int {
128+
when(bodyPart){
129+
"head" -> return 0
130+
"arm" -> return 10
131+
"body" -> return 45
132+
"leg" -> return 80
133+
}
134+
return 0
135+
}
136+
137+
138+
data class TableEntry(
139+
val max: Int,
41140
val description: String,
42-
val wounds: Int,
43-
val additionalEffects: String
141+
val wounds: String,
142+
val additionalEffects: String,
44143
)
144+
145+
data class CritEntry(
146+
val location: Int,
147+
val intensity: String,
148+
val description: String,
149+
val wounds: String,
150+
val additionalEffects: String,
151+
)

0 commit comments

Comments
 (0)