Expand description
Collectors: streams of the messages, reactions, component interactions and modal submissions that arrive while a bot waits for them (D-263–D-269).
A collector watches one thing — the messages of a channel, the reactions or components of
a message, the components of an interaction’s reply, the modals a user submits — and
yields what arrives there as a [Stream]: read it with while let Some(item) = collector.next().await. It is configured with a CollectorBuilder:
time: end after this long;idle: end when nothing was collected for this long;max: end after collecting this many;max_processed: end after this many arrived, whether the filter accepted them or not;filter: collect only what the filter accepts;- for reactions and components,
max_users; for reactions,max_emojisanddispose(also yield removals); for components,component_type.
The collector also ends when what it watches is deleted (its message, channel, thread or
guild), when Collector::stop is called, or when the client shuts down; afterwards
Collector::end_reason tells why. The await_* shortcuts wait for one item
(await_component,
await_modal_submit: a
Result<_, CollectorError>) or collect until the end
(await_messages,
await_reactions: a Collected).
Collected events still reach the event handlers and the router’s routes (D-266): a collector is one more listener, as in discord.js, so a bot must not answer the same interaction from a collector and from a route.
§Sequential handlers
With HandlerMode::Sequential, a handler that waits for
a collector blocks its shard: the collector receives nothing from that shard until it
ends by time (D-267). The client logs a warning when a collector is created in this mode;
the default Concurrent mode has no such limit.
§Examples
client.on(Events::MessageCreate, |ctx, msg| async move {
if msg.content != "!quiz" {
return Ok(());
}
msg.reply(&ctx, "What is 6 × 7? You have 30 seconds.").await?;
let author = msg.author.id;
let answers = ctx
.await_messages(msg.channel_id)
.filter(move |answer| answer.author.id == author)
.max(1)
.time(Duration::from_secs(30))
.await;
match answers.first() {
Some(answer) if answer.content.trim() == "42" => answer.reply(&ctx, "Correct!").await?,
Some(answer) => answer.reply(&ctx, "No, it is 42.").await?,
None => msg.reply(&ctx, "Time is up: it was 42.").await?,
};
Ok(())
});discord.js counterpart: Collector and its subclasses MessageCollector,
ReactionCollector and InteractionCollector.
Modules§
- mode
- The kinds of builders: what a
CollectorBuildermakes.
Structs§
- Await
All - The future of a shortcut collecting until the end (
await_messages,await_reactions). - Await
First - The future of a shortcut waiting for one item (
await_component,await_modal_submit). - Collected
- What the collecting shortcuts return (D-268): the
Collectionof what was collected (throughDeref) and why the collector ended. An empty result is not an error. - Collected
Reaction - One emoji’s reactions gathered by
await_reactions. - Collector
- A running collector: a [
Stream] of what it collects, ending (None) when the collector ends;end_reasonthen tells why. - Collector
Builder - Configures a collector; made by the extension methods (
component_collector,await_messages, …) and theContextmethods taking IDs. - Collector
Error - The error of the shortcuts that wait for one item
(
await_component,await_modal_submit): the collector ended before anything was collected (D-268).
Enums§
- EndReason
- Why a collector ended.
- Reaction
Event - What a reaction collector yields.
Traits§
- Collectable
- What collectors can yield:
ComponentInteraction,ModalInteraction,Arc<MessageCreate>andReactionEvent. This trait is sealed.
Functions§
- reaction_
key - The key of an emoji in
await_reactions’ result: a custom emoji’s ID, or a Unicode emoji itself ("👍"), like discord.js’ reaction keys.