Skip to main content

zaun_client/
lib.rs

1//! A Zaun community client over SimpleX: join, post, moderate and cache communities.
2//!
3//! [`Client`] runs one SimpleX client and keeps a post-quantum connection per community to the
4//! community's host (`zaun-host`). It verifies the host, registers a member key, and keeps a
5//! local cache of each community (channels, roles, members) up to date from the host's events.
6//! In the spirit of discord.js, what it reports are objects that act for themselves:
7//! [`Community`], [`Channel`], [`Message`], [`Member`], [`Role`], [`Attachment`].
8//! [`Client::command`] sends any command the objects do not wrap.
9//!
10//! ```no_run
11//! use zaun_client::{Client, Event};
12//!
13//! let client = Client::builder("./bot").name("Ping bot").key("store passphrase").start()?;
14//! let community = client.join("zaun://join/…", "Ping bot")?;
15//! let general = community.channel_by_name("general").expect("a general channel");
16//! general.send("hello")?;
17//! for event in client.events() {
18//!     match event {
19//!         Event::MessageCreate(message) if message.content() == "!ping" => {
20//!             message.reply("pong")?;
21//!         }
22//!         Event::MemberAdd(member) => println!("welcome, {member}"),
23//!         _ => {}
24//!     }
25//! }
26//! # Ok::<(), zaun_client::Error>(())
27//! ```
28//!
29//! Messages carry rich embeds as discord.js's do: build one with [`EmbedBuilder`] and post it
30//! with [`MessageOptions`]. Its pictures are files uploaded as the message's attachments, never
31//! remote URLs, so no reader's client fetches anything from a third party.
32//!
33//! Voice channels join like discord.js's: `channel.join()?` gives a [`VoiceSession`], and an
34//! [`AudioPlayer`] subscribed to it plays files for a music bot.
35//!
36//! The host is the authority: the cache and [`Member::can`] are a local view that lets an
37//! application hide what a member cannot do, and the host still decides.
38
39mod api;
40#[cfg(not(target_arch = "wasm32"))]
41mod bots;
42mod budget;
43#[cfg(not(target_arch = "wasm32"))]
44mod builder;
45mod cache;
46// DM: direct messages with bots.
47#[cfg(not(target_arch = "wasm32"))]
48mod client;
49#[cfg(not(target_arch = "wasm32"))]
50mod compose;
51mod dm;
52mod driver;
53mod edit;
54mod embed;
55mod error;
56mod event;
57#[cfg(not(target_arch = "wasm32"))]
58mod events;
59pub mod files;
60pub mod mention;
61#[cfg(not(target_arch = "wasm32"))]
62mod message_mentions;
63// presence: status, activities and their cache (§10.5).
64mod named;
65#[cfg(not(target_arch = "wasm32"))]
66mod objects;
67mod perms;
68mod pins;
69pub mod presence;
70// Bot registries: catalog, history and package checks for apps.
71pub mod registry;
72mod store;
73mod sx;
74mod typing;
75pub mod ui;
76/// Voice: natively a str0m mesh over UDP; in WebAssembly the same rules (`voice/web.rs`)
77/// over the browser's WebRTC, driven from `wasm/voice.rs`.
78#[cfg_attr(target_arch = "wasm32", path = "voice/web.rs")]
79mod voice;
80#[cfg(target_arch = "wasm32")]
81pub mod wasm;
82mod wire;
83
84#[cfg(test)]
85mod cache_tests;
86#[cfg(test)]
87#[cfg(not(target_arch = "wasm32"))]
88mod events_tests;
89#[cfg(test)]
90#[cfg(not(target_arch = "wasm32"))]
91mod perf_tests;
92#[cfg(test)]
93mod presence_tests;
94#[cfg(test)]
95#[cfg(not(target_arch = "wasm32"))]
96mod registry_tests;
97#[cfg(test)]
98mod typing_tests;
99
100pub use api::HandOff;
101#[cfg(not(target_arch = "wasm32"))]
102pub use bots::{EphemeralReply, Interaction, InteractionFailure, ModalOpen, Response, Suggestions};
103pub use budget::TEXT_BUDGET;
104#[cfg(not(target_arch = "wasm32"))]
105pub use builder::ClientBuilder;
106pub use cache::CommunityData;
107#[cfg(not(target_arch = "wasm32"))]
108pub use client::Client;
109#[cfg(not(target_arch = "wasm32"))]
110pub use compose::{EditMessage, EmbedBuilder, MessageOptions, Picture};
111pub use dm::{DmChannel, DmSettings};
112pub use error::{Error, Result};
113#[cfg(not(target_arch = "wasm32"))]
114pub use events::{Event, Reaction, VoiceLeave, VoiceMute, VoiceState};
115#[cfg(not(target_arch = "wasm32"))]
116pub use objects::{
117    Attachment, Channel, Community, Member, Message, PartialMessage, PendingMessage, Role,
118};
119pub use presence::Presence;
120#[cfg(target_arch = "wasm32")]
121pub use sx::imp::SimplexClosed;
122pub use typing::{TYPING_SHOWN_MS, TypingNow, TypingSeen};
123#[cfg(not(target_arch = "wasm32"))]
124pub use voice::{
125    AudioPlayer, AudioPlayerEvent, AudioPlayerStatus, AudioResource, FfmpegOptions, VoiceEvent,
126    VoiceOptions, VoiceSession,
127};
128
129/// The Zaun community protocol: ids, commands, events, permission bits and the client session.
130pub use zaun_community_protocol as protocol;
131/// A message's rich embed as the protocol has it ([`Message::embeds`]); build one to send with
132/// [`EmbedBuilder`].
133pub use zaun_community_protocol::command::object::{Embed, EmbedAuthor, EmbedField, EmbedFooter};