|
| 1 | +package de.joshua.dnd |
| 2 | + |
| 3 | +import com.beust.klaxon.JsonReader |
| 4 | +import com.beust.klaxon.Klaxon |
| 5 | +import de.joshua.DiscordBot |
| 6 | +import de.joshua.api.commands.slashcommands.SlashCommand |
| 7 | +import de.joshua.api.commands.slashcommands.SlashCommands |
| 8 | +import net.dv8tion.jda.api.EmbedBuilder |
| 9 | +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent |
| 10 | +import net.dv8tion.jda.api.interactions.commands.OptionType |
| 11 | +import net.dv8tion.jda.api.interactions.commands.build.OptionData |
| 12 | +import net.dv8tion.jda.api.interactions.commands.build.SubcommandData |
| 13 | +import net.dv8tion.jda.internal.interactions.CommandDataImpl |
| 14 | +import java.io.StringReader |
| 15 | +import java.time.Instant |
| 16 | + |
| 17 | +@SlashCommands |
| 18 | +class MiscastCommand : SlashCommand { |
| 19 | + override fun commandData(): CommandDataImpl { |
| 20 | + return CommandDataImpl("miscast", "miscast") |
| 21 | + .addSubcommands( |
| 22 | + SubcommandData("minor", "minor miscast") |
| 23 | + .addOptions( |
| 24 | + OptionData(OptionType.INTEGER, "times", "Amount of roles"), |
| 25 | + OptionData(OptionType.USER, "user", "User to roll for"), |
| 26 | + OptionData(OptionType.BOOLEAN, "private", "Roles a dice privately")), |
| 27 | + SubcommandData("major", "major miscast") |
| 28 | + .addOptions( |
| 29 | + OptionData(OptionType.INTEGER, "times", "Amount of roles"), |
| 30 | + OptionData(OptionType.USER, "user", "User to roll for"), |
| 31 | + OptionData(OptionType.BOOLEAN, "private", "Roles a dice privately"))) |
| 32 | + } |
| 33 | + |
| 34 | + override fun onExecute(event: SlashCommandInteractionEvent) { |
| 35 | + val ephemeral = event.getOption("private") != null && event.getOption("private")?.asBoolean!! |
| 36 | + |
| 37 | + var user = event.user |
| 38 | + if (event.getOption("user") != null) { |
| 39 | + user = event.getOption("user")?.asUser!! |
| 40 | + } |
| 41 | + |
| 42 | + if (event.getOption("times") != null && event.getOption("times")?.asInt!! > 1) { |
| 43 | + val rolls = List(event.getOption("times")?.asInt!!) { getEntryMaxVal(getFileContent(event.subcommandName?:"minor"), Dice.rollDice(100)) } |
| 44 | + |
| 45 | + val embed = |
| 46 | + EmbedBuilder() |
| 47 | + .setAuthor(user.name, null, user.avatarUrl) |
| 48 | + .setTimestamp(Instant.now()) |
| 49 | + .setTitle("Rolled ${rolls.size} ${event.subcommandName?:"minor"} Miscasts") |
| 50 | + .setDescription(rolls.joinToString { "\n" + it.description }) |
| 51 | + |
| 52 | + event |
| 53 | + .replyEmbeds(embed.build()) |
| 54 | + .setEphemeral(ephemeral) |
| 55 | + .setActionRow(Dice.getActionRow()) |
| 56 | + .queue() |
| 57 | + } else { |
| 58 | + val roll = getEntryMaxVal(getFileContent(event.subcommandName?:"minor"), Dice.rollDice(100)) |
| 59 | + |
| 60 | + val embed = |
| 61 | + EmbedBuilder() |
| 62 | + .setAuthor(user.name, null, user.avatarUrl) |
| 63 | + .setTitle("${event.subcommandName?:"minor"} miscast") |
| 64 | + .setDescription(roll.description) |
| 65 | + .setTimestamp(Instant.now()) |
| 66 | + |
| 67 | + event |
| 68 | + .replyEmbeds(embed.build()) |
| 69 | + .setEphemeral(ephemeral) |
| 70 | + .setActionRow(Dice.getActionRow()) |
| 71 | + .queue() |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private fun getFileContent(type: String): ArrayList<MiscastTableEntry> { |
| 76 | + val file = DiscordBot::class.java.getResource("/miscast/${type}.json") |
| 77 | + |
| 78 | + val result = arrayListOf<MiscastTableEntry>() |
| 79 | + val klaxon = Klaxon() |
| 80 | + if (file != null) { |
| 81 | + JsonReader(StringReader(file.readText())).use { reader -> |
| 82 | + reader.beginArray { |
| 83 | + while (reader.hasNext()) { |
| 84 | + klaxon.parse<MiscastTableEntry>(reader)?.let { result.add(it) } |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + return result |
| 90 | + } |
| 91 | + |
| 92 | + private fun getEntryMaxVal(result: ArrayList<MiscastTableEntry>, number: Int): MiscastTableEntry { |
| 93 | + var index = -1 |
| 94 | + for (i in result.size - 1 downTo 0) { |
| 95 | + if (result.get(i).max <= number) { |
| 96 | + index = i |
| 97 | + break |
| 98 | + } |
| 99 | + } |
| 100 | + if(index == -1) index = 0 |
| 101 | + return result[index] |
| 102 | + } |
| 103 | + |
| 104 | + private data class MiscastTableEntry( |
| 105 | + val max: Int, |
| 106 | + val description: String |
| 107 | + ) |
| 108 | +} |
0 commit comments