Skip to content

A collection of kotlin extensions for JDA that make use with reactor-core easier.

License

Notifications You must be signed in to change notification settings

JDA-Applications/jda-reactor

 
 

Repository files navigation

version

jda-reactor

A collection of kotlin extensions for JDA that make use with reactor-core easier.

Installation

Replace the $VERSION with the latest release version.
Replace $JDA_VERSION with the latest stable JDA v4 release.

Gradle

dependencies {
    implementation("net.dv8tion:JDA:$JDA_VERSION")
    implementation("club.minnced:jda-reactor:$VERSION")
}

repositories {
    jcenter()
}

Maven

<dependency>
    <groupId>net.dv8tion</groupId>
    <artifactId>JDA</artifactId>
    <version>$JDA_VERSION</version>
</dependency>
<dependency>
    <groupId>club.minnced</groupId>
    <artifactId>jda-reactor</artifactId>
    <version>$VERSION</version>
</dependency>
<repository>
    <name>jcenter</name>
    <id>jcenter-bintray</id>
    <url>https://jcenter.bintray.com</url>
</repository>

Examples

Some small example usages of the components supported by this library.

ReactiveEventManager

fun main() {
    // Create a ReactiveEventManager for Flux event streams
    val manager = ReactiveEventManager()
    // subscribe directly on the manager instance
    manager.on<ReadyEvent>()                       // Flux<ReadyEvent>
           .next()                                 // Mono<ReadyEvent>
           .subscribe { println("Ready to go!")  } // Subscribe to event
    manager.on<MessageReceivedEvent>()             // Flux<MessageReceivedEvent>
           .filter { it.contentRaw == "!ping" }    // filter by content
           .map { it.channel }                     // Flux<MessageChannel>
           .map { it.sendMessage("Pong!") }        // Flux<MessageAction>
           .subscribe { it.queue() }               // Subscribe to event -> send message on success

   val jda = JDABuilder(BOT_TOKEN)
        .setEventManager(manager)
        .build()

   // you can also subscribe to events from the JDA instance
   jda.on<ShutdownEvent>()                         // Flux<ShutdownEvent>
      .subscribe { println("That was fun!") }      // Make a statement on shutdown, not guaranteed to run if daemon scheduler (default)
}

Mono/Flux RestAction

fun getMessagesForUser(channel: MessageChannel, user: User): Flux<Message> {
    val action = channel.iterableHistory
    return action.asFlux()                     // Flux<Message>
                 .filter { it.author == user } // filter by user
}
fun sendAndLog(channel: MessageChannel, content: String) {
    val action = channel.sendMessage(content)
    action.asMono()                                             // Mono<Message>
          .flatMap { it.addReaction(EMOTE).asMono() }           // Mono<Void!> = empty mono
          .doOnSuccess { println("${channel.name}: $content") } // add side-effect
          .subscribe()                                          // subscribe to empty stream
}

Entity Observers

fun onNextMessage(channel: MessageChannel, callback: (Message) -> Unit) {
    channel.onMessage()                // Flux<MessageReceivedEvent>
           .next()                     // Mono<MessageReceivedEvent>
           .map { it.message }         // Mono<Message>
           .subscribe { callback(it) }
}

fun onReaction(message: Message, reaction: String): Flux<User> {
    return message.on<MessageReactionAddEvent>()                // Flux<MessageReactionAddEvent>
                  .filter { it.reactionEmote.name == reaction } // Flux<MessageReactionAddEvent> with filter
                  .map { it.user }                              // Flux<User>
}
fun onNameChange(user: User): Flux<String> {
    return user.on<UserUpdateNameEvent>() // Flux<UserUpdateNameEvent>
               .map { it.newName }        // Flux<String>
}

fun onNameChange(channel: VoiceChannel): Flux<String> {
    return channel.onUpdate<VoiceChannelUpdateNameEvent>() // Flux<VoiceChannelUpdateNameEvent>
                  .map { it.newName }                      // Flux<String>
}

About

A collection of kotlin extensions for JDA that make use with reactor-core easier.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Kotlin 70.7%
  • Java 29.3%