Skip to content

Commit

Permalink
Add Context::{cache, http} shorthands
Browse files Browse the repository at this point in the history
And remove the internal `.sc()` function in favor of the `.cache()` shorthand or full blown `.serenity_context()`

[amend commit] fix feature gate
  • Loading branch information
kangalio committed May 10, 2023
1 parent 3eef3f4 commit a72b3f3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/builtins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,12 @@ pub async fn servers<U, E>(ctx: crate::Context<'_, U, E>) -> Result<(), serenity
is_public: bool,
}

let guild_ids = ctx.sc().cache.guilds();
let guild_ids = ctx.cache().guilds();
let mut num_unavailable_guilds = 0;
let mut guilds = guild_ids
.iter()
.map(|&guild_id| {
ctx.sc().cache.guild_field(guild_id, |guild| Guild {
ctx.cache().guild_field(guild_id, |guild| Guild {
name: guild.name.clone(),
num_members: guild.member_count,
is_public: guild.features.iter().any(|x| x == "DISCOVERABLE"),
Expand Down
12 changes: 9 additions & 3 deletions src/dispatch/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@ async fn missing_permissions<U, E>(
return Some(serenity::Permissions::empty());
}

let permissions = user_permissions(ctx.sc(), ctx.guild_id(), ctx.channel_id(), user).await;
let permissions = user_permissions(
ctx.serenity_context(),
ctx.guild_id(),
ctx.channel_id(),
user,
)
.await;
Some(required_permissions - permissions?)
}

Expand All @@ -73,7 +79,7 @@ async fn check_permissions_and_cooldown_single<'a, U, E>(
Some(guild_id) => {
#[cfg(feature = "cache")]
if ctx.framework().options().require_cache_for_guild_check
&& ctx.sc().cache.guild_field(guild_id, |_| ()).is_none()
&& ctx.cache().guild_field(guild_id, |_| ()).is_none()
{
return Err(crate::FrameworkError::GuildOnly { ctx });
}
Expand All @@ -88,7 +94,7 @@ async fn check_permissions_and_cooldown_single<'a, U, E>(
}

if cmd.nsfw_only {
let channel = match ctx.channel_id().to_channel(ctx.sc()).await {
let channel = match ctx.channel_id().to_channel(ctx.serenity_context()).await {
Ok(channel) => channel,
Err(e) => {
log::warn!("Error when getting channel: {}", e);
Expand Down
4 changes: 2 additions & 2 deletions src/reply/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl ReplyHandle<'_> {
match &self.0 {
ReplyHandleInner::Prefix(msg) => {
msg.clone()
.edit(ctx.sc(), |b| {
.edit(ctx.serenity_context(), |b| {
// Clear builder so that adding embeds or attachments won't add on top of
// the pre-edit items but replace them (which is apparently the more
// intuitive behavior). Notably, setting the builder to default doesn't
Expand Down Expand Up @@ -150,7 +150,7 @@ impl ReplyHandle<'_> {
/// Deletes this message
pub async fn delete<U, E>(&self, ctx: crate::Context<'_, U, E>) -> Result<(), serenity::Error> {
match &self.0 {
ReplyHandleInner::Prefix(msg) => msg.delete(ctx.sc()).await?,
ReplyHandleInner::Prefix(msg) => msg.delete(ctx.serenity_context()).await?,
ReplyHandleInner::Application {
http: _,
interaction,
Expand Down
30 changes: 18 additions & 12 deletions src/structs/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,6 @@ context_methods! {
self.serenity_context()
}

// Poise internals can't pass `poise::Context` into `impl CacheHttp` parameters, because
// `CacheHttp: Send + Sync` so poise internals would need to add noisy `U: Send + Sync`
// everywhere.
// Poise internals also can't continue to call `.discord()` because it's deprecated. And
// `.serenity_context()` is too long. So we have this instead.
#[doc(hidden)]
(sc self)
(pub fn sc(self) -> &'a serenity::Context) {
self.serenity_context()
}

/// Returns a view into data stored by the framework, like configuration
(framework self)
(pub fn framework(self) -> crate::FrameworkContext<'a, U, E>) {
Expand Down Expand Up @@ -251,7 +240,7 @@ context_methods! {
ctx.interaction.member().map(Cow::Borrowed)
} else {
self.guild_id()?
.member(self.sc(), self.author().id)
.member(self.serenity_context(), self.author().id)
.await
.ok()
.map(Cow::Owned)
Expand Down Expand Up @@ -462,6 +451,23 @@ context_methods! {
}
reply
}

/// Returns serenity's cache which stores various useful data received from the gateway
///
/// Shorthand for [`.serenity_context().cache`](serenity::Context::cache)
#[cfg(feature = "cache")]
(cache self)
(pub fn cache(self) -> &'a serenity::Cache) {
&self.serenity_context().cache
}

/// Returns serenity's raw Discord API client to make raw API requests, if needed.
///
/// Shorthand for [`.serenity_context().http`](serenity::Context::http)
(http self)
(pub fn http(self) -> &'a serenity::Http) {
&self.serenity_context().http
}
}

impl<'a, U, E> Context<'a, U, E> {
Expand Down

0 comments on commit a72b3f3

Please sign in to comment.