@@ -22,6 +22,8 @@ use crate::builder::{
2222} ;
2323#[ cfg( all( feature = "cache" , feature = "model" ) ) ]
2424use crate :: cache:: Cache ;
25+ #[ cfg( all( feature = "cache" , feature = "temp_cache" , feature = "model" ) ) ]
26+ use crate :: cache:: MaybeOwnedArc ;
2527#[ cfg( feature = "model" ) ]
2628use crate :: http:: { CacheHttp , Http , Typing } ;
2729use crate :: model:: prelude:: * ;
@@ -39,6 +41,55 @@ impl ChannelId {
3941
4042#[ cfg( feature = "model" ) ]
4143impl ChannelId {
44+ /// Fetches a channel from the cache, falling back to HTTP/temp cache.
45+ ///
46+ /// It is highly recommended to pass the `guild_id` parameter as otherwise this may perform many
47+ /// HTTP requests.
48+ ///
49+ /// # Errors
50+ ///
51+ /// Errors if the HTTP fallback fails, or if the channel does not come from the guild passed.
52+ pub async fn to_guild_channel (
53+ self ,
54+ cache_http : impl CacheHttp ,
55+ guild_id : Option < GuildId > ,
56+ ) -> Result < GuildChannel > {
57+ #[ cfg( feature = "cache" ) ]
58+ if let Some ( cache) = cache_http. cache ( ) {
59+ if let Some ( guild_id) = guild_id {
60+ if let Some ( guild) = cache. guild ( guild_id) {
61+ if let Some ( channel) = guild. channels . get ( & self ) {
62+ return Ok ( channel. clone ( ) ) ;
63+ }
64+ }
65+ }
66+
67+ #[ cfg( feature = "temp_cache" ) ]
68+ if let Some ( temp_channel) = cache. temp_channels . get ( & self ) {
69+ if guild_id. is_some_and ( |id| temp_channel. base . guild_id != id) {
70+ return Err ( Error :: Model ( ModelError :: ChannelNotFound ) ) ;
71+ }
72+
73+ return Ok ( GuildChannel :: clone ( & temp_channel) ) ;
74+ }
75+ }
76+
77+ let channel = cache_http. http ( ) . get_channel ( self . widen ( ) ) . await ?;
78+ let guild_channel = channel. guild ( ) . ok_or ( ModelError :: InvalidChannelType ) ?;
79+
80+ #[ cfg( all( feature = "cache" , feature = "temp_cache" ) ) ]
81+ if let Some ( cache) = cache_http. cache ( ) {
82+ let cached_channel = MaybeOwnedArc :: new ( guild_channel. clone ( ) ) ;
83+ cache. temp_channels . insert ( self , cached_channel) ;
84+ }
85+
86+ if guild_id. is_some_and ( |id| guild_channel. base . guild_id != id) {
87+ return Err ( Error :: Model ( ModelError :: ChannelNotFound ) ) ;
88+ }
89+
90+ Ok ( guild_channel)
91+ }
92+
4293 /// Creates an invite for the given channel.
4394 ///
4495 /// **Note**: Requires the [Create Instant Invite] permission.
@@ -595,8 +646,6 @@ impl GenericChannelId {
595646
596647 #[ cfg( all( feature = "cache" , feature = "temp_cache" ) ) ]
597648 if let Some ( cache) = cache_http. cache ( ) {
598- use crate :: cache:: MaybeOwnedArc ;
599-
600649 match & channel {
601650 Channel :: Guild ( guild_channel) => {
602651 let cached_channel = MaybeOwnedArc :: new ( guild_channel. clone ( ) ) ;
@@ -614,29 +663,6 @@ impl GenericChannelId {
614663 Ok ( channel)
615664 }
616665
617- /// Fetches a channel from the cache, falling back to HTTP/temp cache.
618- ///
619- /// It is highly recommended to pass the `guild_id` parameter as otherwise this may perform many
620- /// HTTP requests.
621- ///
622- /// # Errors
623- ///
624- /// Errors if the HTTP fallback fails, or if the channel does not come from the guild passed.
625- pub async fn to_guild_channel (
626- self ,
627- cache_http : impl CacheHttp ,
628- guild_id : Option < GuildId > ,
629- ) -> Result < GuildChannel > {
630- let channel = self . to_channel ( cache_http, guild_id) . await ?;
631- let guild_channel = channel. guild ( ) . ok_or ( ModelError :: InvalidChannelType ) ?;
632-
633- if guild_id. is_some_and ( |id| guild_channel. base . guild_id != id) {
634- return Err ( Error :: Model ( ModelError :: ChannelNotFound ) ) ;
635- }
636-
637- Ok ( guild_channel)
638- }
639-
640666 /// Gets a message from the channel.
641667 ///
642668 /// If the cache feature is enabled the cache will be checked first. If not found it will
0 commit comments