// SPDX-FileCopyrightText: 2023 Louis Hollingworth // // SPDX-License-Identifier: GPL-3.0-or-later use crate::models::discord::{DiscordThreadsResp, DiscordThreadsRespThread, EDiscordThreadsResp}; /// Displays current active threads #[poise::command(slash_command)] pub async fn threads(ctx: crate::Context<'_>) -> Result<(), crate::Error> { let mut threadsr: DiscordThreadsResp = DiscordThreadsResp { threads: vec![] }; if ctx.guild_id().is_some() { let gid = ctx.guild_id().unwrap(); let resp = reqwest::Client::new() .get(format!( "https://discord.com/api/v10/guilds/{}/threads/active", gid.to_string() )) .header( "AUTHORIZATION", format!( "Bot {}", std::env::var("DISCORD_TOKEN").expect("missing DISCORD_TOKEN") ), ) .send() .await? .json::() .await?; threadsr = match resp { EDiscordThreadsResp::Err(e) => return Err(crate::Error::from(e)), EDiscordThreadsResp::Ok(tr) => tr, }; } let msg = format!( "The current active threads are:{}", build_thread_response_str(threadsr.threads) ); ctx.reply(msg).await?; Ok(()) } fn build_thread_response_str(threads: Vec) -> String { let mut resp_string = "".to_string(); for thread in threads { resp_string += &(format!("\n- <#{}>", thread.id)); } return resp_string; }