pub use {direct_message::*, group::*, text::*, voice::*};
mod direct_message;
mod group;
mod text;
mod voice;
use serde::Deserialize;
use crate::{builders::EditChannel, models::Id, Context, Result};
#[cfg(feature = "cache")]
use crate::cache::UpdateCache;
#[derive(Debug, Deserialize, Clone, PartialEq)]
#[serde(tag = "channel_type")]
pub enum Channel {
#[serde(rename = "TextChannel")]
Text(TextChannel),
#[serde(rename = "VoiceChannel")]
Voice(VoiceChannel),
Group(GroupChannel),
DirectMessage(DirectMessageChannel),
}
impl Channel {
pub async fn fetch(cx: &Context, id: &Id) -> Result<Self> {
#[cfg(feature = "cache")]
if let Some(channel) = cx.cache.channel(id).await {
return Ok(channel);
}
cx.http_client.get(format!("channels/{}", id)).await
}
pub fn id(&self) -> &Id {
match self {
Self::Text(TextChannel { id, .. })
| Self::Voice(VoiceChannel { id, .. })
| Self::Group(GroupChannel { id, .. })
| Self::DirectMessage(DirectMessageChannel { id, .. }) => id,
}
}
async fn edit(cx: &Context, channel_id: &Id, builder: EditChannel) -> Result {
cx.http_client
.patch(format!("channels/{}", channel_id), builder)
.await
}
async fn delete(cx: &Context, channel_id: &Id) -> Result {
cx.http_client
.delete(format!("channels/{}", channel_id))
.await
}
}
#[cfg(feature = "cache")]
#[async_trait::async_trait]
impl UpdateCache for Channel {
async fn update(&self, cx: &Context) {
cx.cache
.channels
.write()
.await
.insert(self.id().clone(), self.clone());
}
}