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

Rewrite collectors #2240

Merged
merged 1 commit into from
Nov 9, 2022
Merged

Rewrite collectors #2240

merged 1 commit into from
Nov 9, 2022

Conversation

kangalio
Copy link
Collaborator

@kangalio kangalio commented Oct 24, 2022

They were overengineered and a bit complicated to use. Now, each collector has just one corresponding type, with methods collect_stream() or collect_single(), depending on what you need.

The implementation is much simplified, too.

Previously, each collector type had a method on Context, a method on ShardMessenger, a variant on ShardRunnerMessage, a branch in ShardRunner::handle_filters and a branch in ShardRunner::handle_rx_value. But they all fundamentally do the same thing; so I replaced all of that with a single CollectorCallback system that all collectors use. This reduces the collector-related code in the gateway to like a couple lines of code.

collector.rs is now a single file, with a collector primitive function called collect() which is enough to implement any possible collector. The existing predefined collectors for messages, component interactions, modal interactions, reactions, and generic events still exist though. Those collector structs are generated using a macro, because their structure is very consistent (due to them being mostly dumb wrapper around ´collect()`)

I removed the removed filter on ReactionCollector because listening for removed reactions seems very niche, and (like anything else), can be achieved with the collect() primitive as well. So ReactionCollector now always collects added reactions. The collect() docs show an example of how to collect removed reactions.

I also removed the filters on EventCollector. Filtering for specific events is better done using collect() with a match statement over Event, like is shown in collect()'s example

@github-actions github-actions bot added client Related to the `client` module. collector Related to the `collector` module. examples Related to Serenity's examples. gateway Related to the `gateway` module. model Related to the `model` module. labels Oct 24, 2022
@kangalio
Copy link
Collaborator Author

kangalio commented Oct 25, 2022

New API:
image

Compare against current, next

@kangalio
Copy link
Collaborator Author

kangalio commented Oct 25, 2022

I deprecated EventCollector because its approach of "first match event type, then later fallibly unwrap" is suboptimal. Same reason if option.is_some() { option.unwrap() } is discouraged. You can see this nicely in the e10_collector diff:
Screenshot_20221025_224927

kangalio added a commit to kangalio/serenity that referenced this pull request Oct 27, 2022
serenity-rs#2228 changed CreateAutocompleteResponse to be a stand-alone response struct into just the representation of the `data` response struct field. That's because you're supposed to embed it in the `CreateInteractionResponse` enum now. But I forgot that there exists `create_autocomplete_response`, a convenience function for `create_response`, which took CreateAutocompleteResponse directly. After serenity-rs#2228, create_autocomplete_response still compiled, but now didn't send a proper response to discord but just its `data` field.

This PR removes create_autocomplete_response as a temporary fix. For now, you should use create_response as for any other interaction response.

Later, there should be a PR that re-adds convenience interaction response methods, but do it proper this time, i.e. not just for autocomplete, but also for the other response types. This undertaking should happen after serenity-rs#2229 is merged (which itself is currently waiting on serenity-rs#2240), because otherwise we'd have to duplicate the convenience methods for CommandInteraction, ComponentInteraction, and ModalInteraction.
@arqunis arqunis added enhancement An improvement to Serenity. breaking change The public API is changed, resulting in miscompilations or unexpected new behaviour for users labels Oct 30, 2022
@github-actions github-actions bot added the builder Related to the `builder` module. label Nov 6, 2022
@kangalio
Copy link
Collaborator Author

kangalio commented Nov 6, 2022

I added some comments in collector.rs to explain how make_specific_collector works:

make_specific_collector!(
    // First line has name of the collector type, and the type of the collected items.
    ComponentInteractionCollector, ComponentInteraction,
    // This defines the extractor pattern, which extracts the data we want to collect from an Event.
    Event::InteractionCreate(InteractionCreateEvent {
        interaction: Interaction::Component(interaction),
    }) => interaction,
    // All following lines define built-in filters of the collector.
    // Each line consists of:
    // - the filter name (the name of the generated builder-like method on the collector type)
    // - filter argument type (used as argument of the builder-like method on the collector type)
    // - filter expression (this expressoin must return true to let the event through)
    author_id: UserId => interaction.user.id == *author_id,
    channel_id: ChannelId => interaction.channel_id == *channel_id,
    guild_id: GuildId => interaction.guild_id.map_or(true, |x| x == *guild_id),
    message_id: MessageId => interaction.message.id == *message_id,
    custom_ids: Vec<String> => custom_ids.contains(&interaction.data.custom_id),
);

@kangalio kangalio closed this Nov 9, 2022
@github-actions github-actions bot removed builder Related to the `builder` module. model Related to the `model` module. gateway Related to the `gateway` module. client Related to the `client` module. examples Related to Serenity's examples. collector Related to the `collector` module. labels Nov 9, 2022
@kangalio kangalio reopened this Nov 9, 2022
@github-actions github-actions bot added builder Related to the `builder` module. cache Related to the `cache`-feature. client Related to the `client` module. labels Nov 9, 2022
@github-actions github-actions bot added collector Related to the `collector` module. examples Related to Serenity's examples. gateway Related to the `gateway` module. http Related to the `http` module. model Related to the `model` module. labels Nov 9, 2022
@kangalio
Copy link
Collaborator Author

kangalio commented Nov 9, 2022

What's blocking this PR?

@arqunis arqunis merged commit c259280 into serenity-rs:next Nov 9, 2022
arqunis added a commit to arqunis/serenity that referenced this pull request Nov 9, 2022
This reverts commit c259280.

I presumed it was Github being confused with which changes to show when
merging the pull request (serenity-rs#2240), but they were real, reverting a few
commits on `next` as a result.
arqunis pushed a commit that referenced this pull request Nov 13, 2022
arqunis added a commit to arqunis/serenity that referenced this pull request Nov 13, 2022
This reverts commit c259280.

I presumed it was Github being confused with which changes to show when
merging the pull request (serenity-rs#2240), but they were real, reverting a few
commits on `next` as a result.
mkrasnitski pushed a commit to mkrasnitski/serenity that referenced this pull request Feb 28, 2023
mkrasnitski pushed a commit to mkrasnitski/serenity that referenced this pull request Feb 28, 2023
This reverts commit c259280.

I presumed it was Github being confused with which changes to show when
merging the pull request (serenity-rs#2240), but they were real, reverting a few
commits on `next` as a result.
mkrasnitski pushed a commit to mkrasnitski/serenity that referenced this pull request May 18, 2023
mkrasnitski pushed a commit to mkrasnitski/serenity that referenced this pull request May 18, 2023
This reverts commit c259280.

I presumed it was Github being confused with which changes to show when
merging the pull request (serenity-rs#2240), but they were real, reverting a few
commits on `next` as a result.
mkrasnitski pushed a commit to mkrasnitski/serenity that referenced this pull request May 30, 2023
mkrasnitski pushed a commit to mkrasnitski/serenity that referenced this pull request May 30, 2023
This reverts commit c259280.

I presumed it was Github being confused with which changes to show when
merging the pull request (serenity-rs#2240), but they were real, reverting a few
commits on `next` as a result.
mkrasnitski pushed a commit to mkrasnitski/serenity that referenced this pull request Sep 21, 2023
mkrasnitski pushed a commit to mkrasnitski/serenity that referenced this pull request Sep 21, 2023
This reverts commit c259280.

I presumed it was Github being confused with which changes to show when
merging the pull request (serenity-rs#2240), but they were real, reverting a few
commits on `next` as a result.
mkrasnitski pushed a commit to mkrasnitski/serenity that referenced this pull request Oct 17, 2023
mkrasnitski pushed a commit to mkrasnitski/serenity that referenced this pull request Oct 17, 2023
This reverts commit c259280.

I presumed it was Github being confused with which changes to show when
merging the pull request (serenity-rs#2240), but they were real, reverting a few
commits on `next` as a result.
mkrasnitski pushed a commit to mkrasnitski/serenity that referenced this pull request Oct 24, 2023
mkrasnitski pushed a commit to mkrasnitski/serenity that referenced this pull request Oct 24, 2023
This reverts commit c259280.

I presumed it was Github being confused with which changes to show when
merging the pull request (serenity-rs#2240), but they were real, reverting a few
commits on `next` as a result.
arqunis pushed a commit to arqunis/serenity that referenced this pull request Oct 24, 2023
arqunis added a commit to arqunis/serenity that referenced this pull request Oct 24, 2023
This reverts commit c259280.

I presumed it was Github being confused with which changes to show when
merging the pull request (serenity-rs#2240), but they were real, reverting a few
commits on `next` as a result.
arqunis pushed a commit to arqunis/serenity that referenced this pull request Oct 24, 2023
arqunis added a commit to arqunis/serenity that referenced this pull request Oct 24, 2023
This reverts commit c259280.

I presumed it was Github being confused with which changes to show when
merging the pull request (serenity-rs#2240), but they were real, reverting a few
commits on `next` as a result.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change The public API is changed, resulting in miscompilations or unexpected new behaviour for users builder Related to the `builder` module. cache Related to the `cache`-feature. client Related to the `client` module. collector Related to the `collector` module. enhancement An improvement to Serenity. examples Related to Serenity's examples. gateway Related to the `gateway` module. http Related to the `http` module. model Related to the `model` module.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants