removed popular command. fix for ambience command

This commit is contained in:
jellywx 2021-09-18 13:09:14 +01:00
parent 6f86f2afa0
commit 1cc1068788
9 changed files with 75 additions and 129 deletions

2
Cargo.lock generated
View File

@ -1706,7 +1706,7 @@ dependencies = [
[[package]]
name = "soundfx-rs"
version = "1.4.2"
version = "1.4.3"
dependencies = [
"dashmap",
"dotenv",

View File

@ -1,6 +1,6 @@
[package]
name = "soundfx-rs"
version = "1.4.2"
version = "1.4.3"
authors = ["jellywx <judesouthworth@pm.me>"]
edition = "2018"

View File

@ -155,7 +155,7 @@ pub async fn upload_new_sound(
invoke.respond(
ctx.http.clone(),
CreateGenericResponse::new().content(format!(
"You have reached the maximum number of sounds ({}). Either delete some with `?delete` or join our Patreon for unlimited uploads at **https://patreon.com/jellywx**",
"You have reached the maximum number of sounds ({}). Either delete some with `/delete` or join our Patreon for unlimited uploads at **https://patreon.com/jellywx**",
*MAX_SOUNDS,
))).await?;
}
@ -170,7 +170,7 @@ pub async fn upload_new_sound(
.await?;
}
} else {
invoke.respond(ctx.http.clone(), CreateGenericResponse::new().content("Usage: `?upload <name>`. Please ensure the name provided is less than 20 characters in length")).await?;
invoke.respond(ctx.http.clone(), CreateGenericResponse::new().content("Usage: `/upload <name>`. Please ensure the name provided is less than 20 characters in length")).await?;
}
Ok(())

View File

@ -108,13 +108,14 @@ pub async fn play_ambience(
match channel_to_join {
Some(user_channel) => {
let search_name = args.named("name").unwrap().to_lowercase();
let audio_index = ctx.data.read().await.get::<AudioIndex>().cloned().unwrap();
if let Some(filename) = audio_index.get(&search_name) {
if let Some(search_name) = args.named("name") {
if let Some(filename) = audio_index.get(search_name) {
let (track, track_handler) = create_player(
Input::try_from(
Memory::new(ffmpeg(format!("audio/{}", filename)).await.unwrap()).unwrap(),
Memory::new(ffmpeg(format!("audio/{}", filename)).await.unwrap())
.unwrap(),
)
.unwrap(),
);
@ -166,6 +167,23 @@ __Available ambience sounds:__
)
.await?;
}
} else {
invoke
.respond(
ctx.http.clone(),
CreateGenericResponse::new().embed(|e| {
e.title("Available Sounds").description(
audio_index
.keys()
.into_iter()
.map(|i| i.as_str())
.collect::<Vec<&str>>()
.join("\n"),
)
}),
)
.await?;
}
}
None => {

View File

@ -14,13 +14,7 @@ fn format_search_results(search_results: Vec<Sound>) -> CreateGenericResponse {
let field_iter = search_results
.iter()
.take(25)
.map(|item| {
(
&item.name,
format!("ID: {}\nPlays: {}", item.id, item.plays),
true,
)
})
.map(|item| (&item.name, format!("ID: {}", item.id), true))
.filter(|item| {
current_character_count += item.0.len() + item.1.len();
@ -58,11 +52,11 @@ pub async fn list_sounds(
let mut message_buffer;
if args.named("me").map(|i| i.to_owned()) == Some("me".to_string()) {
sounds = Sound::get_user_sounds(invoke.author_id(), pool).await?;
sounds = Sound::user_sounds(invoke.author_id(), pool).await?;
message_buffer = "All your sounds: ".to_string();
} else {
sounds = Sound::get_guild_sounds(invoke.guild_id().unwrap(), pool).await?;
sounds = Sound::guild_sounds(invoke.guild_id().unwrap(), pool).await?;
message_buffer = "All sounds on this server: ".to_string();
}
@ -141,42 +135,6 @@ pub async fn search_sounds(
Ok(())
}
#[command("popular")]
#[group("Search")]
#[description("Show popular sounds")]
pub async fn show_popular_sounds(
ctx: &Context,
invoke: &(dyn CommandInvoke + Sync + Send),
_args: Args,
) -> CommandResult {
let pool = ctx
.data
.read()
.await
.get::<MySQL>()
.cloned()
.expect("Could not get SQLPool from data");
let search_results = sqlx::query_as_unchecked!(
Sound,
"
SELECT name, id, plays, public, server_id, uploader_id
FROM sounds
WHERE public = 1
ORDER BY plays DESC
LIMIT 25
"
)
.fetch_all(&pool)
.await?;
invoke
.respond(ctx.http.clone(), format_search_results(search_results))
.await?;
Ok(())
}
#[command("random")]
#[group("Search")]
#[description("Show a page of random sounds")]
@ -196,7 +154,7 @@ pub async fn show_random_sounds(
let search_results = sqlx::query_as_unchecked!(
Sound,
"
SELECT name, id, plays, public, server_id, uploader_id
SELECT name, id, public, server_id, uploader_id
FROM sounds
WHERE public = 1
ORDER BY rand()

View File

@ -137,7 +137,7 @@ impl EventHandler for Handler {
let mut sound = sqlx::query_as_unchecked!(
Sound,
"
SELECT name, id, plays, public, server_id, uploader_id
SELECT name, id, public, server_id, uploader_id
FROM sounds
WHERE id = ?
",

View File

@ -44,7 +44,7 @@ impl CtxGuildData for Context {
} else {
let pool = self.data.read().await.get::<MySQL>().cloned().unwrap();
match GuildData::get_from_id(guild_id, pool).await {
match GuildData::from_id(guild_id, pool).await {
Ok(d) => {
let lock = Arc::new(RwLock::new(d));
@ -62,7 +62,7 @@ impl CtxGuildData for Context {
}
impl GuildData {
pub async fn get_from_id<G: Into<GuildId>>(
pub async fn from_id<G: Into<GuildId>>(
guild_id: G,
db_pool: MySqlPool,
) -> Result<GuildData, sqlx::Error> {

View File

@ -8,7 +8,7 @@ mod framework;
mod guild_data;
mod sound;
use std::{collections::HashMap, env, sync::Arc, time::Duration};
use std::{collections::HashMap, env, sync::Arc};
use dashmap::DashMap;
use dotenv::dotenv;
@ -92,9 +92,6 @@ async fn play_audio(
call_handler.play(track);
sound.plays += 1;
sound.commit(mysql_pool).await?;
Ok(track_handler)
}
@ -262,7 +259,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
// search commands
.add_command(&cmds::search::LIST_SOUNDS_COMMAND)
.add_command(&cmds::search::SEARCH_SOUNDS_COMMAND)
.add_command(&cmds::search::SHOW_POPULAR_SOUNDS_COMMAND)
.add_command(&cmds::search::SHOW_RANDOM_SOUNDS_COMMAND);
if audio_index.is_some() {
@ -310,29 +306,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
framework_arc.build_slash(&client.cache_and_http.http).await;
let shard_manager = client.shard_manager.clone();
tokio::spawn(async move {
loop {
{
let mut shards = shard_manager
.lock()
.await
.runners
.lock()
.await
.iter()
.map(|(id, shard)| format!("\t{} {}", id.0, shard.stage))
.collect::<Vec<String>>();
shards.sort();
info!("Shard Status: [\n{}\n]", shards.join("\n"));
}
tokio::time::sleep(Duration::from_secs(60)).await;
}
});
if let Ok((Some(lower), Some(upper))) = env::var("SHARD_RANGE").map(|sr| {
let mut split = sr
.split(',')

View File

@ -110,7 +110,6 @@ WHERE
pub struct Sound {
pub name: String,
pub id: u32,
pub plays: u32,
pub public: bool,
pub server_id: u64,
pub uploader_id: Option<u64>,
@ -151,7 +150,7 @@ impl Sound {
let sound = sqlx::query_as_unchecked!(
Self,
"
SELECT name, id, plays, public, server_id, uploader_id
SELECT name, id, public, server_id, uploader_id
FROM sounds
WHERE id = ? AND (
public = 1 OR
@ -175,7 +174,7 @@ SELECT name, id, plays, public, server_id, uploader_id
sound = sqlx::query_as_unchecked!(
Self,
"
SELECT name, id, plays, public, server_id, uploader_id
SELECT name, id, public, server_id, uploader_id
FROM sounds
WHERE name = ? AND (
public = 1 OR
@ -196,7 +195,7 @@ SELECT name, id, plays, public, server_id, uploader_id
sound = sqlx::query_as_unchecked!(
Self,
"
SELECT name, id, plays, public, server_id, uploader_id
SELECT name, id, public, server_id, uploader_id
FROM sounds
WHERE name LIKE CONCAT('%', ?, '%') AND (
public = 1 OR
@ -311,12 +310,10 @@ SELECT COUNT(1) as count
"
UPDATE sounds
SET
plays = ?,
public = ?
WHERE
id = ?
",
self.plays,
self.public,
self.id
)
@ -408,14 +405,14 @@ INSERT INTO sounds (name, server_id, uploader_id, public, src)
}
}
pub async fn get_user_sounds<U: Into<u64>>(
pub async fn user_sounds<U: Into<u64>>(
user_id: U,
db_pool: MySqlPool,
) -> Result<Vec<Sound>, Box<dyn std::error::Error + Send + Sync>> {
let sounds = sqlx::query_as_unchecked!(
Sound,
"
SELECT name, id, plays, public, server_id, uploader_id
SELECT name, id, public, server_id, uploader_id
FROM sounds
WHERE uploader_id = ?
",
@ -427,14 +424,14 @@ SELECT name, id, plays, public, server_id, uploader_id
Ok(sounds)
}
pub async fn get_guild_sounds<G: Into<u64>>(
pub async fn guild_sounds<G: Into<u64>>(
guild_id: G,
db_pool: MySqlPool,
) -> Result<Vec<Sound>, Box<dyn std::error::Error + Send + Sync>> {
let sounds = sqlx::query_as_unchecked!(
Sound,
"
SELECT name, id, plays, public, server_id, uploader_id
SELECT name, id, public, server_id, uploader_id
FROM sounds
WHERE server_id = ?
",