Skip to content

Commit

Permalink
A few Kotlin value classes integrated
Browse files Browse the repository at this point in the history
  • Loading branch information
ileukocyte committed Aug 12, 2022
1 parent fc84479 commit 3a7916e
Show file tree
Hide file tree
Showing 31 changed files with 199 additions and 173 deletions.
7 changes: 6 additions & 1 deletion src/main/kotlin/io/ileukocyte/hibernum/commands/commands.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
@file:JvmName("Commands")
package io.ileukocyte.hibernum.commands

import io.ileukocyte.hibernum.commands.TextCommand.ClassicTextUsage
import io.ileukocyte.hibernum.commands.TextCommand.ClassicTextUsageGroup
import io.ileukocyte.hibernum.extensions.capitalizeAll

import net.dv8tion.jda.api.Permission
Expand Down Expand Up @@ -242,13 +244,16 @@ interface TextCommand : GenericCommand {
override fun toString() = option
}

@JvmInline
value class ClassicTextUsageGroup(val group: Collection<ClassicTextUsage>)

fun String.toClassicTextUsage(
isOptional: Boolean = false,
applyDefaultAffixes: Boolean = true,
) = ClassicTextUsage(this, isOptional, applyDefaultAffixes)
}

typealias ClassicTextUsageGroup = Collection<TextCommand.ClassicTextUsage>
fun usageGroupOf(vararg usages: ClassicTextUsage) = ClassicTextUsageGroup(usages.toList())

/**
* A type of command that can be used as a classic text command exclusively
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package io.ileukocyte.hibernum.commands.developer

import io.ileukocyte.hibernum.Immutable
import io.ileukocyte.hibernum.commands.CommandException
import io.ileukocyte.hibernum.commands.MessageContextOnlyCommand
import io.ileukocyte.hibernum.commands.NoArgumentsException
import io.ileukocyte.hibernum.commands.TextCommand
import io.ileukocyte.hibernum.commands.*
import io.ileukocyte.hibernum.extensions.*
import io.ileukocyte.openweather.Forecast
import io.ileukocyte.openweather.OpenWeatherApi
Expand Down Expand Up @@ -41,8 +38,8 @@ class EvalCommand : TextCommand, MessageContextOnlyCommand {
)
override val aliases = setOf("exec", "execute", "kotlin", "kt")
override val usages = setOf(
setOf("Kotlin code".toClassicTextUsage()),
setOf("reply to a message with Kotlin code".toClassicTextUsage()),
usageGroupOf("Kotlin code".toClassicTextUsage()),
usageGroupOf("reply to a message with Kotlin code".toClassicTextUsage()),
)

private val packages get() = buildString {
Expand Down
240 changes: 123 additions & 117 deletions src/main/kotlin/io/ileukocyte/hibernum/commands/fun/BattleshipCommand.kt

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.ileukocyte.hibernum.commands.`fun`
import io.ileukocyte.hibernum.Immutable
import io.ileukocyte.hibernum.commands.NoArgumentsException
import io.ileukocyte.hibernum.commands.TextCommand
import io.ileukocyte.hibernum.commands.usageGroupOf
import io.ileukocyte.hibernum.extensions.replyEmbed
import io.ileukocyte.hibernum.extensions.sendEmbed

Expand All @@ -15,7 +16,7 @@ class EightBallCommand : TextCommand {
override val name = "8ball"
override val description = "Asks your question of the Magic 8 Ball"
override val aliases = setOf("eightball", "eight-ball")
override val usages = setOf(setOf("question".toClassicTextUsage()))
override val usages = setOf(usageGroupOf("question".toClassicTextUsage()))
override val options = setOf(
OptionData(OptionType.STRING, "question", "The question to ask of the Magic 8 Ball", true))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import io.ileukocyte.hibernum.Immutable
import io.ileukocyte.hibernum.commands.CommandException
import io.ileukocyte.hibernum.commands.NoArgumentsException
import io.ileukocyte.hibernum.commands.TextCommand
import io.ileukocyte.hibernum.commands.usageGroupOf
import io.ileukocyte.hibernum.extensions.*
import io.ileukocyte.hibernum.utils.*

Expand Down Expand Up @@ -35,7 +36,7 @@ class GuessNumberCommand : TextCommand {
.setMaxValue(500_000),
)
override val usages = setOf(
setOf("min".toClassicTextUsage(), "max".toClassicTextUsage()),
usageGroupOf("min".toClassicTextUsage(), "max".toClassicTextUsage()),
)

override suspend fun invoke(event: MessageReceivedEvent, args: String?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ class InvertCommand : TextCommand, ContextCommand {
"a profile picture of the mentioned user (or yours in case of no arguments provided)"
override val aliases = setOf("negate", "negative")
override val usages = setOf(
setOf("image file".toClassicTextUsage(true)),
setOf("image link".toClassicTextUsage(true)),
setOf("user mention".toClassicTextUsage(true)),
usageGroupOf("image file".toClassicTextUsage(true)),
usageGroupOf("image link".toClassicTextUsage(true)),
usageGroupOf("user mention".toClassicTextUsage(true)),
)
override val options = setOf(
OptionData(OptionType.STRING, "link", "The provided image"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.ileukocyte.hibernum.commands.`fun`
import io.ileukocyte.hibernum.Immutable
import io.ileukocyte.hibernum.commands.CommandException
import io.ileukocyte.hibernum.commands.TextCommand
import io.ileukocyte.hibernum.commands.usageGroupOf
import io.ileukocyte.hibernum.extensions.monospace
import io.ileukocyte.hibernum.extensions.replyEmbed
import io.ileukocyte.hibernum.extensions.sendEmbed
Expand All @@ -27,7 +28,7 @@ class RollCommand : TextCommand {
.setMaxValue(25),
)
override val usages = setOf(
setOf("<number of dice (optional, 1–25)>d<number of sides (2–250)>"
usageGroupOf("<number of dice (optional, 1–25)>d<number of sides (2–250)>"
.toClassicTextUsage(applyDefaultAffixes = false))
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package io.ileukocyte.hibernum.commands.`fun`

import io.ileukocyte.hibernum.commands.CommandException
import io.ileukocyte.hibernum.commands.MessageContextOnlyCommand
import io.ileukocyte.hibernum.commands.NoArgumentsException
import io.ileukocyte.hibernum.commands.TextCommand
import io.ileukocyte.hibernum.commands.*
import io.ileukocyte.hibernum.extensions.await
import io.ileukocyte.hibernum.extensions.limitTo

Expand Down Expand Up @@ -32,7 +29,7 @@ class TextToImageCommand : TextCommand, MessageContextOnlyCommand {
override val interactionName = "tti"
override val description = "Creates an image containing the provided text"
override val aliases = setOf("tti")
override val usages = setOf(setOf("input".toClassicTextUsage()))
override val usages = setOf(usageGroupOf("input".toClassicTextUsage()))
override val cooldown = 5L

override suspend fun invoke(event: MessageReceivedEvent, args: String?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import org.jetbrains.kotlin.utils.addToStdlib.cast
class HelpCommand : TextCommand {
override val name = "help"
override val description = "Sends a list of all the bot's commands and provides the user with the documentation"
override val usages = setOf(setOf("command name".toClassicTextUsage(true)))
override val usages = setOf(usageGroupOf("command name".toClassicTextUsage(true)))
override val options = setOf(
OptionData(OptionType.STRING, "command", "The command to provide help for")
.setAutoComplete(true))
Expand Down Expand Up @@ -155,7 +155,7 @@ class HelpCommand : TextCommand {
field {
title = "Classic Text Usages"
description = usages.joinToString("\n") { group ->
"$prefix$name ${group.joinToString(" ") { usage ->
"$prefix$name ${group.group.joinToString(" ") { usage ->
usage.toString()
.applyIf(usage.isOptional) { "$this (optional)" }
.applyIf(usage.applyDefaultAffixes) { surroundWith("<", ">") }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.ileukocyte.hibernum.commands.information
import io.ileukocyte.hibernum.builders.buildEmbed
import io.ileukocyte.hibernum.commands.CommandException
import io.ileukocyte.hibernum.commands.TextCommand
import io.ileukocyte.hibernum.commands.usageGroupOf
import io.ileukocyte.hibernum.extensions.asWord
import io.ileukocyte.hibernum.extensions.await
import io.ileukocyte.hibernum.extensions.escapeMarkdown
Expand All @@ -25,7 +26,7 @@ class EmoteCommand : TextCommand {
override val aliases = setOf("emoji", "emoji-info", "emote-info")
override val options = setOf(
OptionData(OptionType.STRING, "emote", "The custom emoji to check information about", true))
override val usages = setOf(setOf("custom emoji".toClassicTextUsage()))
override val usages = setOf(usageGroupOf("custom emoji".toClassicTextUsage()))

override suspend fun invoke(event: MessageReceivedEvent, args: String?) {
val emote = event.message.mentions.customEmojis.firstOrNull()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.ileukocyte.hibernum.commands.CommandException
import io.ileukocyte.hibernum.commands.NoArgumentsException
import io.ileukocyte.hibernum.commands.TextCommand
import io.ileukocyte.hibernum.commands.information.UserCommand.Companion.FEATURED_PERMISSIONS
import io.ileukocyte.hibernum.commands.usageGroupOf
import io.ileukocyte.hibernum.extensions.*
import io.ileukocyte.hibernum.utils.getImageBytes

Expand All @@ -27,9 +28,9 @@ class RoleCommand : TextCommand {
override val description = "Sends the available information about the specified role"
override val aliases = setOf("role-info")
override val usages = setOf(
setOf("role name".toClassicTextUsage()),
setOf("role mention".toClassicTextUsage()),
setOf("role ID".toClassicTextUsage()),
usageGroupOf("role name".toClassicTextUsage()),
usageGroupOf("role mention".toClassicTextUsage()),
usageGroupOf("role ID".toClassicTextUsage()),
)
override val options = setOf(
OptionData(OptionType.ROLE, "role", "The role to check information about", true))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.ileukocyte.hibernum.builders.buildEmbed
import io.ileukocyte.hibernum.commands.ClassicTextOnlyCommand
import io.ileukocyte.hibernum.commands.CommandException
import io.ileukocyte.hibernum.commands.MessageContextOnlyCommand
import io.ileukocyte.hibernum.commands.usageGroupOf
import io.ileukocyte.hibernum.extensions.await
import io.ileukocyte.hibernum.extensions.escapeMarkdown
import io.ileukocyte.hibernum.utils.getDominantColorByImageUrl
Expand All @@ -26,8 +27,8 @@ class StickerCommand : ClassicTextOnlyCommand, MessageContextOnlyCommand {
override val description = "Sends the available information about the provided sticker"
override val aliases = setOf("sticker-info")
override val usages = setOf(
setOf("sticker".toClassicTextUsage()),
setOf("reply to a sticker message".toClassicTextUsage()),
usageGroupOf("sticker".toClassicTextUsage()),
usageGroupOf("reply to a sticker message".toClassicTextUsage()),
)

override suspend fun invoke(event: MessageReceivedEvent, args: String?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.ileukocyte.hibernum.builders.buildEmbed
import io.ileukocyte.hibernum.commands.CommandException
import io.ileukocyte.hibernum.commands.TextCommand
import io.ileukocyte.hibernum.commands.UserContextOnlyCommand
import io.ileukocyte.hibernum.commands.usageGroupOf
import io.ileukocyte.hibernum.extensions.*
import io.ileukocyte.hibernum.utils.getDominantColorByImageProxy

Expand Down Expand Up @@ -35,9 +36,9 @@ class UserCommand : TextCommand, UserContextOnlyCommand {
override val description = "Sends either detailed information about the specified user's account or their profile picture"
override val aliases = setOf("member", "member-info", "user-info")
override val usages = setOf(
setOf("user name".toClassicTextUsage(true)),
setOf("user mention".toClassicTextUsage(true)),
setOf("user ID".toClassicTextUsage(true)),
usageGroupOf("user name".toClassicTextUsage(true)),
usageGroupOf("user mention".toClassicTextUsage(true)),
usageGroupOf("user ID".toClassicTextUsage(true)),
)
override val options = setOf(
OptionData(OptionType.USER, "user", "The user to check information about"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import io.ileukocyte.hibernum.audio.audioPlayer
import io.ileukocyte.hibernum.commands.CommandException
import io.ileukocyte.hibernum.commands.NoArgumentsException
import io.ileukocyte.hibernum.commands.TextCommand
import io.ileukocyte.hibernum.commands.usageGroupOf
import io.ileukocyte.hibernum.extensions.isInt
import io.ileukocyte.hibernum.extensions.replySuccess
import io.ileukocyte.hibernum.extensions.sendSuccess
Expand All @@ -21,7 +22,7 @@ import net.dv8tion.jda.api.interactions.commands.build.OptionData
class MoveCommand : TextCommand {
override val name = "move"
override val description = "Moves the selected track in the queue to the specified position"
override val usages = setOf(setOf("song", "index").map { it.toClassicTextUsage() })
override val usages = setOf(usageGroupOf("song".toClassicTextUsage(), "index".toClassicTextUsage()))
override val options = setOf(
OptionData(OptionType.INTEGER, "song", "The index of the song to move", true),
OptionData(OptionType.INTEGER, "index", "The index to move the song to", true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import io.ileukocyte.hibernum.audio.canJoinFromAnother
import io.ileukocyte.hibernum.commands.CommandException
import io.ileukocyte.hibernum.commands.NoArgumentsException
import io.ileukocyte.hibernum.commands.TextCommand
import io.ileukocyte.hibernum.commands.usageGroupOf
import io.ileukocyte.hibernum.extensions.*
import io.ileukocyte.hibernum.handlers.CommandHandler
import io.ileukocyte.hibernum.utils.YOUTUBE_LINK_REGEX
Expand All @@ -36,8 +37,8 @@ class PlayCommand : TextCommand {
OptionData(OptionType.ATTACHMENT, "attachment", "The media file to play"),
)
override val usages = setOf(
setOf("query".toClassicTextUsage()),
setOf("file".toClassicTextUsage()),
usageGroupOf("query".toClassicTextUsage()),
usageGroupOf("file".toClassicTextUsage()),
)

override suspend fun invoke(event: MessageReceivedEvent, args: String?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import io.ileukocyte.hibernum.commands.GenericCommand.StaleInteractionHandling
import io.ileukocyte.hibernum.commands.TextCommand
import io.ileukocyte.hibernum.commands.music.LoopCommand.Companion.getButton
import io.ileukocyte.hibernum.commands.music.LoopCommand.Companion.getNext
import io.ileukocyte.hibernum.commands.usageGroupOf
import io.ileukocyte.hibernum.extensions.*
import io.ileukocyte.hibernum.handlers.CommandHandler
import io.ileukocyte.hibernum.utils.asDuration
Expand All @@ -37,7 +38,7 @@ class QueueCommand : TextCommand {
override val name = "queue"
override val description = "Shows the current playlist"
override val aliases = setOf("q", "playlist")
override val usages = setOf(setOf("page".toClassicTextUsage(true)))
override val usages = setOf(usageGroupOf("page".toClassicTextUsage(true)))
override val options = setOf(
OptionData(OptionType.INTEGER, "page", "Initial page number"),
OptionData(OptionType.BOOLEAN, "gui-player", "Whether the button player should " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.ileukocyte.hibernum.commands.music
import io.ileukocyte.hibernum.audio.audioPlayer
import io.ileukocyte.hibernum.commands.CommandException
import io.ileukocyte.hibernum.commands.TextCommand
import io.ileukocyte.hibernum.commands.usageGroupOf
import io.ileukocyte.hibernum.extensions.replySuccess
import io.ileukocyte.hibernum.extensions.sendSuccess

Expand All @@ -14,7 +15,7 @@ import net.dv8tion.jda.api.interactions.commands.build.OptionData
class RemoveCommand : TextCommand {
override val name = "remove"
override val description = "Removes the selected track from the queue"
override val usages = setOf(setOf("song".toClassicTextUsage()))
override val usages = setOf(usageGroupOf("song".toClassicTextUsage()))
override val options = setOf(
OptionData(OptionType.INTEGER, "song", "The number of the song to remove", true))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import io.ileukocyte.hibernum.audio.audioPlayer
import io.ileukocyte.hibernum.audio.customUserData
import io.ileukocyte.hibernum.commands.CommandException
import io.ileukocyte.hibernum.commands.TextCommand
import io.ileukocyte.hibernum.commands.usageGroupOf
import io.ileukocyte.hibernum.extensions.await

import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
Expand All @@ -15,7 +16,7 @@ class RequeueCommand : TextCommand {
override val name = "requeue"
override val description = "Adds the specified (or currently playing) song to the end of the queue"
override val aliases = setOf("re-add", "readd", "re-queue")
override val usages = setOf(setOf("song".toClassicTextUsage()))
override val usages = setOf(usageGroupOf("song".toClassicTextUsage()))
override val options = setOf(
OptionData(OptionType.INTEGER, "song", "The number of the song to readd"))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import io.ileukocyte.hibernum.audio.audioPlayer
import io.ileukocyte.hibernum.commands.CommandException
import io.ileukocyte.hibernum.commands.NoArgumentsException
import io.ileukocyte.hibernum.commands.TextCommand
import io.ileukocyte.hibernum.commands.usageGroupOf
import io.ileukocyte.hibernum.extensions.replySuccess
import io.ileukocyte.hibernum.extensions.sendSuccess
import io.ileukocyte.hibernum.utils.TIME_CODE_REGEX
Expand All @@ -22,7 +23,7 @@ class SeekCommand : TextCommand {
override val name = "seek"
override val description = "Jumps to the specified time while playing the track"
override val aliases = setOf("jump")
override val usages = setOf(setOf("[r(ewind):/f(ast-forward):]time code".toClassicTextUsage()))
override val usages = setOf(usageGroupOf("[r(ewind):/f(ast-forward):]time code".toClassicTextUsage()))
override val options = setOf(
OptionData(
OptionType.STRING,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.ileukocyte.hibernum.audio.audioPlayer
import io.ileukocyte.hibernum.audio.customUserData
import io.ileukocyte.hibernum.commands.CommandException
import io.ileukocyte.hibernum.commands.TextCommand
import io.ileukocyte.hibernum.commands.usageGroupOf
import io.ileukocyte.hibernum.extensions.await
import io.ileukocyte.hibernum.extensions.setFailureEmbed

Expand All @@ -22,7 +23,7 @@ import net.dv8tion.jda.api.interactions.commands.build.OptionData
class SelectCommand : TextCommand {
override val name = "select"
override val description = "Selects another track from the queue and plays it"
override val usages = setOf(setOf("song".toClassicTextUsage()))
override val usages = setOf(usageGroupOf("song".toClassicTextUsage()))
override val options = setOf(
OptionData(OptionType.INTEGER, "song", "The number of the song to play", true))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import io.ileukocyte.hibernum.builders.buildEmbed
import io.ileukocyte.hibernum.commands.CommandException
import io.ileukocyte.hibernum.commands.NoArgumentsException
import io.ileukocyte.hibernum.commands.TextCommand
import io.ileukocyte.hibernum.commands.usageGroupOf
import io.ileukocyte.hibernum.extensions.*
import io.ileukocyte.hibernum.extensions.EmbedType
import io.ileukocyte.hibernum.utils.*
Expand Down Expand Up @@ -40,7 +41,7 @@ class YouTubePlayCommand : TextCommand {
.addChoice("Videos", "videos")
.addChoice("Playlists", "playlists"),
)
override val usages = setOf(setOf("query".toClassicTextUsage()))
override val usages = setOf(usageGroupOf("query".toClassicTextUsage()))
override val cooldown = 5L

override suspend fun invoke(event: MessageReceivedEvent, args: String?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.ileukocyte.hibernum.builders.buildEmbed
import io.ileukocyte.hibernum.commands.CommandException
import io.ileukocyte.hibernum.commands.NoArgumentsException
import io.ileukocyte.hibernum.commands.TextCommand
import io.ileukocyte.hibernum.commands.usageGroupOf
import io.ileukocyte.hibernum.extensions.*

import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent
Expand All @@ -23,7 +24,7 @@ class CharacterCommand : TextCommand {
override val aliases = setOf("character")
override val options = setOf(
OptionData(OptionType.STRING, "input", "The characters provided"))
override val usages = setOf(setOf("input".toClassicTextUsage()))
override val usages = setOf(usageGroupOf("input".toClassicTextUsage()))

override suspend fun invoke(event: MessageReceivedEvent, args: String?) {
val input = args
Expand Down
Loading

0 comments on commit 3a7916e

Please sign in to comment.