change volume command

This commit is contained in:
jude-lafitteIII 2020-04-28 17:17:32 +01:00
parent e34d36b4a3
commit 9af33602b3
2 changed files with 66 additions and 14 deletions

View File

@ -8,4 +8,4 @@ edition = "2018"
serenity = {path = "/home/jude/serenity", features = ["voice"]} serenity = {path = "/home/jude/serenity", features = ["voice"]}
sqlx = {version = "0.3", default-features = false, features = ["runtime-tokio", "macros", "mysql", "bigdecimal"]} sqlx = {version = "0.3", default-features = false, features = ["runtime-tokio", "macros", "mysql", "bigdecimal"]}
dotenv = "0.15" dotenv = "0.15"
tokio = {version = "0.2.19", features = ["fs"]} tokio = {version = "0.2.19", features = ["fs", "sync"]}

View File

@ -42,6 +42,7 @@ use std::{
sync::Arc, sync::Arc,
}; };
use serenity::model::guild::Guild; use serenity::model::guild::Guild;
use tokio::sync::RwLockReadGuard;
struct SQLPool; struct SQLPool;
@ -58,7 +59,7 @@ impl TypeMapKey for VoiceManager {
static THEME_COLOR: u32 = 0x00e0f3; static THEME_COLOR: u32 = 0x00e0f3;
#[group] #[group]
#[commands(play, info, )] #[commands(play, info, help, change_volume, )]
struct General; struct General;
struct Sound { struct Sound {
@ -69,13 +70,13 @@ struct Sound {
struct GuildData { struct GuildData {
id: u64, id: u64,
name: Option<String>, pub name: Option<String>,
prefix: String, pub prefix: String,
volume: u8, pub volume: u8,
} }
impl GuildData { impl GuildData {
async fn get_from_id(guild_id: u64, db_pool: MySqlPool) -> Result<GuildData, Box<dyn std::error::Error>> { async fn get_from_id(guild_id: u64, db_pool: MySqlPool) -> Option<GuildData> {
let guild = sqlx::query_as!( let guild = sqlx::query_as!(
GuildData, GuildData,
" "
@ -85,12 +86,16 @@ WHERE id = ?
", guild_id ", guild_id
) )
.fetch_one(&db_pool) .fetch_one(&db_pool)
.await?; .await;
Ok(guild) match guild {
Ok(guild) => Some(guild),
Err(_) => None,
}
} }
async fn create_from_id(guild: Guild, db_pool: MySqlPool) -> Result<GuildData, Box<dyn std::error::Error>> { async fn create_from_guild(guild: RwLockReadGuard<'_, Guild>, db_pool: MySqlPool) -> Result<GuildData, Box<dyn std::error::Error>> {
let guild_data = sqlx::query!( let guild_data = sqlx::query!(
" "
INSERT INTO servers (id, name) INSERT INTO servers (id, name)
@ -102,7 +107,7 @@ VALUES (?, ?)
Ok(GuildData { Ok(GuildData {
id: *guild.id.as_u64(), id: *guild.id.as_u64(),
name: Some(guild.name), name: Some(guild.name.clone()),
prefix: String::from("?"), prefix: String::from("?"),
volume: 100, volume: 100,
}) })
@ -145,9 +150,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.get::<SQLPool>().cloned().expect("Could not get SQLPool from data"); .get::<SQLPool>().cloned().expect("Could not get SQLPool from data");
match GuildData::get_from_id(*msg.guild_id.unwrap().as_u64(), pool).await { match GuildData::get_from_id(*msg.guild_id.unwrap().as_u64(), pool).await {
Ok(guild) => Some(guild.prefix), Some(guild) => Some(guild.prefix),
Err(_) => Some(String::from("?")) None => Some(String::from("?"))
} }
}))) })))
.group(&GENERAL_GROUP); .group(&GENERAL_GROUP);
@ -251,7 +256,8 @@ async fn store_sound_source(sound: &Sound) -> Result<String, Box<dyn std::error:
Ok(path_name) Ok(path_name)
} }
#[command] #[command("play")]
#[aliases("p")]
async fn play(ctx: &mut Context, msg: &Message, args: Args) -> CommandResult { async fn play(ctx: &mut Context, msg: &Message, args: Args) -> CommandResult {
let guild = match msg.guild(&ctx.cache).await { let guild = match msg.guild(&ctx.cache).await {
Some(guild) => guild, Some(guild) => guild,
@ -338,7 +344,6 @@ async fn help(ctx: &mut Context, msg: &Message, _args: Args) -> CommandResult {
#[command] #[command]
async fn info(ctx: &mut Context, msg: &Message, _args: Args) -> CommandResult { async fn info(ctx: &mut Context, msg: &Message, _args: Args) -> CommandResult {
msg.channel_id.send_message(&ctx, |m| m msg.channel_id.send_message(&ctx, |m| m
.embed(|e| e .embed(|e| e
.title("Info") .title("Info")
@ -358,3 +363,50 @@ There is a maximum sound limit per user. This can be removed by donating at http
Ok(()) Ok(())
} }
#[command("volume")]
async fn change_volume(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
let guild = match msg.guild(&ctx.cache).await {
Some(guild) => guild,
None => {
return Ok(());
}
};
let pool = ctx.data.read().await
.get::<SQLPool>().cloned().expect("Could not get SQLPool from data");
let mut guild_data_opt = GuildData::get_from_id(*guild.read().await.id.as_u64(), pool.clone()).await;
if guild_data_opt.is_none() {
guild_data_opt = Some(GuildData::create_from_guild(guild.read().await, pool.clone()).await.unwrap())
}
let mut guild_data = guild_data_opt.unwrap();
if args.len() == 1 {
match args.single::<u8>() {
Ok(volume) => {
guild_data.volume = volume;
guild_data.commit(pool).await?;
msg.channel_id.say(&ctx, format!("Volume changed to {}%", volume)).await?;
}
Err(_) => {
msg.channel_id.say(&ctx,
format!("Current server volume: {vol}%. Change the volume with ```{prefix}volume <new volume>```",
vol = guild_data.volume, prefix = guild_data.prefix)).await?;
}
}
}
else {
msg.channel_id.say(&ctx,
format!("Current server volume: {vol}%. Change the volume with ```{prefix}volume <new volume>```",
vol = guild_data.volume, prefix = guild_data.prefix)).await?;
}
Ok(())
}