generated from lucxjo/template
(chore, #8) Refactored code into separate files.
Signed-off-by: Louis Hollingworth <louis@hollingworth.nl>
This commit is contained in:
parent
0960577152
commit
94d5f5afe7
7 changed files with 117 additions and 84 deletions
54
src/commands/threads.rs
Normal file
54
src/commands/threads.rs
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
// 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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue