@@ -360,23 +360,30 @@ impl ChannelId {
360360 http. follow_news_channel ( self , & map) . await
361361 }
362362
363- /// First attempts to retrieve the channel from the `temp_cache` if enabled, otherwise performs
364- /// a HTTP request.
365- ///
366- /// It is recommended to first check if the channel is accessible via `Cache::guild` and
367- /// `Guild::members`, although this requires a `GuildId`.
363+ /// Attempts to retrieve the channel from the guild cache, otherwise from HTTP/temp cache.
368364 ///
369365 /// # Errors
370366 ///
371367 /// Returns [`Error::Http`] if the channel retrieval request failed.
372- pub async fn to_channel ( self , cache_http : impl CacheHttp ) -> Result < Channel > {
373- #[ cfg( feature = "temp_cache" ) ]
374- {
375- if let Some ( cache) = cache_http. cache ( ) {
376- if let Some ( channel) = cache. temp_channels . get ( & self ) {
377- return Ok ( Channel :: Guild ( GuildChannel :: clone ( & * channel) ) ) ;
368+ pub async fn to_channel (
369+ self ,
370+ cache_http : impl CacheHttp ,
371+ guild_id : Option < GuildId > ,
372+ ) -> Result < Channel > {
373+ #[ cfg( feature = "cache" ) ]
374+ if let Some ( cache) = cache_http. cache ( ) {
375+ if let Some ( guild_id) = guild_id {
376+ if let Some ( guild) = cache. guild ( guild_id) {
377+ if let Some ( channel) = guild. channels . get ( & self ) {
378+ return Ok ( Channel :: Guild ( channel. clone ( ) ) ) ;
379+ }
378380 }
379381 }
382+
383+ #[ cfg( feature = "temp_cache" ) ]
384+ if let Some ( channel) = cache. temp_channels . get ( & self ) {
385+ return Ok ( Channel :: Guild ( GuildChannel :: clone ( & * channel) ) ) ;
386+ }
380387 }
381388
382389 let channel = cache_http. http ( ) . get_channel ( self ) . await ?;
@@ -396,6 +403,29 @@ impl ChannelId {
396403 Ok ( channel)
397404 }
398405
406+ /// Fetches a channel from the cache, falling back to HTTP/temp cache.
407+ ///
408+ /// It is highly recommended to pass the `guild_id` parameter as otherwise this may perform many
409+ /// HTTP requests.
410+ ///
411+ /// # Errors
412+ ///
413+ /// Errors if the HTTP fallback fails, or if the channel does not come from the guild passed.
414+ pub async fn to_guild_channel (
415+ self ,
416+ cache_http : impl CacheHttp ,
417+ guild_id : Option < GuildId > ,
418+ ) -> Result < GuildChannel > {
419+ let channel = self . to_channel ( cache_http, guild_id) . await ?;
420+ let guild_channel = channel. guild ( ) . ok_or ( ModelError :: InvalidChannelType ) ?;
421+
422+ if guild_id. is_some_and ( |id| guild_channel. guild_id != id) {
423+ return Err ( Error :: Model ( ModelError :: ChannelNotFound ) ) ;
424+ }
425+
426+ Ok ( guild_channel)
427+ }
428+
399429 /// Gets all of the channel's invites.
400430 ///
401431 /// Requires the [Manage Channels] permission.
0 commit comments