Skip to content

Commit

Permalink
Update Sponsorblock plugin
Browse files Browse the repository at this point in the history
- Update plugin name
- Add support for YouTubeChapter events
  • Loading branch information
DRSchlaubi committed Jul 22, 2023
1 parent f5cb9d1 commit 4cb5d8c
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ plugins {
}

group = "dev.schlaubi.lavakord"
version = "5.0.3"
version = "5.1.0"

allprojects {
repositories {
mavenCentral()
maven("https://maven.pkg.jetbrains.space/public/p/kotlinx-html/maven")
maven("https://oss.sonatype.org/content/repositories/snapshots")
maven("https://maven.topi.wtf/snapshots")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.launch
import kotlinx.serialization.SerializationException
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.decodeFromJsonElement
import kotlinx.serialization.json.jsonObject
Expand Down Expand Up @@ -164,7 +165,12 @@ internal class NodeImpl(
eventPublisher.tryEmit(event)
return
}
when (val event = lavakord.json.decodeFromJsonElement<Message>(eventRaw)) {
val event = try {
lavakord.json.decodeFromJsonElement<Message>(eventRaw)
} catch (e: SerializationException) {
LOG.warn(e) {"Could not parse event"}
}
when (event) {
is Message.PlayerUpdateEvent -> (lavakord.getLink(event.guildId).player as WebsocketPlayer).provideState(
event.state
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ internal class WebsocketPlayer(internal val node: NodeImpl, internal val guildId
)
}

private suspend fun handleNewTrack(event: TrackStartEvent) {
private fun handleNewTrack(event: TrackStartEvent) {
updateTime = Clock.System.now()
val track = event.track
lastPosition = 0.milliseconds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ suspend fun main() {
}
}

lavalink.addNode("wss://3bc979254994.ngrok.app", "youshallnotpass")
lavalink.addNode("ws://localhost:2333", "youshallnotpass")

kord.on<MessageCreateEvent> {
val args = message.content.split(" ")
Expand Down
6 changes: 3 additions & 3 deletions plugins/sponsorblock/src/commonMain/kotlin/Plugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import dev.schlaubi.lavakord.plugins.sponsorblock.model.Event as SponsorblockRes
*/
public object Sponsorblock : Plugin {
override val name: String
// Topi messed up :rolling_eyes:
get() = "lavasrc-plugin"
get() = "sponsorblock-plugin"
override val version: String
get() = "3.0.0-beta.1"

override val eventTypes: List<String> = listOf("SegmentsLoaded", "SegmentSkipped")
override val eventTypes: List<String> =
listOf("SegmentsLoaded", "SegmentSkipped", "ChaptersLoaded", "ChapterStarted")

override fun JsonElement.decodeToEvent(): Event =
Json.decodeFromJsonElement<SponsorblockRestEvent>(this)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package dev.schlaubi.lavakord.plugins.sponsorblock.model

import kotlinx.serialization.KSerializer
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlin.time.Duration
import kotlin.time.DurationUnit
import kotlin.time.toDuration

internal object DurationSerializer : KSerializer<Duration> {
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor(
"com.github.topi314.sponsorblock.plugin.protocol.serialization.DurationonAsMilliseconds", PrimitiveKind.LONG
)

override fun deserialize(decoder: Decoder): Duration = decoder.decodeLong().toDuration(DurationUnit.MILLISECONDS)

override fun serialize(encoder: Encoder, value: Duration): Unit = encoder.encodeLong(value.inWholeMilliseconds)
}
68 changes: 53 additions & 15 deletions plugins/sponsorblock/src/commonMain/kotlin/model/Events.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonClassDiscriminator
import kotlin.time.Duration
import kotlin.time.DurationUnit
import kotlin.time.toDuration
import dev.schlaubi.lavakord.plugins.sponsorblock.model.Event as LavakordEvent

@JsonClassDiscriminator("type")
Expand All @@ -30,30 +28,53 @@ internal sealed interface Event : SponsorblockEvent {
override val guildId: ULong,
override val segment: SponsorblockSegment
) : LavakordEvent, SegmentSkippedEvent

@SerialName("ChaptersLoaded")
@Serializable
data class ChaptersLoaded(
override val op: String,
override val guildId: ULong,
override val chapters: List<YouTubeChapter>
) : LavakordEvent, ChaptersLoadedEvent

@SerialName("ChapterStarted")
@Serializable
data class ChapterStarted(
override val op: String,
override val guildId: ULong,
override val chapter: YouTubeChapter
) : LavakordEvent, ChapterStartedEvent
}

/**
* Representation of a Sponsorblock segment.
*
* @property category the [Category] of this segment
* @property startMs the milliseconds of the position where the segment started
* @property endMs the milliseconds of the position where the segment ended
* @property start the [position][Duration] where the segment started
* @property end the [position][Duration] where the segment ended
*/
@Serializable
public data class SponsorblockSegment(
val category: Category,
@SerialName("start")
val startMs: Int,
@SerialName("end")
val endMs: Int
) {
public val start: Duration
get() = startMs.toDuration(DurationUnit.MILLISECONDS)
public val end: Duration
get() = endMs.toDuration(DurationUnit.MILLISECONDS)
}
val start: @Serializable(with = DurationSerializer::class) Duration,
val end: @Serializable(with = DurationSerializer::class) Duration
)

/**
* Representation of a YouTube chapter.
*
* @property name the name of the chapter
* @property start the [position][Duration] where the chapter started
* @property end the [position][Duration] where the chapter ended
* @property duration the [Duration] of the chapter
*/
@Serializable
public data class YouTubeChapter(
val name: String,
val start: @Serializable(with = DurationSerializer::class) Duration,
val end: @Serializable(with = DurationSerializer::class) Duration,
val duration: @Serializable(with = DurationSerializer::class) Duration
)

/**
* Super class for all sponsor block events.
Expand All @@ -75,6 +96,23 @@ public interface SegmentsLoadedEvent : SponsorblockEvent {
* @property segment the [segment][SponsorblockSegment] which was skipped
*/
public interface SegmentSkippedEvent : SponsorblockEvent {

public val segment: SponsorblockSegment
}

/**
* Event fired when the segments for a track were loaded.
*
* @property chapters the [chapters][YouTubeChapter] for this track.
*/
public interface ChaptersLoadedEvent : SponsorblockEvent {
public val chapters: List<YouTubeChapter>
}

/**
* Event fired when a chapter began.
*
* @property chapter the [chapter][YouTubeChapter] which started.
*/
public interface ChapterStartedEvent : SponsorblockEvent {
public val chapter: YouTubeChapter
}

0 comments on commit 4cb5d8c

Please sign in to comment.