Skip to content

Commit

Permalink
Rename Context::shard_manager back to shard
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrasnitski committed Feb 8, 2025
1 parent b4e564a commit 9588965
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 37 deletions.
32 changes: 14 additions & 18 deletions examples/e09_collectors/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@ impl EventHandler for Handler {
// There is a method implemented for some models to conveniently collect replies. They
// return a builder that can be turned into a Stream, or here, where we can await a
// single reply
let collector = msg
.author
.id
.collect_messages(ctx.shard_messenger.clone())
.timeout(Duration::from_secs(10));
let collector =
msg.author.id.collect_messages(ctx.shard.clone()).timeout(Duration::from_secs(10));
if let Some(answer) = collector.await {
if answer.content.to_lowercase() == "ferris" {
let _ = answer.reply(&ctx.http, "That's correct!").await;
Expand All @@ -50,7 +47,7 @@ impl EventHandler for Handler {
// The message model can also be turned into a Collector to collect reactions on it.
let collector = react_msg
.id
.collect_reactions(ctx.shard_messenger.clone())
.collect_reactions(ctx.shard.clone())
.timeout(Duration::from_secs(10))
.author_id(msg.author.id);

Expand All @@ -68,7 +65,7 @@ impl EventHandler for Handler {
let _ = msg.reply(&ctx.http, "Write 5 messages in 10 seconds").await;

// We can create a collector from scratch too using this builder future.
let collector = MessageCollector::new(ctx.shard_messenger.clone())
let collector = MessageCollector::new(ctx.shard.clone())
// Only collect messages by this user.
.author_id(msg.author.id)
.channel_id(msg.channel_id)
Expand Down Expand Up @@ -103,17 +100,16 @@ impl EventHandler for Handler {
// We can also collect arbitrary events using the collect() function. For example, here we
// collect updates to the messages that the user sent above and check for them updating all
// 5 of them.
let mut collector =
serenity::collector::collect(&ctx.shard_messenger, move |event| match event {
// Only collect MessageUpdate events for the 5 MessageIds we're interested in.
Event::MessageUpdate(event)
if collected.iter().any(|msg| event.message.id == msg.id) =>
{
Some(event.message.id)
},
_ => None,
})
.take_until(Box::pin(tokio::time::sleep(Duration::from_secs(20))));
let mut collector = serenity::collector::collect(&ctx.shard, move |event| match event {
// Only collect MessageUpdate events for the 5 MessageIds we're interested in.
Event::MessageUpdate(event)
if collected.iter().any(|msg| event.message.id == msg.id) =>
{
Some(event.message.id)
},
_ => None,
})
.take_until(Box::pin(tokio::time::sleep(Duration::from_secs(20))));

let _ = msg.reply(&ctx.http, "Edit each of those 5 messages in 20 seconds").await;
let mut edited = HashSet::new();
Expand Down
4 changes: 2 additions & 2 deletions examples/e14_message_components/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl EventHandler for Handler {
// manually in the EventHandler.
let interaction = match m
.id
.collect_component_interactions(ctx.shard_messenger.clone())
.collect_component_interactions(ctx.shard.clone())
.timeout(Duration::from_secs(60 * 3))
.await
{
Expand Down Expand Up @@ -108,7 +108,7 @@ impl EventHandler for Handler {

// Wait for multiple interactions
let mut interaction_stream =
m.id.collect_component_interactions(ctx.shard_messenger.clone())
m.id.collect_component_interactions(ctx.shard.clone())
.timeout(Duration::from_secs(60 * 3))
.stream();

Expand Down
11 changes: 5 additions & 6 deletions examples/testing/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ async fn message(ctx: &Context, msg: Message) -> Result<(), serenity::Error> {
.await?;
let button_press = msg
.id
.collect_component_interactions(ctx.shard_messenger.clone())
.collect_component_interactions(ctx.shard.clone())
.timeout(std::time::Duration::from_secs(10))
.await;
match button_press {
Expand Down Expand Up @@ -181,11 +181,10 @@ async fn message(ctx: &Context, msg: Message) -> Result<(), serenity::Error> {
.await?;

let msg_id = msg.id;
let mut message_updates =
serenity::collector::collect(&ctx.shard_messenger, move |ev| match ev {
Event::MessageUpdate(x) if x.message.id == msg_id => Some(()),
_ => None,
});
let mut message_updates = serenity::collector::collect(&ctx.shard, move |ev| match ev {
Event::MessageUpdate(x) if x.message.id == msg_id => Some(()),
_ => None,
});
let _ = tokio::time::timeout(Duration::from_millis(2000), message_updates.next()).await;
msg.edit(&ctx, EditMessage::new().suppress_embeds(true)).await?;
} else if msg.content == "voicemessage" {
Expand Down
2 changes: 1 addition & 1 deletion src/collector/quick_modal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl<'a> CreateQuickModal<'a> {
);
builder.execute(&ctx.http, interaction_id, token).await?;

let collector = ModalInteractionCollector::new(ctx.shard_messenger.clone())
let collector = ModalInteractionCollector::new(ctx.shard.clone())
.custom_ids(vec![FixedString::from_str_trunc(&modal_custom_id)]);

let collector = match self.timeout {
Expand Down
20 changes: 10 additions & 10 deletions src/gateway/client/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct Context {
/// [`Client::data`]: super::Client::data
data: Arc<dyn std::any::Any + Send + Sync>,
/// The messenger to communicate with the shard runner.
pub shard_messenger: ShardMessenger,
pub shard: ShardMessenger,
/// The ID of the shard this context is related to.
pub shard_id: ShardId,
pub http: Arc<Http>,
Expand All @@ -42,7 +42,7 @@ pub struct Context {
impl fmt::Debug for Context {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Context")
.field("shard_messenger", &self.shard_messenger)
.field("shard", &self.shard)
.field("shard_id", &self.shard_id)
.finish_non_exhaustive()
}
Expand All @@ -69,7 +69,7 @@ impl Context {
) -> Context {
Context {
data,
shard_messenger,
shard: shard_messenger,
shard_id,
http,
#[cfg(feature = "cache")]
Expand Down Expand Up @@ -131,7 +131,7 @@ impl Context {
///
/// [`Online`]: OnlineStatus::Online
pub fn online(&self) {
self.shard_messenger.set_status(OnlineStatus::Online);
self.shard.set_status(OnlineStatus::Online);
}

/// Sets the current user as being [`Idle`]. This maintains the current activity.
Expand All @@ -158,7 +158,7 @@ impl Context {
///
/// [`Idle`]: OnlineStatus::Idle
pub fn idle(&self) {
self.shard_messenger.set_status(OnlineStatus::Idle);
self.shard.set_status(OnlineStatus::Idle);
}

/// Sets the current user as being [`DoNotDisturb`]. This maintains the current activity.
Expand All @@ -185,7 +185,7 @@ impl Context {
///
/// [`DoNotDisturb`]: OnlineStatus::DoNotDisturb
pub fn dnd(&self) {
self.shard_messenger.set_status(OnlineStatus::DoNotDisturb);
self.shard.set_status(OnlineStatus::DoNotDisturb);
}

/// Sets the current user as being [`Invisible`]. This maintains the current activity.
Expand All @@ -212,7 +212,7 @@ impl Context {
///
/// [`Invisible`]: OnlineStatus::Invisible
pub fn invisible(&self) {
self.shard_messenger.set_status(OnlineStatus::Invisible);
self.shard.set_status(OnlineStatus::Invisible);
}

/// "Resets" the current user's presence, by setting the activity to [`None`] and the online
Expand Down Expand Up @@ -243,7 +243,7 @@ impl Context {
/// [`Event::Resumed`]: crate::model::event::Event::Resumed
/// [`Online`]: OnlineStatus::Online
pub fn reset_presence(&self) {
self.shard_messenger.set_presence(None, OnlineStatus::Online);
self.shard.set_presence(None, OnlineStatus::Online);
}

/// Sets the current activity.
Expand Down Expand Up @@ -271,7 +271,7 @@ impl Context {
/// }
/// ```
pub fn set_activity(&self, activity: Option<ActivityData>) {
self.shard_messenger.set_activity(activity);
self.shard.set_activity(activity);
}

/// Sets the current user's presence, providing all fields to be passed.
Expand Down Expand Up @@ -319,7 +319,7 @@ impl Context {
/// [`DoNotDisturb`]: OnlineStatus::DoNotDisturb
/// [`Idle`]: OnlineStatus::Idle
pub fn set_presence(&self, activity: Option<ActivityData>, status: OnlineStatus) {
self.shard_messenger.set_presence(activity, status);
self.shard.set_presence(activity, status);
}

/// Gets all emojis for the current application.
Expand Down

0 comments on commit 9588965

Please sign in to comment.