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,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]
|
||||
|
|
5
src/commands/mod.rs
Normal file
5
src/commands/mod.rs
Normal file
|
@ -0,0 +1,5 @@
|
|||
// SPDX-FileCopyrightText: 2023 Louis Hollingworth <louis@hollingworth.nl>
|
||||
//
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
pub mod threads;
|
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;
|
||||
}
|
11
src/lib.rs
Normal file
11
src/lib.rs
Normal file
|
@ -0,0 +1,11 @@
|
|||
// SPDX-FileCopyrightText: 2023 Louis Hollingworth <louis@hollingworth.nl>
|
||||
//
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
pub mod commands;
|
||||
pub mod models;
|
||||
|
||||
pub type Error = Box<dyn std::error::Error + Send + Sync>;
|
||||
pub type Context<'a> = poise::Context<'a, Data, Error>;
|
||||
|
||||
pub struct Data {}
|
86
src/main.rs
86
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<dyn std::error::Error + Send + Sync>;
|
||||
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<DiscordThreadsRespThread>,
|
||||
}
|
||||
|
||||
#[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::<EDiscordThreadsResp>()
|
||||
.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<DiscordThreadsRespThread>) -> 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"))
|
||||
|
|
35
src/models/discord/mod.rs
Normal file
35
src/models/discord/mod.rs
Normal file
|
@ -0,0 +1,35 @@
|
|||
// SPDX-FileCopyrightText: 2023 Louis Hollingworth <louis@hollingworth.nl>
|
||||
//
|
||||
// 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<DiscordThreadsRespThread>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Debug, Deserialize)]
|
||||
pub struct DiscordThreadsRespThread {
|
||||
pub id: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum EDiscordThreadsResp {
|
||||
Ok(DiscordThreadsResp),
|
||||
Err(DiscordHttpError),
|
||||
}
|
5
src/models/mod.rs
Normal file
5
src/models/mod.rs
Normal file
|
@ -0,0 +1,5 @@
|
|||
// SPDX-FileCopyrightText: 2023 Louis Hollingworth <louis@hollingworth.nl>
|
||||
//
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
pub mod discord;
|
Loading…
Reference in a new issue