diff options
author | Cara Salter <cara@devcara.com> | 2022-01-16 00:02:56 -0500 |
---|---|---|
committer | Cara Salter <cara@devcara.com> | 2022-01-16 00:02:56 -0500 |
commit | eb41fede91d072693b279f908976d2ebc2ba1ca3 (patch) | |
tree | 81154fe49330b429e66fff098653fc636747717c | |
parent | 16921c23ff7186381a16ca0b03c521d56ee4eb21 (diff) | |
download | glitch-ng-eb41fede91d072693b279f908976d2ebc2ba1ca3.tar.gz glitch-ng-eb41fede91d072693b279f908976d2ebc2ba1ca3.zip |
format
-rw-r--r-- | src/commands/actions.rs | 30 | ||||
-rw-r--r-- | src/commands/meta.rs | 28 | ||||
-rw-r--r-- | src/commands/mod.rs | 4 | ||||
-rw-r--r-- | src/commands/osu.rs | 100 | ||||
-rw-r--r-- | src/commands/pony.rs | 48 | ||||
-rw-r--r-- | src/handler.rs | 56 | ||||
-rw-r--r-- | src/main.rs | 42 |
7 files changed, 206 insertions, 102 deletions
diff --git a/src/commands/actions.rs b/src/commands/actions.rs index 96862bb..63d00fb 100644 --- a/src/commands/actions.rs +++ b/src/commands/actions.rs @@ -78,11 +78,18 @@ static BOOP_VEC: [&'static str; 15] = [ /// Usage: /// ~boop <@User> #[poise::command(context_menu_command = "Boop!", slash_command, prefix_command)] -pub async fn boop(ctx: Context<'_>, - #[description = "The user to be booped"] user: serenity::User, - ) -> Result<(), Error> { +pub async fn boop( + ctx: Context<'_>, + #[description = "The user to be booped"] user: serenity::User, +) -> Result<(), Error> { let url = get_random_url_from_vec(BOOP_VEC.to_vec()); - ctx.say(format!("<@{}> boops <@{}>! Awwwww!\n{}", ctx.author().id.0, user.id.0, url)).await?; + ctx.say(format!( + "<@{}> boops <@{}>! Awwwww!\n{}", + ctx.author().id.0, + user.id.0, + url + )) + .await?; Ok(()) } @@ -92,11 +99,18 @@ pub async fn boop(ctx: Context<'_>, /// Usage: /// ~hug <@User> #[poise::command(context_menu_command = "Hug!", slash_command, prefix_command)] -pub async fn hug(ctx: Context<'_>, - #[description = "The user to be hugged"] user: serenity::User, - ) -> Result<(), Error> { +pub async fn hug( + ctx: Context<'_>, + #[description = "The user to be hugged"] user: serenity::User, +) -> Result<(), Error> { let url = get_random_url_from_vec(HUG_VEC.to_vec()); - ctx.say(format!("<@{}> hugs <@{}>! So kind of them.\n{}", ctx.author().id.0, user.id.0, url)).await?; + ctx.say(format!( + "<@{}> hugs <@{}>! So kind of them.\n{}", + ctx.author().id.0, + user.id.0, + url + )) + .await?; Ok(()) } diff --git a/src/commands/meta.rs b/src/commands/meta.rs index d2c5564..917fbfe 100644 --- a/src/commands/meta.rs +++ b/src/commands/meta.rs @@ -13,15 +13,18 @@ pub async fn ping(ctx: Context<'_>) -> Result<(), Error> { #[poise::command(prefix_command, slash_command)] pub async fn about(ctx: Context<'_>) -> Result<(), Error> { let current_version = env!("CARGO_PKG_VERSION"); - - if let Err(e) = ctx.send(|m| { - m.embed(|e| { - e.title("Glitch"); - e - }); - m - }).await { + if let Err(e) = ctx + .send(|m| { + m.embed(|e| { + e.title("Glitch"); + + e + }); + m + }) + .await + { return Err(e.into()); } @@ -29,10 +32,9 @@ pub async fn about(ctx: Context<'_>) -> Result<(), Error> { } #[poise::command(prefix_command, slash_command, context_menu_command = "User Info")] -pub async fn userinfo(ctx: Context<'_>, - #[description = "The user to get info on"] - user: serenity::User, - ) -> Result<(), Error> { - +pub async fn userinfo( + ctx: Context<'_>, + #[description = "The user to get info on"] user: serenity::User, +) -> Result<(), Error> { Ok(()) } diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 2121532..3ffb136 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,5 +1,5 @@ -pub mod meta; -pub mod pony; pub mod actions; +pub mod meta; pub mod osu; +pub mod pony; pub mod reactionroles; diff --git a/src/commands/osu.rs b/src/commands/osu.rs index 28d3cb3..8d91f56 100644 --- a/src/commands/osu.rs +++ b/src/commands/osu.rs @@ -29,17 +29,24 @@ async fn setup_reqwest() -> Result<reqwest::Client, Error> { scope: "public".into(), }; - let req = reqwest::Client::new().post("https://osu.ppy.sh/oauth/token") - .json(&token_req).send().await? - .json::<OsuTokenResponse>().await?; - + let req = reqwest::Client::new() + .post("https://osu.ppy.sh/oauth/token") + .json(&token_req) + .send() + .await? + .json::<OsuTokenResponse>() + .await?; let mut headers = header::HeaderMap::new(); - headers.insert("Authorization", header::HeaderValue::from_str(format!("Bearer {}", req.access_token).as_str()).unwrap()); - + headers.insert( + "Authorization", + header::HeaderValue::from_str(format!("Bearer {}", req.access_token).as_str()).unwrap(), + ); + Ok(ClientBuilder::new() .default_headers(headers) - .build().unwrap()) + .build() + .unwrap()) } #[derive(Deserialize, Serialize, Clone, Debug)] @@ -75,14 +82,21 @@ struct OsuUserStatsGrades { /// Examples: /// ~osup muirrum #[poise::command(slash_command, prefix_command)] -pub async fn osup(ctx: Context<'_>, - #[description = "The osu! username or ID to look up"] - lookup: String, - ) -> Result<(), Error> { +pub async fn osup( + ctx: Context<'_>, + #[description = "The osu! username or ID to look up"] lookup: String, +) -> Result<(), Error> { let client = setup_reqwest().await?; - let mut res = client.get(format!("https://osu.ppy.sh/api/v2/users/{}?key=username", lookup)) - .send().await?.json::<OsuUser>().await?; + let mut res = client + .get(format!( + "https://osu.ppy.sh/api/v2/users/{}?key=username", + lookup + )) + .send() + .await? + .json::<OsuUser>() + .await?; res.country_code = res.country_code.to_lowercase(); @@ -90,13 +104,31 @@ pub async fn osup(ctx: Context<'_>, m.embed(|e| { e.title(format!("osu! Profile: {}", res.clone().username)); e.thumbnail(res.clone().avatar_url); - e.field("Ranks", format!(":map: #{}\n:flag_{}: #{}", res.clone().statistics.global_rank.unwrap_or(0), res.clone().country_code, res.clone().statistics.country_rank.unwrap_or(0u32)), true); - - e.field("Stats", format!("**PP:** {}\n**Acc:** {}%", res.clone().statistics.pp, res.clone().statistics.hit_accuracy.unwrap_or(0.0)), false); + e.field( + "Ranks", + format!( + ":map: #{}\n:flag_{}: #{}", + res.clone().statistics.global_rank.unwrap_or(0), + res.clone().country_code, + res.clone().statistics.country_rank.unwrap_or(0u32) + ), + true, + ); + + e.field( + "Stats", + format!( + "**PP:** {}\n**Acc:** {}%", + res.clone().statistics.pp, + res.clone().statistics.hit_accuracy.unwrap_or(0.0) + ), + false, + ); e }); m - }).await?; + }) + .await?; Ok(()) } @@ -140,28 +172,42 @@ struct OsuBeatMapSetCovers { /// Usage: /// ~osubm <id> #[poise::command(slash_command, prefix_command)] -pub async fn osubm(ctx: Context<'_>, - #[description = "The beatmap ID"] - bm_id: u32, - ) -> Result<(), Error> { +pub async fn osubm( + ctx: Context<'_>, + #[description = "The beatmap ID"] bm_id: u32, +) -> Result<(), Error> { let client = setup_reqwest().await?; - let mut res = client.get(format!("https://osu.ppy.sh/api/v2/beatmaps/{}", bm_id)) - .send().await?.json::<OsuBeatMap>().await?; + let mut res = client + .get(format!("https://osu.ppy.sh/api/v2/beatmaps/{}", bm_id)) + .send() + .await? + .json::<OsuBeatMap>() + .await?; ctx.send(|m| { m.embed(|e| { - e.title(format!("osu! Beatmap: {} by {}", res.beatmapset.title, res.beatmapset.creator)); + e.title(format!( + "osu! Beatmap: {} by {}", + res.beatmapset.title, res.beatmapset.creator + )); e.image(res.beatmapset.covers.list2); - e.description(format!("**Link:** {}\n**Length:** {} **BPM:** {}\n**Difficulty:** {}:star:", res.url, res.total_length, res.bpm, res.difficulty_rating)); + e.description(format!( + "**Link:** {}\n**Length:** {} **BPM:** {}\n**Difficulty:** {}:star:", + res.url, res.total_length, res.bpm, res.difficulty_rating + )); e.footer(|f| { - f.text(format!("BM ID {} | BM Set ID {}\nCreated {}", res.id, res.beatmapset.id, res.beatmapset.submitted_date)); + f.text(format!( + "BM ID {} | BM Set ID {}\nCreated {}", + res.id, res.beatmapset.id, res.beatmapset.submitted_date + )); f }); e }); m - }).await?; + }) + .await?; Ok(()) } diff --git a/src/commands/pony.rs b/src/commands/pony.rs index 75b4238..b908ca4 100644 --- a/src/commands/pony.rs +++ b/src/commands/pony.rs @@ -39,8 +39,10 @@ pub struct PonyRepresentation { /// Retrieves a random SFW pony image #[poise::command(slash_command, prefix_command)] pub async fn randpony(ctx: Context<'_>) -> Result<(), Error> { - let mut response_msg = ctx.say("Fetching a random pony image, please wait!").await?; - + let mut response_msg = ctx + .say("Fetching a random pony image, please wait!") + .await?; + let client = reqwest::Client::new(); let res = reqwest::get("https://theponyapi.com/api/v1/pony/random") .await? @@ -65,11 +67,12 @@ pub async fn randpony(ctx: Context<'_>) -> Result<(), Error> { e }); m - }).await?; - }, + }) + .await?; + } Err(e) => { ctx.say("Error editing message").await?; - }, + } }; Ok(()) @@ -82,18 +85,25 @@ pub async fn randpony(ctx: Context<'_>) -> Result<(), Error> { /// Example: /// ~tpony twilight sparkle,fluttershy #[poise::command(slash_command, prefix_command)] -pub async fn tpony(ctx: Context<'_>, - #[description = "List of tags"] - #[rest] - tags: String, - ) -> Result<(), Error> { - let mut response_msg = ctx.say(format!("Fetching pony image based on tags: {:?}", tags.clone())).await?; - - let res = reqwest::get(format!("https://theponyapi.com/api/v1/pony/random?q={}", tags).as_str()) - .await? - .json::<PonyResponse>() +pub async fn tpony( + ctx: Context<'_>, + #[description = "List of tags"] + #[rest] + tags: String, +) -> Result<(), Error> { + let mut response_msg = ctx + .say(format!( + "Fetching pony image based on tags: {:?}", + tags.clone() + )) .await?; + let res = + reqwest::get(format!("https://theponyapi.com/api/v1/pony/random?q={}", tags).as_str()) + .await? + .json::<PonyResponse>() + .await?; + match response_msg.unwrap().message().await { Ok(mut msg) => { msg.edit(&ctx.discord(), |m| { @@ -112,14 +122,14 @@ pub async fn tpony(ctx: Context<'_>, e }); m - }).await?; - }, + }) + .await?; + } Err(e) => { ctx.say("Error editing message").await?; - }, + } }; - Ok(()) } diff --git a/src/handler.rs b/src/handler.rs index ef73f36..00cf0eb 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -1,12 +1,13 @@ use poise::serenity_prelude as serenity; -use crate::{Data, Error}; use crate::models::ReactionRole; +use crate::{Data, Error}; -pub async fn event_handler(ctx: &serenity::Context, - event: &poise::Event<'_>, - data: &Data, - ) -> Result<(), Error> { +pub async fn event_handler( + ctx: &serenity::Context, + event: &poise::Event<'_>, + data: &Data, +) -> Result<(), Error> { { let pool = data.pg.lock().unwrap().clone(); match event { @@ -15,26 +16,55 @@ pub async fn event_handler(ctx: &serenity::Context, if add_reaction.user_id.unwrap() == current_user.id { return Ok(()); } - let rrole = sqlx::query_as!(ReactionRole, "SELECT * FROM reaction_roles WHERE message_id=$1 AND reaction=$2", add_reaction.message_id.0.to_string(), add_reaction.emoji.to_string()).fetch_one(&pool).await?; - let member = ctx.http.get_member(rrole.guild_id.parse::<u64>()?, add_reaction.user_id.unwrap().0).await?; + let rrole = sqlx::query_as!( + ReactionRole, + "SELECT * FROM reaction_roles WHERE message_id=$1 AND reaction=$2", + add_reaction.message_id.0.to_string(), + add_reaction.emoji.to_string() + ) + .fetch_one(&pool) + .await?; + let member = ctx + .http + .get_member( + rrole.guild_id.parse::<u64>()?, + add_reaction.user_id.unwrap().0, + ) + .await?; let member_roles = member.roles; let role_id = serenity::RoleId(rrole.role_id.parse::<u64>()?); if member_roles.contains(&role_id) { - ctx.http.remove_member_role(member.guild_id.0, member.user.id.0, role_id.0, Some("Reaction Role Menu")).await?; + ctx.http + .remove_member_role( + member.guild_id.0, + member.user.id.0, + role_id.0, + Some("Reaction Role Menu"), + ) + .await?; } else { - ctx.http.add_member_role(rrole.guild_id.parse::<u64>()?, add_reaction.user_id.unwrap().0, rrole.role_id.parse::<u64>()?, Some("Reaction Role")).await?; + ctx.http + .add_member_role( + rrole.guild_id.parse::<u64>()?, + add_reaction.user_id.unwrap().0, + rrole.role_id.parse::<u64>()?, + Some("Reaction Role"), + ) + .await?; } - let dm_chan = add_reaction.user_id.unwrap().create_dm_channel(&ctx.http).await?; + let dm_chan = add_reaction + .user_id + .unwrap() + .create_dm_channel(&ctx.http) + .await?; dm_chan.say(ctx, format!("Toggled the role!")).await?; add_reaction.delete(&ctx.http).await?; - - }, + } _ => (), } } - Ok(()) } diff --git a/src/main.rs b/src/main.rs index 021886f..4698fb1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ -use std::{time::Duration, sync::Mutex}; +use std::{sync::Mutex, time::Duration}; use dotenv::dotenv; -use sqlx::{PgPool, postgres::PgPoolOptions}; +use sqlx::{postgres::PgPoolOptions, PgPool}; type Error = Box<dyn std::error::Error + Send + Sync>; type Context<'a> = poise::Context<'a, Data, Error>; @@ -11,24 +11,30 @@ mod handler; mod models; pub struct Data { - pg: Mutex<PgPool>, + pg: Mutex<PgPool>, } /// Show help menu #[poise::command(prefix_command, slash_command)] -async fn help(ctx: Context<'_>, - #[description = "Command to get help for"] command: Option<String>, - ) -> Result<(), Error> { - poise::builtins::help(ctx, command.as_deref(), poise::builtins::HelpConfiguration::default()).await?; +async fn help( + ctx: Context<'_>, + #[description = "Command to get help for"] command: Option<String>, +) -> Result<(), Error> { + poise::builtins::help( + ctx, + command.as_deref(), + poise::builtins::HelpConfiguration::default(), + ) + .await?; Ok(()) } async fn on_error(error: poise::FrameworkError<'_, Data, Error>) { match error { poise::FrameworkError::Setup { error } => panic!("Failed to start bot: {:?}", error), - poise::FrameworkError::Command {error, ctx} => { + poise::FrameworkError::Command { error, ctx } => { println!("Error in command {}: {:?}", ctx.command().name, error); - }, + } error => { if let Err(e) = poise::builtins::on_error(error).await { println!("Error handling error: {}", e); @@ -57,20 +63,14 @@ async fn main() { commands::meta::ping(), commands::meta::about(), commands::meta::userinfo(), - commands::actions::boop(), commands::actions::hug(), - commands::pony::randpony(), commands::pony::tpony(), - commands::osu::osup(), commands::osu::osubm(), - poise::Command { - subcommands: vec![ - commands::reactionroles::init(), - ], + subcommands: vec![commands::reactionroles::init()], ..commands::reactionroles::rroles() }, ], @@ -92,7 +92,7 @@ async fn main() { additional_prefixes: vec![ poise::Prefix::Literal("hey glitch"), poise::Prefix::Literal("hey glitch,"), - ], + ], ..Default::default() }, listener: |ctx, event, _, data| Box::pin(handler::event_handler(ctx, event, data)), @@ -105,11 +105,14 @@ async fn main() { Box::pin(async move { let pool = PgPoolOptions::new() .max_connections(5) - .connect(&std::env::var("DATABASE_URL").unwrap_or("postgres://postgres@localhost/glitch".to_string())) + .connect( + &std::env::var("DATABASE_URL") + .unwrap_or("postgres://postgres@localhost/glitch".to_string()), + ) .await .expect("Couldn't connect to postgresql"); Ok(Data { - pg: Mutex::new(pool) + pg: Mutex::new(pool), }) }) }) @@ -118,4 +121,3 @@ async fn main() { .await .unwrap(); } - |