Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable navigation via direct input #1

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion src/main/java/wiresegal/wob/WordsOfBrandon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ const val iconUrl = "https://cdn.discordapp.com/emojis/373082865073913859.png?v=

val api: DiscordApi = DiscordApiBuilder().setToken(token).login().join()

var awaiting = mutableListOf<Pair<Message, Triple<Long, Int, List<EmbedBuilder>>>>()
var questionMessages = mutableListOf<Message>()

fun Array<Pair<String, String>>.embeds(title: String, color: Color): List<EmbedBuilder> {
return this.mapIndexed { idx, (rattle, comment) -> EmbedBuilder().apply {
setTitle("(${idx + 1}/$size) \n$title")
Expand Down Expand Up @@ -68,6 +71,7 @@ fun TextChannel.sendRandomEmbed(requester: DiscordEntity, title: String, message
sendMessage(embed).get().setupDeletable(requester).setupControls(requester, index, embeds)
}


fun Element.find(vararg evaluators: Evaluator) = allElements.find(*evaluators)

fun Elements.find(vararg evaluators: Evaluator): Elements {
Expand Down Expand Up @@ -218,8 +222,9 @@ const val first = "⏮"
const val jumpLeft = "⏪"
const val jumpRight = "⏩"
const val no = "❌"
const val nums = "\uD83D\uDD22"

val validReactions = listOf(arrowLeft, arrowRight, done, last, first, jumpLeft, jumpRight, no)
val validReactions = listOf(arrowLeft, arrowRight, done, last, first, jumpLeft, jumpRight, nums, no)

val messagesWithEmbedLists = mutableMapOf<Long, Triple<Long, Int, List<EmbedBuilder>>>()
val messageToAuthor = mutableMapOf<Long, Long>()
Expand All @@ -234,6 +239,14 @@ fun updateMessageWithJump(jump: Int, message: Message, entry: Triple<Long, Int,
}
}

fun updateIndexToInput(originalMessage: Message, entry: Triple<Long, Int, List<EmbedBuilder>>) {
if (!awaiting.contains(originalMessage to entry)) {
val questionMessage = originalMessage.channel.sendMessage("What number entry would you like to go to?").get()
awaiting.add(originalMessage to entry)
questionMessages.add(questionMessage)
}
}

fun Message.setupDeletable(author: DiscordEntity) = setupDeletable(author.id)

fun Message.setupDeletable(id: Long): Message {
Expand All @@ -254,6 +267,7 @@ fun Message.setupControls(requester: DiscordEntity, index: Int, embeds: List<Emb
addReaction(jumpRight)
if (embeds.size > 2)
addReaction(last)
addReaction(nums)

messagesWithEmbedLists[id] = Triple(requester.id, index, embeds)
return this
Expand Down Expand Up @@ -395,6 +409,7 @@ fun main(args: Array<String>) {
arrowRight -> updateMessageWithJump(1, message, messageValue)
jumpRight -> updateMessageWithJump(10, message, messageValue)
last -> updateMessageWithJump(embeds.size, message, messageValue)
nums -> updateIndexToInput(message, messageValue)
done -> {
val finalEmbed = embeds[index]
finalEmbed.setTitle(finalEmbed.toJsonNode()["title"].asText().replace(".*\n".toRegex(), ""))
Expand All @@ -411,4 +426,32 @@ fun main(args: Array<String>) {
}
}
}
api.addMessageCreateListener {
val userInput = it.message
var matchFound = false
var deletionIndex = mutableListOf<Int>()
for(awaitElement in awaiting){
val (originalMessage, entry) = awaitElement
val (uid, index, embeds) = entry
if (userInput.author.id == uid) {
val numsOnly = userInput.content.replace("\\D".toRegex(), "")
if (numsOnly != "") {
val requestedIndex = numsOnly.toInt() - 1
val jump = requestedIndex - index
updateMessageWithJump(jump, originalMessage, entry)
deletionIndex.add(awaiting.indexOf(awaitElement))
matchFound = true
userInput.delete()
}
}
}
if (matchFound) {
for (match in deletionIndex.asReversed()) {
awaiting.removeAt(match)
questionMessages[match].delete()
questionMessages.removeAt(match)
}
userInput.delete()
}
}
}