Change dj role to be optional

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/2/head
trivernis 4 years ago
parent 69e7a2a18f
commit 0d43e8294e
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -2,8 +2,8 @@ use crate::error::DatabaseResult;
use crate::models::*; use crate::models::*;
use crate::schema::*; use crate::schema::*;
use crate::PoolConnection; use crate::PoolConnection;
use diesel::insert_into;
use diesel::prelude::*; use diesel::prelude::*;
use diesel::{delete, insert_into};
use std::any; use std::any;
use std::fmt::Debug; use std::fmt::Debug;
use std::str::FromStr; use std::str::FromStr;
@ -79,6 +79,19 @@ impl Database {
Ok(()) Ok(())
} }
/// Deletes a guild setting
pub fn delete_guild_setting(&self, guild_id: u64, key: &str) -> DatabaseResult<()> {
use guild_settings::dsl;
log::debug!("Deleting '{}' for guild {}", key, guild_id);
let connection = self.pool.get()?;
delete(dsl::guild_settings)
.filter(dsl::guild_id.eq(guild_id as i64))
.filter(dsl::key.eq(key))
.execute(&connection)?;
Ok(())
}
/// Returns a list of all guild playlists /// Returns a list of all guild playlists
pub fn get_guild_playlists(&self, guild_id: u64) -> DatabaseResult<Vec<GuildPlaylist>> { pub fn get_guild_playlists(&self, guild_id: u64) -> DatabaseResult<Vec<GuildPlaylist>> {
use guild_playlists::dsl; use guild_playlists::dsl;

@ -4,16 +4,20 @@ use serenity::framework::standard::CommandResult;
use serenity::model::channel::Message; use serenity::model::channel::Message;
use crate::commands::common::handle_autodelete; use crate::commands::common::handle_autodelete;
use crate::commands::music::get_queue_for_guild; use crate::commands::music::{get_queue_for_guild, is_dj};
#[command] #[command]
#[only_in(guilds)] #[only_in(guilds)]
#[description("Clears the queue")] #[description("Clears the queue")]
#[usage("")] #[usage("")]
#[aliases("cl")] #[aliases("cl")]
#[allowed_roles("DJ")]
async fn clear_queue(ctx: &Context, msg: &Message) -> CommandResult { async fn clear_queue(ctx: &Context, msg: &Message) -> CommandResult {
let guild = msg.guild(&ctx.cache).await.unwrap(); let guild = msg.guild(&ctx.cache).await.unwrap();
if !is_dj(ctx, guild.id, &msg.author).await? {
msg.channel_id.say(ctx, "Requires DJ permissions").await?;
return Ok(());
}
log::debug!("Clearing queue for guild {}", guild.id); log::debug!("Clearing queue for guild {}", guild.id);
let queue = get_queue_for_guild(ctx, &guild.id).await?; let queue = get_queue_for_guild(ctx, &guild.id).await?;

@ -4,17 +4,21 @@ use serenity::framework::standard::CommandResult;
use serenity::model::channel::Message; use serenity::model::channel::Message;
use crate::commands::common::handle_autodelete; use crate::commands::common::handle_autodelete;
use crate::commands::music::{get_queue_for_guild, get_voice_manager}; use crate::commands::music::{get_queue_for_guild, get_voice_manager, is_dj};
#[command] #[command]
#[only_in(guilds)] #[only_in(guilds)]
#[description("Leaves a voice channel")] #[description("Leaves a voice channel")]
#[usage("")] #[usage("")]
#[aliases("stop")] #[aliases("stop")]
#[allowed_roles("DJ")]
async fn leave(ctx: &Context, msg: &Message) -> CommandResult { async fn leave(ctx: &Context, msg: &Message) -> CommandResult {
let guild = msg.guild(&ctx.cache).await.unwrap(); let guild = msg.guild(&ctx.cache).await.unwrap();
log::debug!("Leave request received for guild {}", guild.id); log::debug!("Leave request received for guild {}", guild.id);
if !is_dj(ctx, guild.id, &msg.author).await? {
msg.channel_id.say(ctx, "Requires DJ permissions").await?;
return Ok(());
}
let manager = get_voice_manager(ctx).await; let manager = get_voice_manager(ctx).await;
let queue = get_queue_for_guild(ctx, &guild.id).await?; let queue = get_queue_for_guild(ctx, &guild.id).await?;
let queue_lock = queue.lock().await; let queue_lock = queue.lock().await;

@ -36,6 +36,7 @@ mod save_playlist;
mod shuffle; mod shuffle;
mod skip; mod skip;
use crate::providers::settings::{get_setting, Setting};
use clear_queue::CLEAR_QUEUE_COMMAND; use clear_queue::CLEAR_QUEUE_COMMAND;
use current::CURRENT_COMMAND; use current::CURRENT_COMMAND;
use join::JOIN_COMMAND; use join::JOIN_COMMAND;
@ -47,6 +48,7 @@ use play_next::PLAY_NEXT_COMMAND;
use playlists::PLAYLISTS_COMMAND; use playlists::PLAYLISTS_COMMAND;
use queue::QUEUE_COMMAND; use queue::QUEUE_COMMAND;
use save_playlist::SAVE_PLAYLIST_COMMAND; use save_playlist::SAVE_PLAYLIST_COMMAND;
use serenity::model::user::User;
use shuffle::SHUFFLE_COMMAND; use shuffle::SHUFFLE_COMMAND;
use skip::SKIP_COMMAND; use skip::SKIP_COMMAND;
@ -369,3 +371,22 @@ async fn added_multiple_msg(ctx: &Context, msg: &Message, songs: &mut Vec<Song>)
.await?; .await?;
Ok(()) Ok(())
} }
/// Returns if the given user is a dj in the given guild based on the
/// setting for the name of the dj role
async fn is_dj(ctx: &Context, guild: GuildId, user: &User) -> BotResult<bool> {
let dj_role = get_setting::<String>(ctx, guild, Setting::MusicDjRole).await?;
if let Some(role_name) = dj_role {
let roles = ctx.http.get_guild_roles(guild.0).await?;
let role_result = roles.iter().find(|r| r.name == role_name);
if let Some(role) = role_result {
Ok(user.has_role(ctx, guild, role.id).await?)
} else {
Ok(false)
}
} else {
Ok(true)
}
}

@ -4,16 +4,20 @@ use serenity::model::channel::Message;
use serenity::prelude::*; use serenity::prelude::*;
use crate::commands::common::handle_autodelete; use crate::commands::common::handle_autodelete;
use crate::commands::music::get_queue_for_guild; use crate::commands::music::{get_queue_for_guild, is_dj};
#[command] #[command]
#[only_in(guilds)] #[only_in(guilds)]
#[description("Pauses playback")] #[description("Pauses playback")]
#[usage("")] #[usage("")]
#[allowed_roles("DJ")]
async fn pause(ctx: &Context, msg: &Message) -> CommandResult { async fn pause(ctx: &Context, msg: &Message) -> CommandResult {
let guild = msg.guild(&ctx.cache).await.unwrap(); let guild = msg.guild(&ctx.cache).await.unwrap();
log::debug!("Pausing playback for guild {}", guild.id); log::debug!("Pausing playback for guild {}", guild.id);
if !is_dj(ctx, guild.id, &msg.author).await? {
msg.channel_id.say(ctx, "Requires DJ permissions").await?;
return Ok(());
}
let queue = get_queue_for_guild(ctx, &guild.id).await?; let queue = get_queue_for_guild(ctx, &guild.id).await?;
let mut queue_lock = queue.lock().await; let mut queue_lock = queue.lock().await;

@ -5,7 +5,7 @@ use serenity::model::channel::Message;
use crate::commands::common::handle_autodelete; use crate::commands::common::handle_autodelete;
use crate::commands::music::{ use crate::commands::music::{
get_channel_for_author, get_queue_for_guild, get_songs_for_query, get_voice_manager, get_channel_for_author, get_queue_for_guild, get_songs_for_query, get_voice_manager, is_dj,
join_channel, play_next_in_queue, join_channel, play_next_in_queue,
}; };
@ -15,13 +15,15 @@ use crate::commands::music::{
#[usage("<song-url>")] #[usage("<song-url>")]
#[min_args(1)] #[min_args(1)]
#[aliases("pn", "play-next")] #[aliases("pn", "play-next")]
#[allowed_roles("DJ")]
async fn play_next(ctx: &Context, msg: &Message, args: Args) -> CommandResult { async fn play_next(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
let query = args.message(); let query = args.message();
let guild = msg.guild(&ctx.cache).await.unwrap(); let guild = msg.guild(&ctx.cache).await.unwrap();
log::debug!("Playing song as next song for guild {}", guild.id); log::debug!("Playing song as next song for guild {}", guild.id);
if !is_dj(ctx, guild.id, &msg.author).await? {
msg.channel_id.say(ctx, "Requires DJ permissions").await?;
return Ok(());
}
let manager = get_voice_manager(ctx).await; let manager = get_voice_manager(ctx).await;
let mut handler = manager.get(guild.id); let mut handler = manager.get(guild.id);

@ -1,3 +1,4 @@
use crate::commands::music::is_dj;
use crate::utils::context_data::get_database_from_context; use crate::utils::context_data::get_database_from_context;
use serenity::client::Context; use serenity::client::Context;
use serenity::framework::standard::macros::command; use serenity::framework::standard::macros::command;
@ -11,9 +12,13 @@ use serenity::model::channel::Message;
#[example("anime https://www.youtube.com/playlist?list=PLqaM77H_o5hykROCe3uluvZEaPo6bZj-C")] #[example("anime https://www.youtube.com/playlist?list=PLqaM77H_o5hykROCe3uluvZEaPo6bZj-C")]
#[min_args(2)] #[min_args(2)]
#[aliases("add-playlist", "save-playlist")] #[aliases("add-playlist", "save-playlist")]
#[allowed_roles("DJ")]
async fn save_playlist(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult { async fn save_playlist(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
let guild = msg.guild(&ctx.cache).await.unwrap(); let guild = msg.guild(&ctx.cache).await.unwrap();
if !is_dj(ctx, guild.id, &msg.author).await? {
msg.channel_id.say(ctx, "Requires DJ permissions").await?;
return Ok(());
}
let name: String = args.single().unwrap(); let name: String = args.single().unwrap();
let url: &str = args.remains().unwrap(); let url: &str = args.remains().unwrap();
log::debug!( log::debug!(

@ -4,18 +4,21 @@ use serenity::framework::standard::CommandResult;
use serenity::model::channel::Message; use serenity::model::channel::Message;
use crate::commands::common::handle_autodelete; use crate::commands::common::handle_autodelete;
use crate::commands::music::get_queue_for_guild; use crate::commands::music::{get_queue_for_guild, is_dj};
#[command] #[command]
#[only_in(guilds)] #[only_in(guilds)]
#[description("Shuffles the queue")] #[description("Shuffles the queue")]
#[usage("")] #[usage("")]
#[aliases("sh")] #[aliases("sh")]
#[allowed_roles("DJ")]
async fn shuffle(ctx: &Context, msg: &Message) -> CommandResult { async fn shuffle(ctx: &Context, msg: &Message) -> CommandResult {
let guild = msg.guild(&ctx.cache).await.unwrap(); let guild = msg.guild(&ctx.cache).await.unwrap();
log::debug!("Shuffling queue for guild {}", guild.id); log::debug!("Shuffling queue for guild {}", guild.id);
if !is_dj(ctx, guild.id, &msg.author).await? {
msg.channel_id.say(ctx, "Requires DJ permissions").await?;
return Ok(());
}
let queue = get_queue_for_guild(ctx, &guild.id).await?; let queue = get_queue_for_guild(ctx, &guild.id).await?;
{ {
let mut queue_lock = queue.lock().await; let mut queue_lock = queue.lock().await;

@ -4,17 +4,19 @@ use serenity::framework::standard::CommandResult;
use serenity::model::channel::Message; use serenity::model::channel::Message;
use crate::commands::common::handle_autodelete; use crate::commands::common::handle_autodelete;
use crate::commands::music::get_queue_for_guild; use crate::commands::music::{get_queue_for_guild, is_dj};
#[command] #[command]
#[only_in(guilds)] #[only_in(guilds)]
#[description("Skips to the next song")] #[description("Skips to the next song")]
#[usage("")] #[usage("")]
#[aliases("next")] #[aliases("next")]
#[allowed_roles("DJ")]
async fn skip(ctx: &Context, msg: &Message) -> CommandResult { async fn skip(ctx: &Context, msg: &Message) -> CommandResult {
let guild = msg.guild(&ctx.cache).await.unwrap(); let guild = msg.guild(&ctx.cache).await.unwrap();
if !is_dj(ctx, guild.id, &msg.author).await? {
msg.channel_id.say(ctx, "Requires DJ permissions").await?;
return Ok(());
}
log::debug!("Skipping song for guild {}", guild.id); log::debug!("Skipping song for guild {}", guild.id);
let queue = get_queue_for_guild(ctx, &guild.id).await?; let queue = get_queue_for_guild(ctx, &guild.id).await?;
let queue_lock = queue.lock().await; let queue_lock = queue.lock().await;

@ -37,21 +37,23 @@ async fn get(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
} }
} else { } else {
log::debug!("Displaying all guild settings"); log::debug!("Displaying all guild settings");
let mut kv_pairs = Vec::new();
for key in ALL_SETTINGS { for key in ALL_SETTINGS {
let key = key.to_string(); let key = key.to_string();
let mut kv_pairs = Vec::new();
{ {
match database.get_guild_setting::<String>(guild.id.0, &key)? { match database.get_guild_setting::<String>(guild.id.0, &key)? {
Some(value) => kv_pairs.push(format!("`{}` = `{}`", key, value)), Some(value) => kv_pairs.push(format!("`{}` = `{}`", key, value)),
None => kv_pairs.push(format!("`{}` not set", key)), None => kv_pairs.push(format!("`{}` not set", key)),
} }
} }
msg.channel_id
.send_message(ctx, |m| {
m.embed(|e| e.title("Guild Settings").description(kv_pairs.join("\n")))
})
.await?;
} }
msg.channel_id
.send_message(ctx, |m| {
m.embed(|e| e.title("Guild Settings").description(kv_pairs.join("\n")))
})
.await?;
} }
Ok(()) Ok(())

@ -8,10 +8,12 @@ use crate::utils::context_data::get_database_from_context;
#[command] #[command]
#[only_in(guilds)] #[only_in(guilds)]
#[description("Set a guild setting")] #[description(
#[usage("<setting> <value>")] "Set a guild setting. If no value is given the setting will be reset to the default value."
)]
#[usage("<setting> (<value>)")]
#[example("music.autoshuffle true")] #[example("music.autoshuffle true")]
#[min_args(2)] #[min_args(1)]
#[max_args(2)] #[max_args(2)]
#[required_permissions("MANAGE_GUILD")] #[required_permissions("MANAGE_GUILD")]
async fn set(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult { async fn set(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
@ -24,14 +26,20 @@ async fn set(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
.await?; .await?;
return Ok(()); return Ok(());
} }
let value = args.single::<String>().unwrap();
let database = get_database_from_context(ctx).await; let database = get_database_from_context(ctx).await;
let guild = msg.guild(&ctx.cache).await.unwrap(); let guild = msg.guild(&ctx.cache).await.unwrap();
database.set_guild_setting(guild.id.0, &key, value.clone())?; if let Ok(value) = args.single::<String>() {
msg.channel_id database.set_guild_setting(guild.id.0, &key, value.clone())?;
.say(ctx, format!("Set `{}` to `{}`", key, value)) msg.channel_id
.await?; .say(ctx, format!("Set `{}` to `{}`", key, value))
.await?;
} else {
database.delete_guild_setting(guild.id.0, &key)?;
msg.channel_id
.say(ctx, format!("Setting `{}` reset to default", key))
.await?;
}
Ok(()) Ok(())
} }

@ -4,11 +4,16 @@ use serenity::client::Context;
use serenity::model::prelude::GuildId; use serenity::model::prelude::GuildId;
use std::str::FromStr; use std::str::FromStr;
pub static ALL_SETTINGS: &[Setting] = &[Setting::MusicAutoShuffle, Setting::BotAutoDelete]; pub static ALL_SETTINGS: &[Setting] = &[
Setting::MusicAutoShuffle,
Setting::BotAutoDelete,
Setting::MusicDjRole,
];
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum Setting { pub enum Setting {
MusicAutoShuffle, MusicAutoShuffle,
MusicDjRole,
BotAutoDelete, BotAutoDelete,
} }
@ -17,6 +22,7 @@ impl ToString for Setting {
match self { match self {
Self::MusicAutoShuffle => "music.autoshuffle".to_string(), Self::MusicAutoShuffle => "music.autoshuffle".to_string(),
Self::BotAutoDelete => "bot.autodelete".to_string(), Self::BotAutoDelete => "bot.autodelete".to_string(),
Self::MusicDjRole => "music.dj-role".to_string(),
} }
} }
} }

Loading…
Cancel
Save