chore: fix formatting and imports

authored by Kaitlyn~Ethylia and committed by isabelroses.com 616c5399 16c220e1

Changed files
+56 -43
src
commands
event_handler
+2 -2
src/commands/bot/bot.rs
··· 1 1 use crate::Context; 2 2 use color_eyre::eyre::Result; 3 - use poise::{serenity_prelude as serenity, CreateReply}; 3 + use poise::{serenity_prelude::CreateEmbed, CreateReply}; 4 4 use std::env; 5 5 6 6 /// Displays information about the bot ··· 9 9 let rev = env::var("BUILD_REV").unwrap_or("unknown".to_string()); 10 10 11 11 let embed = CreateReply::default().embed( 12 - serenity::CreateEmbed::default() 12 + CreateEmbed::default() 13 13 .title("Bot Info") 14 14 //.thumbnail(bot.avatar_url().expect("avatar failed")) 15 15 .color(0x00ff_ffff)
+13 -13
src/commands/fun/bottom.rs
··· 15 15 /// Translate your words for the tops and normies to understand 16 16 #[poise::command(slash_command, guild_only)] 17 17 pub async fn topify(ctx: Context<'_>, #[description = "text"] input: String) -> Result<()> { 18 - const MAX_LEN: usize = 1994; 19 - const WRAP: &'static str = "```"; 18 + const MAX_LEN: usize = 1994; 19 + const WRAP: &'static str = "```"; 20 20 let out = bottom::decode_string(&input); 21 21 22 - if let Ok(out) = out { 23 - let mut out = out.as_str(); 24 - let len = out.len(); 25 - for _ in 0..(len / MAX_LEN) { 26 - let (x, xs) = out.split_at(MAX_LEN); 27 - ctx.say(format!("{WRAP}{x}{WRAP}")).await?; 28 - out = xs; 29 - } 30 - if len % MAX_LEN != 0 { 31 - ctx.say(format!("{WRAP}{out}{WRAP}")).await?; 32 - } 22 + if let Ok(out) = out { 23 + let mut out = out.as_str(); 24 + let len = out.len(); 25 + for _ in 0..(len / MAX_LEN) { 26 + let (x, xs) = out.split_at(MAX_LEN); 27 + ctx.say(format!("{WRAP}{x}{WRAP}")).await?; 28 + out = xs; 29 + } 30 + if len % MAX_LEN != 0 { 31 + ctx.say(format!("{WRAP}{out}{WRAP}")).await?; 32 + } 33 33 } else { 34 34 ctx.say("I couldn't decode that message.").await?; 35 35 }
+3 -3
src/commands/misc/nixpkgs.rs
··· 1 1 use color_eyre::eyre::Result; 2 2 use git_tracker::Tracker; 3 - use poise::{serenity_prelude as serenity, CreateReply}; 3 + use poise::{serenity_prelude::CreateEmbed, CreateReply}; 4 4 use serde::Deserialize; 5 5 use std::env; 6 6 ··· 22 22 let github_token = env::var("GITHUB_TOKEN").expect("GITHUB_TOKEN not set"); 23 23 let tracker = Tracker::from_path(&nixpkgs_path)?; 24 24 25 - let client = &ctx.data().client; 25 + let client = &ctx.data().client; 26 26 27 27 // find out what commit our PR was merged in 28 28 let Some(commit_sha) = ({ ··· 72 72 }; 73 73 74 74 let embed = CreateReply::default().embed( 75 - serenity::CreateEmbed::new() 75 + CreateEmbed::new() 76 76 .title(format!("Nixpkgs PR #{pr} Status")) 77 77 .url(format!("{NIXPKGS_URL}/pull/{pr}")) 78 78 .description(embed_description),
+2 -2
src/commands/user/avatar.rs
··· 1 1 use crate::Context; 2 2 use color_eyre::eyre::Result; 3 - use poise::serenity_prelude as serenity; 3 + use poise::serenity_prelude::User; 4 4 5 5 /// Displays your or another user's avatar 6 6 #[poise::command(slash_command)] 7 7 pub async fn avatar( 8 8 ctx: Context<'_>, 9 - #[description = "Selected user"] user: Option<serenity::User>, 9 + #[description = "Selected user"] user: Option<User>, 10 10 ) -> Result<()> { 11 11 let user = user.as_ref().unwrap_or_else(|| ctx.author()); 12 12 let avatar = user.avatar_url().expect("Could not get avatar URL");
+6 -3
src/commands/user/whois.rs
··· 1 1 use crate::Context; 2 2 use color_eyre::eyre::Result; 3 - use poise::{serenity_prelude as serenity, CreateReply}; 3 + use poise::{ 4 + serenity_prelude::{CreateEmbed, User}, 5 + CreateReply, 6 + }; 4 7 5 8 /// Displays your or another user's info 6 9 #[poise::command(slash_command)] 7 10 pub async fn whois( 8 11 ctx: Context<'_>, 9 - #[description = "Selected user"] user: Option<serenity::User>, 12 + #[description = "Selected user"] user: Option<User>, 10 13 ) -> Result<()> { 11 14 let user = user.as_ref().unwrap_or_else(|| ctx.author()); 12 15 let membership = ctx.guild_id().unwrap().member(ctx.http(), user.id).await?; ··· 15 18 let joined_at = membership.joined_at.unwrap().unix_timestamp(); 16 19 17 20 let embed = CreateReply::default().embed( 18 - serenity::CreateEmbed::default() 21 + CreateEmbed::default() 19 22 .title(&user.name) 20 23 .thumbnail(user.avatar_url().expect("avatar failed")) 21 24 .color(0x00ff_ffff)
+16 -7
src/event_handler/code_expantion.rs
··· 1 1 // the logic here is pretty much ripped from https://github.com/uncenter/discord-forum-bot/blob/main/src/modules/expandGitHubLinks.ts 2 2 // with some modifications so I can make it work on diffrent git hosts 3 3 4 - use color_eyre::eyre::{self, Result}; 4 + use color_eyre::eyre::{eyre, Result}; 5 5 use poise::serenity_prelude::{Context, FullEvent}; 6 6 use regex::Regex; 7 7 use reqwest::Client; 8 8 9 - pub async fn handle(ctx: &Context, event: &FullEvent) -> Result<()> { 9 + pub async fn handle(ctx: &Context, event: &FullEvent, client: &Client) -> Result<()> { 10 10 if let FullEvent::Message { new_message } = event { 11 - let code_blocks = extract_code_blocks(new_message.content.clone()).await?; 11 + let code_blocks = extract_code_blocks(new_message.content.clone(), client).await?; 12 12 13 13 if !code_blocks.is_empty() { 14 14 new_message ··· 21 21 Ok(()) 22 22 } 23 23 24 - async fn extract_code_blocks(msg: String) -> Result<Vec<String>> { 24 + async fn extract_code_blocks(msg: String, client: &Client) -> Result<Vec<String>> { 25 25 let re = Regex::new( 26 - r"https?://(?P<host>(git.*|codeberg\.org))/(?P<repo>[\w-]+/[\w.-]+)/(blob|(src/(commit|branch)))?/(?P<reference>\S+?)/(?P<file>\S+)#L(?P<start>\d+)(?:[~-]L?(?P<end>\d+)?)?", 26 + r#"(?x) 27 + https?:// 28 + (?P<host> 29 + (git.*|codeberg\.org)) / 30 + (?P<repo> [\w-]+/[\w.-]+) / 31 + (blob|(src/(commit|branch)))? / 32 + (?P<reference> \S+?) / 33 + (?P<file> \S+) #L 34 + (?P<start> \d+) 35 + (?:[~-]L?(?P<end>\d+)?)? 36 + "#, 27 37 )?; 28 38 29 39 let mut blocks: Vec<String> = Vec::new(); 30 - let client = Client::new(); 31 40 32 41 for caps in re.captures_iter(&msg) { 33 42 let (host, repo, reference, file, start, end) = extract_url_components(&caps)?; ··· 79 88 ) -> Result<String> { 80 89 let response = client.get(raw_url).send().await?; 81 90 if !response.status().is_success() { 82 - return Err(eyre::eyre!("Failed to fetch content from {}", raw_url)); 91 + return Err(eyre!("Failed to fetch content from {}", raw_url)); 83 92 } 84 93 85 94 let text = response.text().await?;
+2 -2
src/event_handler/mod.rs
··· 11 11 println!("Logged in as {}", data_about_bot.user.name); 12 12 } 13 13 14 - code_expantion::handle(ctx, event).await?; 14 + let client = &_data.client; 15 15 16 - Ok(()) 16 + code_expantion::handle(ctx, event, client).await 17 17 }
+12 -11
src/main.rs
··· 6 6 use std::env; 7 7 8 8 use color_eyre::eyre::{Report, Result}; 9 - use poise::serenity_prelude::{self as serenity, ActivityData, GatewayIntents}; 9 + use poise::serenity_prelude::{ActivityData, ClientBuilder, GatewayIntents}; 10 10 11 11 #[derive(Debug)] 12 - pub struct Data { // User data, which is stored and accessible in all command invocations 13 - client: Client, 14 - } 12 + // User data, which is stored and accessible in all command invocations 13 + pub struct Data { 14 + client: Client, 15 + } 15 16 16 17 pub type Context<'a> = poise::Context<'a, Data, Report>; 17 18 ··· 48 49 commands::fun::bottom::topify(), 49 50 commands::fun::bottom::bottomify(), 50 51 ], 51 - event_handler: |ctx, event, _, data| Box::pin(crate::event_handler::event_handler(ctx, event, data)), 52 + event_handler: |ctx, event, _, data| { 53 + Box::pin(crate::event_handler::event_handler(ctx, event, data)) 54 + }, 52 55 ..Default::default() 53 56 }; 54 57 ··· 57 60 Box::pin(async move { 58 61 ctx.set_activity(Some(ActivityData::custom("new bot, who dis?"))); 59 62 60 - poise::builtins::register_globally(ctx, &framework.options().commands).await?; 63 + poise::builtins::register_globally(ctx, &framework.options().commands).await?; 61 64 62 65 Ok(Data { 63 - client: Client::builder() 64 - .user_agent("blahaj") 65 - .build()?, 66 - }) 66 + client: Client::builder().user_agent("blahaj").build()?, 67 + }) 67 68 }) 68 69 }) 69 70 .options(opts) 70 71 .build(); 71 72 72 - let client = serenity::ClientBuilder::new(token, intents) 73 + let client = ClientBuilder::new(token, intents) 73 74 .framework(framework) 74 75 .await; 75 76