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 way to intercept events and insert custom context #667

Merged
merged 15 commits into from
Aug 18, 2022

Conversation

lukellmann
Copy link
Member

@lukellmann lukellmann commented Aug 12, 2022

This PR adresses #665 (comment) by adding a way to insert a custom object into events.

The way to do this is by overwriting GatewayEventInterceptor for a Kord instance. The interceptor can then assign a customContext when creating core events. The interceptor can be changed via gatewayEventInterceptor in KordBuilder. For convenience, DefaultGatewayEventInterceptor can be created with a lambda that creates the object that will be assigned to customContext.

Code example:

class YourCustomContext(...)

val kord = Kord(token) {
    gatewayEventInterceptor = DefaultGatewayEventInterceptor(
        customContextCreator = { event, kord -> YourCustomContext(...) }
    )
}

kord.on<MessageCreateEvent> {
    // receive the value previously set when creating the event
    val context = customContext as YourCustomContext
    // ...
}

kord.login()

Note that the reference to customContext will be copied by withStrategy() of Strategizable events.

See this conversation (especially this message) for the decision to use Any? for customContext.

This PR also fixes withStrategy() for TypingStartEvent, ThreadListSyncEvent, InviteCreateEvent and ReactionRemoveEmojiEvent.

# Conflicts:
#	core/src/main/kotlin/gateway/handler/ChannelEventHandler.kt
#	core/src/main/kotlin/gateway/handler/GuildEventHandler.kt
#	core/src/main/kotlin/gateway/handler/InteractionEventHandler.kt
#	core/src/main/kotlin/gateway/handler/LifeCycleEventHandler.kt
#	core/src/main/kotlin/gateway/handler/MessageEventHandler.kt
#	core/src/main/kotlin/gateway/handler/ThreadEventHandler.kt
#	core/src/main/kotlin/gateway/handler/VoiceEventHandler.kt
#	core/src/main/kotlin/gateway/handler/WebhookEventHandler.kt
@lukellmann lukellmann marked this pull request as ready for review August 16, 2022 22:19
@lukellmann
Copy link
Member Author

lukellmann commented Aug 16, 2022

After #665 is merged, I would like to merge 0.8.x into this PR before merging it for a better diff.
Have done this now.

# Conflicts:
#	core/src/main/kotlin/event/channel/ChannelCreateEvent.kt
#	core/src/main/kotlin/event/channel/ChannelDeleteEvent.kt
#	core/src/main/kotlin/event/channel/ChannelUpdateEvent.kt
#	core/src/main/kotlin/event/channel/thread/ThreadCreateEvent.kt
#	core/src/main/kotlin/event/channel/thread/ThreadDeleteEvent.kt
#	core/src/main/kotlin/event/channel/thread/ThreadMemberUpdateEvent.kt
#	core/src/main/kotlin/event/channel/thread/ThreadMembersUpdateEvent.kt
#	core/src/main/kotlin/event/channel/thread/ThreadUpdateEvent.kt
#	core/src/main/kotlin/event/gateway/Events.kt
#	core/src/main/kotlin/event/guild/GuildCreateEvent.kt
#	core/src/main/kotlin/event/guild/GuildDeleteEvent.kt
#	core/src/main/kotlin/event/guild/GuildUpdateEvent.kt
#	core/src/main/kotlin/event/guild/MemberLeaveEvent.kt
#	core/src/main/kotlin/event/interaction/ApplicationCommandCreate.kt
#	core/src/main/kotlin/event/interaction/ApplicationCommandDelete.kt
#	core/src/main/kotlin/event/interaction/ApplicationCommandInteractionCreate.kt
#	core/src/main/kotlin/event/interaction/ApplicationCommandPermissionsUpdateEvent.kt
#	core/src/main/kotlin/event/interaction/ApplicationCommandUpdate.kt
#	core/src/main/kotlin/event/interaction/AutoCompleteInteractionCreate.kt
#	core/src/main/kotlin/event/interaction/ComponentInteractionCreate.kt
#	core/src/main/kotlin/event/interaction/ModalSubmitInteractionCreate.kt
#	core/src/main/kotlin/event/user/UserUpdateEvent.kt
#	core/src/main/kotlin/event/user/VoiceStateUpdateEvent.kt
#	core/src/main/kotlin/gateway/handler/BaseGatewayEventHandler.kt
#	core/src/main/kotlin/gateway/handler/ChannelEventHandler.kt
#	core/src/main/kotlin/gateway/handler/DefaultGatewayEventInterceptor.kt
#	core/src/main/kotlin/gateway/handler/GuildEventHandler.kt
#	core/src/main/kotlin/gateway/handler/InteractionEventHandler.kt
#	core/src/main/kotlin/gateway/handler/LifeCycleEventHandler.kt
#	core/src/main/kotlin/gateway/handler/MessageEventHandler.kt
#	core/src/main/kotlin/gateway/handler/ThreadEventHandler.kt
#	core/src/main/kotlin/gateway/handler/UserEventHandler.kt
#	core/src/main/kotlin/gateway/handler/VoiceEventHandler.kt
#	core/src/main/kotlin/gateway/handler/WebhookEventHandler.kt
@gdude2002
Copy link
Contributor

gdude2002 commented Aug 18, 2022

The changeset is too large for me to review by hand, but the functionality as presented by this PR description looks fine and should be plenty sufficient for KordEx.

@lukellmann
Copy link
Member Author

The most important changes are in the following files:

  • core/src/main/kotlin/builder/kord/KordBuilder.kt
  • core/src/main/kotlin/event/Event.kt
  • core/src/main/kotlin/gateway/handler/DefaultGatewayEventInterceptor.kt
  • core/src/main/kotlin/gateway/handler/GatewayEventInterceptor.kt

@HopeBaron HopeBaron merged commit 1bdd345 into 0.8.x Aug 18, 2022
@lukellmann lukellmann deleted the feature/custom-event-context branch August 18, 2022 19:03
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.

3 participants