From 94d5f5afe77def832815ef7d34c7e1fbfeecb8d4 Mon Sep 17 00:00:00 2001 From: Louis Hollingworth Date: Sat, 28 Oct 2023 16:55:58 +0100 Subject: [PATCH] (chore, #8) Refactored code into separate files. Signed-off-by: Louis Hollingworth --- Cargo.toml | 5 +++ src/commands/mod.rs | 5 +++ src/commands/threads.rs | 54 ++++++++++++++++++++++++ src/lib.rs | 11 +++++ src/main.rs | 86 +-------------------------------------- src/models/discord/mod.rs | 35 ++++++++++++++++ src/models/mod.rs | 5 +++ 7 files changed, 117 insertions(+), 84 deletions(-) create mode 100644 src/commands/mod.rs create mode 100644 src/commands/threads.rs create mode 100644 src/lib.rs create mode 100644 src/models/discord/mod.rs create mode 100644 src/models/mod.rs diff --git a/Cargo.toml b/Cargo.toml index 1e7b682..ea0b5ab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,11 @@ name = "er" version = "2.0.0" edition = "2021" +[lib] + +[[bin]] +name = "er" + # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] diff --git a/src/commands/mod.rs b/src/commands/mod.rs new file mode 100644 index 0000000..f8f1b73 --- /dev/null +++ b/src/commands/mod.rs @@ -0,0 +1,5 @@ +// SPDX-FileCopyrightText: 2023 Louis Hollingworth +// +// SPDX-License-Identifier: GPL-3.0-or-later + +pub mod threads; diff --git a/src/commands/threads.rs b/src/commands/threads.rs new file mode 100644 index 0000000..221ae9d --- /dev/null +++ b/src/commands/threads.rs @@ -0,0 +1,54 @@ +// SPDX-FileCopyrightText: 2023 Louis Hollingworth +// +// 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::() + .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; +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..5f094c1 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,11 @@ +// SPDX-FileCopyrightText: 2023 Louis Hollingworth +// +// SPDX-License-Identifier: GPL-3.0-or-later + +pub mod commands; +pub mod models; + +pub type Error = Box; +pub type Context<'a> = poise::Context<'a, Data, Error>; + +pub struct Data {} diff --git a/src/main.rs b/src/main.rs index 019470a..aabec09 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,90 +3,8 @@ // SPDX-License-Identifier: GPL-3.0-or-later use dotenvy::dotenv; +use er::{commands, Data}; use poise::serenity_prelude as serenity; -use serde::{Deserialize, Serialize}; -use thiserror::Error; - -struct Data {} -type Error = Box; -type Context<'a> = poise::Context<'a, Data, Error>; - -#[derive(Serialize, Deserialize, Debug, Error)] -struct DiscordHttpError { - message: String, - code: serde_json::Number, -} - -impl std::fmt::Display for DiscordHttpError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}: {}", self.code, self.message) - } -} - -#[derive(Serialize, Deserialize, Debug)] -struct DiscordThreadsResp { - threads: Vec, -} - -#[derive(Serialize, Debug, Deserialize)] -struct DiscordThreadsRespThread { - id: String, -} - -#[derive(Debug, Serialize, Deserialize)] -#[serde(untagged)] -enum EDiscordThreadsResp { - Ok(DiscordThreadsResp), - Err(DiscordHttpError), -} - -/// Displays current active threads -#[poise::command(slash_command)] -async fn threads(ctx: Context<'_>) -> Result<(), 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(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; -} #[tokio::main] async fn main() { @@ -94,7 +12,7 @@ async fn main() { let framework = poise::Framework::builder() .options(poise::FrameworkOptions { - commands: vec![threads()], + commands: vec![commands::threads::threads()], ..Default::default() }) .token(std::env::var("DISCORD_TOKEN").expect("missing DISCORD_TOKEN")) diff --git a/src/models/discord/mod.rs b/src/models/discord/mod.rs new file mode 100644 index 0000000..460ab6c --- /dev/null +++ b/src/models/discord/mod.rs @@ -0,0 +1,35 @@ +// SPDX-FileCopyrightText: 2023 Louis Hollingworth +// +// SPDX-License-Identifier: GPL-3.0-or-later + +use serde::{Deserialize, Serialize}; +use thiserror::Error; + +#[derive(Serialize, Deserialize, Debug, Error)] +pub struct DiscordHttpError { + pub message: String, + pub code: serde_json::Number, +} + +impl std::fmt::Display for DiscordHttpError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}: {}", self.code, self.message) + } +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct DiscordThreadsResp { + pub threads: Vec, +} + +#[derive(Serialize, Debug, Deserialize)] +pub struct DiscordThreadsRespThread { + pub id: String, +} + +#[derive(Debug, Serialize, Deserialize)] +#[serde(untagged)] +pub enum EDiscordThreadsResp { + Ok(DiscordThreadsResp), + Err(DiscordHttpError), +} diff --git a/src/models/mod.rs b/src/models/mod.rs new file mode 100644 index 0000000..318349a --- /dev/null +++ b/src/models/mod.rs @@ -0,0 +1,5 @@ +// SPDX-FileCopyrightText: 2023 Louis Hollingworth +// +// SPDX-License-Identifier: GPL-3.0-or-later + +pub mod discord;