Open beta

main
DaXcess 2022-11-01 09:36:25 +01:00
parent d530b182b3
commit 6da879ee0c
9 changed files with 58 additions and 44 deletions

View File

@ -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";

View File

@ -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 </link:1036714850367320136> or go to https://account.spoticord.com/ to get started.")
.description("You need to link your Spotify account. Use </link:1036714850367320136> 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 </link:1036714850367320136> or go to https://account.spoticord.com/ to get started.")
.description("You need to link your Spotify account. Use </link:1036714850367320136> or go to [the accounts website](https://account.spoticord.com/) to get started.")
.status(Status::Error)
.build(),
true,

View File

@ -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);

View File

@ -7,7 +7,7 @@ use serenity::{
prelude::{Context, EventHandler},
};
use crate::utils::consts::MOTD;
use crate::consts::MOTD;
use super::commands::CommandManager;

View File

@ -14,6 +14,7 @@ use crate::{
mod audio;
mod bot;
mod consts;
mod database;
mod ipc;
mod librespot_ext;

View File

@ -1,3 +1,8 @@
use serenity::{
model::{prelude::UserId, user::User},
prelude::Context,
};
pub fn escape(text: impl Into<String>) -> String {
let text: String = text.into();
@ -8,3 +13,15 @@ pub fn escape(text: impl Into<String>) -> String {
.replace("~", "\\~")
.replace("`", "\\`")
}
pub async fn get_user(ctx: &Context, id: UserId) -> Option<User> {
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)
}

View File

@ -1,6 +1,5 @@
use std::time::{SystemTime, UNIX_EPOCH};
pub mod consts;
pub mod discord;
pub mod embed;
pub mod spotify;

View File

@ -5,40 +5,6 @@ use log::{error, trace};
use serde::Deserialize;
use serde_json::Value;
pub async fn get_username(token: impl Into<String>) -> Result<String, String> {
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<String>) -> Result<String, String> {
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<String>,
track: SpotifyId,