er/src/commands/threads.rs
Louis Hollingworth 94d5f5afe7
(chore, #8) Refactored code into separate files.
Signed-off-by: Louis Hollingworth <louis@hollingworth.nl>
2023-10-28 16:55:58 +01:00

54 lines
1.6 KiB
Rust

// SPDX-FileCopyrightText: 2023 Louis Hollingworth <louis@hollingworth.nl>
//
// SPDX-License-Identifier: GPL-3.0-or-later
use crate::models::discord::{DiscordThreadsResp, DiscordThreadsRespThread, EDiscordThreadsResp};
use poise::serenity_prelude as serenity;
/// 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::<EDiscordThreadsResp>()
.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<DiscordThreadsRespThread>) -> String {
let mut resp_string = "".to_string();
for thread in threads {
resp_string += &(format!("\n- <#{}>", thread.id));
}
return resp_string;
}