diff --git a/src/bot/commands/core/version.rs b/src/bot/commands/core/version.rs index d7f5aa4..ffa3e69 100644 --- a/src/bot/commands/core/version.rs +++ b/src/bot/commands/core/version.rs @@ -7,10 +7,7 @@ use serenity::{ prelude::Context, }; -use crate::{ - bot::commands::CommandOutput, - utils::{consts::VERSION, embed::Status}, -}; +use crate::{bot::commands::CommandOutput, consts::VERSION, utils::embed::Status}; pub const NAME: &str = "version"; diff --git a/src/bot/commands/music/join.rs b/src/bot/commands/music/join.rs index 730ff9c..1347ac1 100644 --- a/src/bot/commands/music/join.rs +++ b/src/bot/commands/music/join.rs @@ -101,7 +101,7 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu EmbedBuilder::new() .title("Cannot join voice channel") .icon_url("https://spoticord.com/static/image/prohibited.png") - .description("You need to link your Spotify account. Use or go to https://account.spoticord.com/ to get started.") + .description("You need to link your Spotify account. Use or go to [the accounts website](https://account.spoticord.com/) to get started.") .status(Status::Error) .build(), true, @@ -141,7 +141,7 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu EmbedBuilder::new() .title("Cannot join voice channel") .icon_url("https://spoticord.com/static/image/prohibited.png") - .description("You need to link your Spotify account. Use or go to https://account.spoticord.com/ to get started.") + .description("You need to link your Spotify account. Use or go to [the accounts website](https://account.spoticord.com/) to get started.") .status(Status::Error) .build(), true, diff --git a/src/bot/commands/music/playing.rs b/src/bot/commands/music/playing.rs index c1ccf6c..8994a87 100644 --- a/src/bot/commands/music/playing.rs +++ b/src/bot/commands/music/playing.rs @@ -103,11 +103,11 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu description.push_str(&format!("{} / {}", utils::time_to_str(position / 1000), utils::time_to_str(pbi.duration_ms / 1000))); // Get owner of session - let owner = match ctx.cache.user(owner) { + let owner = match utils::discord::get_user(&ctx, owner).await { Some(user) => user, None => { // This shouldn't happen - // TODO: This can happen, idk when + // TODO: Test if this can no longer happen error!("Could not find user with id {}", owner); diff --git a/src/bot/events.rs b/src/bot/events.rs index 7241dab..16a49a6 100644 --- a/src/bot/events.rs +++ b/src/bot/events.rs @@ -7,7 +7,7 @@ use serenity::{ prelude::{Context, EventHandler}, }; -use crate::utils::consts::MOTD; +use crate::consts::MOTD; use super::commands::CommandManager; diff --git a/src/utils/consts.rs b/src/consts.rs similarity index 100% rename from src/utils/consts.rs rename to src/consts.rs diff --git a/src/main.rs b/src/main.rs index cb301d0..83855c7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,6 +14,7 @@ use crate::{ mod audio; mod bot; +mod consts; mod database; mod ipc; mod librespot_ext; diff --git a/src/utils/discord.rs b/src/utils/discord.rs index 2e56418..9643c8d 100644 --- a/src/utils/discord.rs +++ b/src/utils/discord.rs @@ -1,3 +1,8 @@ +use serenity::{ + model::{prelude::UserId, user::User}, + prelude::Context, +}; + pub fn escape(text: impl Into) -> String { let text: String = text.into(); @@ -8,3 +13,15 @@ pub fn escape(text: impl Into) -> String { .replace("~", "\\~") .replace("`", "\\`") } + +pub async fn get_user(ctx: &Context, id: UserId) -> Option { + let user = match ctx.cache.user(id) { + Some(user) => user, + None => match ctx.http.get_user(id.0).await { + Ok(user) => user, + Err(_) => return None, + }, + }; + + Some(user) +} diff --git a/src/utils/mod.rs b/src/utils/mod.rs index d457b50..c9520ed 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,6 +1,5 @@ use std::time::{SystemTime, UNIX_EPOCH}; -pub mod consts; pub mod discord; pub mod embed; pub mod spotify; diff --git a/src/utils/spotify.rs b/src/utils/spotify.rs index 7364b66..4857233 100644 --- a/src/utils/spotify.rs +++ b/src/utils/spotify.rs @@ -5,40 +5,6 @@ use log::{error, trace}; use serde::Deserialize; use serde_json::Value; -pub async fn get_username(token: impl Into) -> Result { - let token = token.into(); - let client = reqwest::Client::new(); - - let response = match client - .get("https://api.spotify.com/v1/me") - .bearer_auth(token) - .send() - .await - { - Ok(response) => response, - Err(why) => { - error!("Failed to get username: {}", why); - return Err(format!("{}", why)); - } - }; - - let body: Value = match response.json().await { - Ok(body) => body, - Err(why) => { - error!("Failed to parse body: {}", why); - return Err(format!("{}", why)); - } - }; - - if let Value::String(username) = &body["id"] { - trace!("Got username: {}", username); - return Ok(username.clone()); - } - - error!("Missing 'id' field in body"); - Err("Failed to parse body: Invalid body received".to_string()) -} - #[derive(Debug, Clone, Deserialize)] pub struct Artist { pub name: String, @@ -76,6 +42,40 @@ pub struct Episode { pub show: Show, } +pub async fn get_username(token: impl Into) -> Result { + let token = token.into(); + let client = reqwest::Client::new(); + + let response = match client + .get("https://api.spotify.com/v1/me") + .bearer_auth(token) + .send() + .await + { + Ok(response) => response, + Err(why) => { + error!("Failed to get username: {}", why); + return Err(format!("{}", why)); + } + }; + + let body: Value = match response.json().await { + Ok(body) => body, + Err(why) => { + error!("Failed to parse body: {}", why); + return Err(format!("{}", why)); + } + }; + + if let Value::String(username) = &body["id"] { + trace!("Got username: {}", username); + return Ok(username.clone()); + } + + error!("Missing 'id' field in body"); + Err("Failed to parse body: Invalid body received".to_string()) +} + pub async fn get_track_info( token: impl Into, track: SpotifyId,