62 lines
1.7 KiB
Rust
Raw Normal View History

2024-03-24 20:23:16 +00:00
use rocket::{get, http::CookieJar, serde::json::json, State};
2023-10-08 18:24:04 +01:00
use serde::Serialize;
use serenity::{
client::Context,
model::{
channel::GuildChannel,
id::{ChannelId, GuildId},
},
};
2024-03-24 20:23:16 +00:00
use crate::web::{check_authorization, routes::JsonResult};
2023-10-08 18:24:04 +01:00
#[derive(Serialize)]
struct ChannelInfo {
id: String,
name: String,
webhook_avatar: Option<String>,
webhook_name: Option<String>,
}
#[get("/api/guild/<id>/channels")]
pub async fn get_guild_channels(
id: u64,
cookies: &CookieJar<'_>,
ctx: &State<Context>,
) -> JsonResult {
offline!(Ok(json!(vec![ChannelInfo {
name: "general".to_string(),
id: "1".to_string(),
webhook_avatar: None,
webhook_name: None,
}])));
check_authorization(cookies, ctx.inner(), id).await?;
2024-01-06 19:48:17 +00:00
match GuildId::new(id).to_guild_cached(ctx.inner()) {
2023-10-08 18:24:04 +01:00
Some(guild) => {
let mut channels = guild
.channels
.iter()
.filter(|(_, channel)| channel.is_text_based())
2024-01-06 19:48:17 +00:00
.map(|(id, channel)| (id.to_owned(), channel.to_owned()))
2023-10-08 18:24:04 +01:00
.collect::<Vec<(ChannelId, GuildChannel)>>();
channels.sort_by(|(_, c1), (_, c2)| c1.position.cmp(&c2.position));
let channel_info = channels
.iter()
.map(|(channel_id, channel)| ChannelInfo {
name: channel.name.to_string(),
id: channel_id.to_string(),
webhook_avatar: None,
webhook_name: None,
})
.collect::<Vec<ChannelInfo>>();
Ok(json!(channel_info))
}
None => json_err!("Bot not in guild"),
}
}