pub struct Swarm {
pub ecs_lock: Arc<Mutex<World>>,
pub resolved_address: Arc<RwLock<SocketAddr>>,
pub address: Arc<RwLock<ServerAddress>>,
pub instance_container: Arc<RwLock<InstanceContainer>>,
/* private fields */
}
Expand description
A swarm is a way to conveniently control many bots at once, while also being able to control bots at an individual level when desired.
Swarms are created from SwarmBuilder
.
The S
type parameter is the type of the state for individual bots.
It’s used to make the Swarm::add
function work.
Fields§
§ecs_lock: Arc<Mutex<World>>
§resolved_address: Arc<RwLock<SocketAddr>>
§address: Arc<RwLock<ServerAddress>>
§instance_container: Arc<RwLock<InstanceContainer>>
Implementations§
Source§impl Swarm
impl Swarm
Make a bot Swarm
.
§Examples
use azalea::{prelude::*, swarm::prelude::*};
use std::time::Duration;
#[derive(Default, Clone, Component)]
struct State {}
#[derive(Default, Clone, Resource)]
struct SwarmState {}
#[tokio::main]
async fn main() {
let mut accounts = Vec::new();
let mut states = Vec::new();
for i in 0..10 {
accounts.push(Account::offline(&format!("bot{i}")));
states.push(State::default());
}
SwarmBuilder::new()
.add_accounts(accounts.clone())
.set_handler(handle)
.set_swarm_handler(swarm_handle)
.join_delay(Duration::from_millis(1000))
.start("localhost")
.await
.unwrap();
}
async fn handle(bot: Client, event: Event, _state: State) -> anyhow::Result<()> {
match &event {
_ => {}
}
Ok(())
}
async fn swarm_handle(
mut swarm: Swarm,
event: SwarmEvent,
_state: SwarmState,
) -> anyhow::Result<()> {
match &event {
SwarmEvent::Disconnect(account, join_opts) => {
// automatically reconnect after 5 seconds
tokio::time::sleep(Duration::from_secs(5)).await;
swarm.add_with_opts(account, State::default(), join_opts).await?;
}
SwarmEvent::Chat(m) => {
println!("{}", m.message().to_ansi());
}
_ => {}
}
Ok(())
}
Sourcepub async fn add<S: Component + Clone>(
&mut self,
account: &Account,
state: S,
) -> Result<Client, JoinError>
pub async fn add<S: Component + Clone>( &mut self, account: &Account, state: S, ) -> Result<Client, JoinError>
Add a new account to the swarm. You can remove it later by calling
Client::disconnect
.
§Errors
Returns an Err
if the bot could not do a handshake successfully.
Sourcepub async fn add_with_opts<S: Component + Clone>(
&mut self,
account: &Account,
state: S,
join_opts: &JoinOpts,
) -> Result<Client, JoinError>
pub async fn add_with_opts<S: Component + Clone>( &mut self, account: &Account, state: S, join_opts: &JoinOpts, ) -> Result<Client, JoinError>
Sourcepub async fn add_and_retry_forever<S: Component + Clone>(
&mut self,
account: &Account,
state: S,
) -> Client
pub async fn add_and_retry_forever<S: Component + Clone>( &mut self, account: &Account, state: S, ) -> Client
Add a new account to the swarm, retrying if it couldn’t join. This will run forever until the bot joins or the task is aborted.
This does exponential backoff (though very limited), starting at 5 seconds and doubling up to 15 seconds.
Sourcepub async fn add_and_retry_forever_with_opts<S: Component + Clone>(
&mut self,
account: &Account,
state: S,
opts: &JoinOpts,
) -> Client
pub async fn add_and_retry_forever_with_opts<S: Component + Clone>( &mut self, account: &Account, state: S, opts: &JoinOpts, ) -> Client
Same as Self::add_and_retry_forever
, but allow passing custom join
options.
Trait Implementations§
Source§impl IntoIterator for Swarm
impl IntoIterator for Swarm
impl Resource for Swarm
Auto Trait Implementations§
impl Freeze for Swarm
impl !RefUnwindSafe for Swarm
impl Send for Swarm
impl Sync for Swarm
impl Unpin for Swarm
impl !UnwindSafe for Swarm
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.§impl<T> Downcast for Twhere
T: AsAny + ?Sized,
impl<T> Downcast for Twhere
T: AsAny + ?Sized,
§fn downcast_ref<T>(&self) -> Option<&T>where
T: AsAny,
fn downcast_ref<T>(&self) -> Option<&T>where
T: AsAny,
Any
.§fn downcast_mut<T>(&mut self) -> Option<&mut T>where
T: AsAny,
fn downcast_mut<T>(&mut self) -> Option<&mut T>where
T: AsAny,
Any
.