Skip to content

Commit

Permalink
profile upgrades
Browse files Browse the repository at this point in the history
  • Loading branch information
peterwilli committed Sep 13, 2022
1 parent 4047a59 commit d3c8651
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 10 deletions.
30 changes: 30 additions & 0 deletions src/main/kotlin/commands/social/SetBackground.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ import miniManual
import net.dv8tion.jda.api.JDA
import net.dv8tion.jda.api.entities.Message
import net.dv8tion.jda.api.entities.User
import net.dv8tion.jda.api.entities.emoji.Emoji
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
import net.dv8tion.jda.api.events.interaction.component.SelectMenuInteractionEvent
import net.dv8tion.jda.api.hooks.ListenerAdapter
import net.dv8tion.jda.api.interactions.components.buttons.Button
import net.dv8tion.jda.api.interactions.components.buttons.ButtonStyle
import net.dv8tion.jda.api.interactions.components.selections.SelectMenu
import net.dv8tion.jda.api.interactions.components.selections.SelectOption
import net.dv8tion.jda.api.requests.restaction.WebhookMessageEditAction
import net.dv8tion.jda.api.utils.FileUpload
import org.atteo.evo.inflector.English
Expand All @@ -28,6 +34,27 @@ import java.awt.image.BufferedImage
import java.net.URL
import javax.imageio.ImageIO

object DropdownBot : ListenerAdapter() {
override fun onSlashCommandInteraction(event: SlashCommandInteractionEvent) {
if (event.name == "food") {
val selectMenu = SelectMenu.create("choose-food")
.addOption("Pizza", "pizza", "Classic") // SelectOption with only the label, value, and description
.addOptions(
SelectOption.of("Hamburger", "hamburger") // another way to create a SelectOption
.withDescription("Tasty") // this time with a description
.withEmoji(Emoji.fromUnicode("\uD83C\uDF54")) // and an emoji
.withDefault(true)) // while also being the default option
.build()
}
}

override fun onSelectMenuInteraction(event: SelectMenuInteractionEvent) {
if (event.componentId == "choose-food") {
event.reply("You chose " + event.values[0]).queue()
}
}
}

fun setBackgroundCommand(jda: JDA) {
jda.onCommand("set_background") { event ->
try {
Expand Down Expand Up @@ -67,6 +94,9 @@ fun setBackgroundCommand(jda: JDA) {
val card = makeProfileCard(event.user, getSlicedBG())
return event.hook.editMessage(content = "**Preview!**")
.setFiles(FileUpload.fromData(bufferedImageToByteArray(card), "profile.png"))
}
fun getFontDropdown() {

}
fun getButtons(): Array<Button> {
val upButton = jda.button(
Expand Down
17 changes: 13 additions & 4 deletions src/main/kotlin/ui/RealPaginator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,17 @@ class Paginator internal constructor(private val nonce: String, private val ttl:

private var index = 0
private val pageCache = mutableListOf<MessageCreateData>()
private val nextPage: MessageCreateData get() = pageCache[++index]
private val prevPage: MessageCreateData get() = pageCache[--index]
private val nextPage: MessageCreateData get() {
index = (index + 1) % pageCache.size
return pageCache[index]
}
private val prevPage: MessageCreateData get() {
index = (index - 1)
if(index < 0) {
index = pageCache.size - 1
}
return pageCache[index]
}
var customActionComponents: List<ActionComponent>? = null
var filter: (ButtonInteraction) -> Boolean = { true }
var injectMessageCallback: ((index: Int, messageEdit: MessageEditCallbackAction) -> Unit)? = null
Expand All @@ -72,8 +81,8 @@ class Paginator internal constructor(private val nonce: String, private val ttl:

internal fun getControls(): ActionRow {
val controls: MutableList<ActionComponent> = mutableListOf(
prev.withDisabled(index == 0).withId("$nonce:prev"),
next.withDisabled(index == pageCache.size - 1).withId("$nonce:next")
prev.withId("$nonce:prev"),
next.withId("$nonce:next")
)
if(customActionComponents != null) {
controls.addAll(customActionComponents!!)
Expand Down
16 changes: 10 additions & 6 deletions src/main/kotlin/utils/MakeProfileCard.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import org.atteo.evo.inflector.English
import java.awt.Color
import java.awt.Font
import java.awt.image.BufferedImage
import java.net.URL
import javax.imageio.ImageIO

val profileCardHeight = 128
fun makeProfileCard(user: User, overrideBackground: BufferedImage?): BufferedImage {
val thingyUser = userDao.queryBuilder().selectColumns("id", "generationsDone").where().eq("discordUserID", user.id).queryForFirst()
val pfp = ImageIO.read(URL(user.avatarUrl)).resize(64, 64)
val card = BufferedImage(512, profileCardHeight, BufferedImage.TYPE_INT_RGB)
val labelFont = Font("Arial", Font.PLAIN, 16)
val valFont = Font("Arial", Font.BOLD, 18)

val g = card.graphics

fun drawBackground(bg: BufferedImage) {
Expand All @@ -22,16 +24,18 @@ fun makeProfileCard(user: User, overrideBackground: BufferedImage?): BufferedIma
if (overrideBackground != null) {
drawBackground(overrideBackground)
}
g.drawImage(pfp, 10, 10, null)
val pfpSideX = pfp.width + 10
g.color = Color.WHITE
g.font = valFont
g.drawString(user.name, 20, 25)
g.drawString(user.name, pfpSideX + 10, 25)

fun addLabelAndValue(label: String, value: String) {
g.drawString(value, 20, 60)
fun addLabelAndValue(x: Int, y: Int, label: String, value: String) {
g.drawString(value, x, y)
val valWidth = g.fontMetrics.stringWidth(value)
g.font = labelFont
g.drawString(label, valWidth + 40, 60)
g.drawString(label, x + valWidth + 10, y)
}
addLabelAndValue(English.plural("Generation", if(thingyUser.generationsDone > 1) 2 else 1), thingyUser.generationsDone.toString())
addLabelAndValue(pfpSideX + 10, 60, English.plural("Generation", if(thingyUser.generationsDone > 1) 2 else 1), thingyUser.generationsDone.toString())
return card
}
13 changes: 13 additions & 0 deletions src/main/kotlin/utils/ResizeImage.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package utils

import java.awt.Image
import java.awt.image.BufferedImage

fun BufferedImage.resize(newW: Int, newH: Int): BufferedImage {
val tmp = this.getScaledInstance(newW, newH, Image.SCALE_SMOOTH)
val dimg = BufferedImage(newW, newH, BufferedImage.TYPE_INT_RGB)
val g2d = dimg.createGraphics()
g2d.drawImage(tmp, 0, 0, null)
g2d.dispose()
return dimg
}

0 comments on commit d3c8651

Please sign in to comment.