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

Add queue command #1

Merged
merged 14 commits into from
Nov 8, 2020
Merged

Add queue command #1

merged 14 commits into from
Nov 8, 2020

Conversation

OtterBoops
Copy link
Collaborator

@OtterBoops OtterBoops commented Nov 3, 2020

Basic list of songs queued, including the one currently playing.
Stuff to be taken care of after initial approval:

  • - Sanitize output
  • - Format better
  • - Pagination (somehow)
  • - Add requester info

@OtterBoops OtterBoops marked this pull request as draft November 3, 2020 11:17
@OtterBoops OtterBoops self-assigned this Nov 3, 2020
@OtterBoops
Copy link
Collaborator Author

Off the top of your head, can the current way of writing the list cause any issues (unwanted mentions, janky text, etc.)?

Also is there a way for me to get the song requester that is readily available? Or does that need to be implemented separately?

@freyacodes
Copy link
Owner

Sorry for the late reply. I'm currently sick.

Off the top of your head, can the current way of writing the list cause any issues (unwanted mentions, janky text, etc.)?

All mentions are configured to be disabled by default. Janky text is possible. At some point I would like to write formatter helper functions that take unsafe args as varargs. Don't worry about it for now.

Also is there a way for me to get the song requester that is readily available? Or does that need to be implemented separately?

We'd need to store metadata about it. Lavaplayer is in no way aware of JDA. Lavaplayer has a way to set custom data with each track. I have a fancy way of doing this with Kotlin.

duration += queue.getDuration()
return duration
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These new functions looks a lot like a Java dev trying to write Java in Kotlin. Kotlin has some tricks that would make these much shorter.

var duration: Long = 0

if (player.playingTrack != null)
duration += player.playingTrack.info.length
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gets the total length not the remaining length. Subtract track the position.

) : Command("q") {

private val pageSize = 5
private val response = StringBuilder()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this a class property?? This is in no way thread-safe.

val pageIndex: Int

// Handle edge cases for arguments
if (index == 0)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This chain can be replaced by a single call to Int#coerceIn()

If you were using Java, I would recommend Math.max() and Math.min() on a single line.

pageIndex = index

//Add header
response.append("Page **${pageIndex}** of **${pageCount}**\n\n")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
response.append("Page **${pageIndex}** of **${pageCount}**\n\n")
response.append("Page **$pageIndex** of **$pageCount**\n\n")

return hms
}

private fun CommandContext.paginateQueue(queue: List<AudioTrack>, index: Int) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try this neat helper function to use a StringBuilder as receiver and return a string

Suggested change
private fun CommandContext.paginateQueue(queue: List<AudioTrack>, index: Int) {
private fun CommandContext.paginateQueue(queue: List<AudioTrack>, index: Int) = builder {


// Preserve original indexes
queue.forEachIndexed { ind, t ->
if (t in queue.chunked(pageSize)[pageIndex - 1])
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be smarter to just iterate a sublist?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't a sublist hold its' own indexes, in this case 0-4? Or am I mistaken?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, but we can just calculate an offset

return queue
}
fun getDuration(): Long {
Copy link
Owner

@freyacodes freyacodes Nov 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Want to save 6 lines of code?
fun getDuration() = queue.sumOf { it.info.length }

@OtterBoops OtterBoops marked this pull request as ready for review November 8, 2020 16:45
@freyacodes freyacodes merged commit 4b623bc into master Nov 8, 2020
@freyacodes freyacodes deleted the command/queue branch November 8, 2020 16:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants