pub struct Context<B: Backend = DashMapBackend> { /* private fields */ }Expand description
Everything an event handler needs to act: the REST client, the cache and the shard that received the event.
Cheap to clone (a single reference count increment). B is the cache backend of the
Client (D-172); code that does not name it gets the default.
discord.js counterpart: the client reachable from every structure (message.client).
Implementations§
Source§impl<B: Backend> Context<B>
Collectors by ID, for code that holds IDs rather than the channel or the message.
impl<B: Backend> Context<B>
Collectors by ID, for code that holds IDs rather than the channel or the message.
Sourcepub fn message_collector(
&self,
channel: ChannelId,
) -> CollectorBuilder<Arc<MessageCreate>>
pub fn message_collector( &self, channel: ChannelId, ) -> CollectorBuilder<Arc<MessageCreate>>
A collector of the messages sent in channel (D-263, D-269).
It ends at its limits, or when the channel, thread or guild is deleted. Needs the
GUILD_MESSAGES or DIRECT_MESSAGES intent (and MESSAGE_CONTENT to read guild
messages’ content).
discord.js counterpart: TextChannel#createMessageCollector.
§Examples
let mut messages = ctx
.message_collector(msg.channel_id)
.time(Duration::from_secs(60))
.build();
while let Some(message) = messages.next().await {
println!("{}: {}", message.author.username, message.content);
}Sourcepub fn await_messages(
&self,
channel: ChannelId,
) -> CollectorBuilder<Arc<MessageCreate>, All>
pub fn await_messages( &self, channel: ChannelId, ) -> CollectorBuilder<Arc<MessageCreate>, All>
Collects the messages sent in channel until the collector ends, like
message_collector; set a limit (time, max, …), or it
only ends with the channel.
discord.js counterpart: TextChannel#awaitMessages.
Sourcepub fn component_collector(
&self,
channel: ChannelId,
) -> CollectorBuilder<ComponentInteraction>
pub fn component_collector( &self, channel: ChannelId, ) -> CollectorBuilder<ComponentInteraction>
A collector of the component interactions (buttons, select menus) on every message of
channel.
discord.js counterpart: TextChannel#createMessageComponentCollector.
Sourcepub fn await_component(
&self,
channel: ChannelId,
) -> CollectorBuilder<ComponentInteraction, First>
pub fn await_component( &self, channel: ChannelId, ) -> CollectorBuilder<ComponentInteraction, First>
Waits for one component interaction on any message of channel; set a
time, or it only ends with the channel.
§Errors
CollectorError with the reason if the collector ended first.
discord.js counterpart: TextChannel#awaitMessageComponent.
Source§impl<B: Backend> Context<B>
impl<B: Backend> Context<B>
Sourcepub fn application_id(&self) -> Option<ApplicationId>
pub fn application_id(&self) -> Option<ApplicationId>
Returns the bot’s application ID, known from the first READY (D-245); None only in
handlers that run before it (ShardError, Invalidated, ShardDisconnect while
logging in).
discord.js counterpart: client.application.id.
Sourcepub fn cache(&self) -> Option<&Cache<B>>
pub fn cache(&self) -> Option<&Cache<B>>
Returns the cache, if the client has one (ClientBuilder::cache,
D-170). The event being handled is already applied to it (D-171).
discord.js counterpart: the managers’ cache collections (client.guilds.cache, …).
Sourcepub fn shard_id(&self) -> ShardId
pub fn shard_id(&self) -> ShardId
Returns the ID of the shard that received the event.
discord.js counterpart: the shardId some discord.js events pass; Guild#shardId.
Sourcepub fn shard(&self) -> &MessageSender
pub fn shard(&self) -> &MessageSender
Returns a sender for gateway commands on the shard that received the event.
discord.js counterpart: Guild#shard.
Sourcepub fn shutdown(&self)
pub fn shutdown(&self)
Asks the client to disconnect, as Client::shutdown does:
every shard closes, the session is invalidated (the bot appears offline) and
Client::login returns Ok(()). Has no effect once the client
has stopped.
discord.js counterpart: client.destroy() (through message.client).
§Examples
use djs_rs::{model::id::UserId, prelude::*};
const OWNER: UserId = UserId::new(123_456_789_012_345_678);
client.on(Events::MessageCreate, |ctx, msg| async move {
if msg.content == "!stop" && msg.author.id == OWNER {
msg.reply(&ctx, "Bye!").await?;
ctx.shutdown();
}
Ok(())
});Source§impl<B: Backend> Context<B>
Converts REST objects to the cached types through the context’s cache (D-230, D-233),
with the code compiled in djs-rs (D-289).
impl<B: Backend> Context<B>
Converts REST objects to the cached types through the context’s cache (D-230, D-233), with the code compiled in djs-rs (D-289).
Sourcepub const fn guilds(&self) -> GuildManager<'_, B>
pub const fn guilds(&self) -> GuildManager<'_, B>
Returns the guild manager (D-229).
discord.js counterpart: Client#guilds.
Sourcepub const fn channels(&self) -> ChannelManager<'_, B>
pub const fn channels(&self) -> ChannelManager<'_, B>
Returns the channel manager.
discord.js counterpart: Client#channels.
Sourcepub const fn guild_channels(
&self,
guild_id: GuildId,
) -> GuildChannelManager<'_, B>
pub const fn guild_channels( &self, guild_id: GuildId, ) -> GuildChannelManager<'_, B>
Returns the manager of a guild’s channels as a whole: create, list, positions (D-234).
discord.js counterpart: Guild#channels.
Sourcepub const fn users(&self) -> UserManager<'_, B>
pub const fn users(&self) -> UserManager<'_, B>
Returns the user manager.
discord.js counterpart: Client#users.
Sourcepub const fn members(&self, guild_id: GuildId) -> MemberManager<'_, B>
pub const fn members(&self, guild_id: GuildId) -> MemberManager<'_, B>
Returns the manager of a guild’s members.
discord.js counterpart: Guild#members.
Sourcepub const fn messages(&self, channel_id: ChannelId) -> MessageManager<'_, B>
pub const fn messages(&self, channel_id: ChannelId) -> MessageManager<'_, B>
Returns the manager of a channel’s messages.
discord.js counterpart: TextChannel#messages (and TextChannel#send).
Sourcepub const fn roles(&self, guild_id: GuildId) -> RoleManager<'_, B>
pub const fn roles(&self, guild_id: GuildId) -> RoleManager<'_, B>
Returns the manager of a guild’s roles.
discord.js counterpart: Guild#roles.
Sourcepub const fn bans(&self, guild_id: GuildId) -> BanManager<'_, B>
pub const fn bans(&self, guild_id: GuildId) -> BanManager<'_, B>
Returns the manager of a guild’s bans.
discord.js counterpart: Guild#bans.
Sourcepub const fn emojis(&self, guild_id: GuildId) -> EmojiManager<'_, B>
pub const fn emojis(&self, guild_id: GuildId) -> EmojiManager<'_, B>
Returns the manager of a guild’s custom emojis.
discord.js counterpart: Guild#emojis.
Sourcepub const fn stickers(&self, guild_id: GuildId) -> StickerManager<'_, B>
pub const fn stickers(&self, guild_id: GuildId) -> StickerManager<'_, B>
Returns the manager of a guild’s stickers.
discord.js counterpart: Guild#stickers.
Sourcepub const fn scheduled_events(
&self,
guild_id: GuildId,
) -> ScheduledEventManager<'_, B>
pub const fn scheduled_events( &self, guild_id: GuildId, ) -> ScheduledEventManager<'_, B>
Returns the manager of a guild’s scheduled events.
discord.js counterpart: Guild#scheduledEvents.
Sourcepub const fn stage_instances(
&self,
guild_id: GuildId,
) -> StageInstanceManager<'_, B>
pub const fn stage_instances( &self, guild_id: GuildId, ) -> StageInstanceManager<'_, B>
Returns the manager of a guild’s live stages.
discord.js counterpart: Guild#stageInstances.
Sourcepub const fn soundboard_sounds(
&self,
guild_id: GuildId,
) -> SoundboardSoundManager<'_, B>
pub const fn soundboard_sounds( &self, guild_id: GuildId, ) -> SoundboardSoundManager<'_, B>
Returns the manager of a guild’s soundboard sounds.
discord.js counterpart: Guild#soundboardSounds.
Sourcepub const fn threads(&self, channel_id: ChannelId) -> ThreadManager<'_, B>
pub const fn threads(&self, channel_id: ChannelId) -> ThreadManager<'_, B>
Returns the manager of the threads of a parent channel (D-242).
discord.js counterpart: TextChannel#threads / ForumChannel#threads.
Sourcepub const fn thread_members(
&self,
thread_id: ChannelId,
) -> ThreadMemberManager<'_, B>
pub const fn thread_members( &self, thread_id: ChannelId, ) -> ThreadMemberManager<'_, B>
Returns the manager of a thread’s members (D-242).
discord.js counterpart: ThreadChannel#members.
Sourcepub async fn default_soundboard_sounds(
&self,
) -> Result<Collection<SoundboardSoundId, SoundboardSound>, Error>
pub async fn default_soundboard_sounds( &self, ) -> Result<Collection<SoundboardSoundId, SoundboardSound>, Error>
Discord’s default soundboard sounds, available in every guild (List Default Soundboard Sounds; D-243). They are not cached.
discord.js counterpart: Client#fetchDefaultSoundboardSounds.
§Errors
Returns the REST error if the request failed.
Sourcepub fn commands(&self) -> CommandManager<'_, B>
pub fn commands(&self) -> CommandManager<'_, B>
Returns the manager of the bot’s global application commands (D-244).
discord.js counterpart: client.application.commands.
§Panics
Panics before the first READY, while the application ID is unknown (D-247): only in
ShardError, Invalidated or ShardDisconnect handlers running while logging in.
Sourcepub fn guild_commands(&self, guild_id: GuildId) -> GuildCommandManager<'_, B>
pub fn guild_commands(&self, guild_id: GuildId) -> GuildCommandManager<'_, B>
Sourcepub fn application_emojis(&self) -> ApplicationEmojiManager<'_, B>
pub fn application_emojis(&self) -> ApplicationEmojiManager<'_, B>
Sourcepub const fn auto_moderation_rules(
&self,
guild_id: GuildId,
) -> AutoModerationRuleManager<'_, B>
pub const fn auto_moderation_rules( &self, guild_id: GuildId, ) -> AutoModerationRuleManager<'_, B>
Returns the manager of a guild’s auto moderation rules.
discord.js counterpart: Guild#autoModerationRules.
Sourcepub async fn sticker(&self, sticker_id: StickerId) -> Result<Sticker, Error>
pub async fn sticker(&self, sticker_id: StickerId) -> Result<Sticker, Error>
A sticker by its ID, standard or from a guild (Get Sticker). Not cached.
discord.js counterpart: Client#fetchSticker.
§Errors
Returns the REST error if the request failed.
Sourcepub async fn sticker_packs(
&self,
) -> Result<Collection<StickerPackId, StickerPack>, Error>
pub async fn sticker_packs( &self, ) -> Result<Collection<StickerPackId, StickerPack>, Error>
Discord’s standard sticker packs (List Sticker Packs). Not cached.
discord.js counterpart: Client#fetchStickerPacks.
§Errors
Returns the REST error if the request failed.
Sourcepub async fn sticker_pack(
&self,
pack_id: StickerPackId,
) -> Result<StickerPack, Error>
pub async fn sticker_pack( &self, pack_id: StickerPackId, ) -> Result<StickerPack, Error>
One standard sticker pack (Get Sticker Pack). Not cached.
discord.js counterpart: Client#fetchStickerPacks({ packId }).
§Errors
Returns the REST error if the request failed.
Sourcepub const fn invites(&self) -> InviteManager<'_, B>
pub const fn invites(&self) -> InviteManager<'_, B>
Returns the invite manager (D-239).
discord.js counterpart: Guild#invites and Client#fetchInvite.
Sourcepub const fn webhooks(&self) -> WebhookManager<'_, B>
pub const fn webhooks(&self) -> WebhookManager<'_, B>
Returns the webhook manager (D-237).
discord.js counterpart: the webhook methods of TextChannel, Guild and Client.
Trait Implementations§
Auto Trait Implementations§
impl<B = DashMapBackend> !RefUnwindSafe for Context<B>
impl<B = DashMapBackend> !UnwindSafe for Context<B>
impl<B> Freeze for Context<B>
impl<B> Send for Context<B>
impl<B> Sync for Context<B>
impl<B> Unpin for Context<B>
impl<B> UnsafeUnpin for Context<B>
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
§impl<T> Classify for T
impl<T> Classify for T
type Classified = T
fn classify(self) -> T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Declassify for T
impl<T> Declassify for T
type Declassified = T
fn declassify(self) -> T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more