Skip to content

Commit

Permalink
Merge branch 'master' of github.com:cycle-five/cracktunes
Browse files Browse the repository at this point in the history
  • Loading branch information
cycle-five committed Feb 27, 2024
2 parents 78b63a6 + ce81bfc commit 8e39139
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 45 deletions.
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ export SPOTIFY_CLIENT_SECRET=XXXXXX
# [Optional] OpenAI API key for the chatgpt feature.
#
export OPENAI_API_KEY=XXXXXX

#
# [Optional] pgadmin support
#
export PGADMIN_MAIL=XXXXXX
export PGADMIN_PW=XXXXXX
12 changes: 8 additions & 4 deletions crack-core/src/commands/music/skip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,15 @@ pub async fn downvote(ctx: Context<'_>) -> Result<(), Error> {
let queue = handler.queue();
let metadata = get_track_metadata(&queue.current().unwrap()).await;

ctx.data()
.downvote_track(guild_id, &metadata.source_url.unwrap().to_owned())
.await?;
let source_url = &metadata.source_url.ok_or("ASDF").unwrap();
let res1 = ctx.data().downvote_track(guild_id, source_url);

let res2 = force_skip_top_track(&handler);

tracing::warn!("downvoting track: {}", source_url);

tokio::join!(res1, res2).0?;

force_skip_top_track(&handler).await?;
Ok(())
}

Expand Down
4 changes: 2 additions & 2 deletions crack-core/src/commands/settings/set/set_auto_role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ pub async fn auto_role(
.guild_settings_map
.write()
.unwrap()
.entry(ctx.guild_id().unwrap())
.entry(guild_id)
.and_modify(|e| {
e.set_auto_role(Some(auto_role_id));
})
.or_insert_with(|| {
GuildSettings::new(
ctx.guild_id().unwrap(),
guild_id,
Some(&ctx.data().bot_settings.get_prefix()),
get_guild_name(ctx.serenity_context(), guild_id),
)
Expand Down
20 changes: 17 additions & 3 deletions crack-core/src/commands/settings/set/set_idle_timeout.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use crate::guild::settings::GuildSettings;
use crate::utils::check_reply;
use crate::utils::get_guild_name;
use crate::Context;
use crate::Error;
use poise::CreateReply;

/// Set the idle timeout for the bot in vc.
#[cfg(not(tarpaulin_include))]
#[poise::command(prefix_command, owners_only, ephemeral, aliases("set_idle_timeout"))]
pub async fn idle_timeout(
ctx: Context<'_>,
Expand All @@ -14,12 +17,23 @@ pub async fn idle_timeout(

let timeout = timeout * 60;

data.guild_settings_map
let _res = data
.guild_settings_map
.write()
.unwrap()
.entry(guild_id)
.and_modify(|e| e.timeout = timeout);

.and_modify(|e| e.timeout = timeout)
.or_insert_with(|| {
GuildSettings::new(
guild_id,
Some(&ctx.data().bot_settings.get_prefix()),
get_guild_name(ctx.serenity_context(), guild_id),
)
.with_timeout(timeout)
.clone()
})
.welcome_settings
.clone();
check_reply(
ctx.send(
CreateReply::default()
Expand Down
74 changes: 57 additions & 17 deletions crack-core/src/commands/settings/set/set_volume.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,19 @@ use serenity::all::GuildId;

/// Get the current `volume` and `old_volume` setting for the guild.
pub fn set_volume(
guild_settings_map: GuildSettingsMapParam,
guild_settings_map: &GuildSettingsMapParam,
guild_id: GuildId,
vol: f32,
) -> (f32, f32) {
let guild_settings = {
let mut guild_settings_mut = guild_settings_map.write().unwrap();
guild_settings_mut
.entry(guild_id)
.and_modify(|e| {
e.volume = vol;
e.old_volume = e.volume;
})
.or_insert(GuildSettings::new(guild_id, Some(DEFAULT_PREFIX), None))
.clone()
};
let mut guild_settings_mut = guild_settings_map.write().unwrap();
guild_settings_mut
.entry(guild_id)
.and_modify(|e| {
e.old_volume = e.volume;
e.volume = vol;
})
.or_insert(GuildSettings::new(guild_id, Some(DEFAULT_PREFIX), None).with_volume(vol));
let guild_settings = guild_settings_mut.get(&guild_id).unwrap();
(guild_settings.volume, guild_settings.old_volume)
}

Expand All @@ -35,12 +33,54 @@ pub async fn volume(
let guild_id = ctx.guild_id().unwrap();

let (vol, old_vol) = {
let guild_settings_map = ctx.data().guild_settings_map.clone();
let guild_settings_map = &ctx.data().guild_settings_map;
set_volume(guild_settings_map, guild_id, volume)
};

ctx.say(format!("vol: {}, old_vol: {}", vol, old_vol))
.await
.map_err(|e| e.into())
.map(|_| ())
let msg = ctx
.say(format!("vol: {}, old_vol: {}", vol, old_vol))
.await?
.into_message()
.await?;
ctx.data().add_msg_to_cache(guild_id, msg);
Ok(())
}

#[cfg(test)]
mod test {
use crate::commands::settings::set::set_volume::set_volume;
use crate::guild::settings::GuildSettingsMapParam;
use serenity::model::id::GuildId;

#[test]
fn test_set_volume() {
let guild_id = GuildId::new(1);
let guild_settings_map = GuildSettingsMapParam::default();

let (vol, old_vol) = set_volume(&guild_settings_map, guild_id, 0.5);
assert_eq!(vol, 0.5);
assert_eq!(old_vol, 0.1);
assert_eq!(
guild_settings_map
.read()
.unwrap()
.get(&guild_id)
.unwrap()
.volume,
vol
);

let (vol, old_vol) = set_volume(&guild_settings_map, guild_id, 0.6);
assert_eq!(vol, 0.6);
assert_eq!(old_vol, 0.5);
assert_eq!(
guild_settings_map
.read()
.unwrap()
.get(&guild_id)
.unwrap()
.volume,
vol
);
}
}
21 changes: 14 additions & 7 deletions crack-core/src/db/guild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ impl GuildEntity {
}

/// Create or update the welcome settings for a guild.
#[cfg(not(tarpaulin_include))]
pub async fn write_welcome_settings(
pool: &PgPool,
guild_id: i64,
Expand All @@ -154,6 +155,7 @@ impl GuildEntity {
}

/// Update the premium status for a guild.
#[cfg(not(tarpaulin_include))]
pub async fn update_premium(
pool: &PgPool,
guild_id: i64,
Expand All @@ -176,6 +178,7 @@ impl GuildEntity {
}

/// Write the settings for a guild to the database.
#[cfg(not(tarpaulin_include))]
pub async fn write_settings(
pool: &PgPool,
settings: &crate::guild::settings::GuildSettings,
Expand Down Expand Up @@ -260,16 +263,16 @@ impl GuildEntity {

/// Get the log settings for a guild from the database.
pub async fn get_log_settings(
&self,
pool: &PgPool,
id: i64,
) -> Result<Option<crate::guild::settings::LogSettings>, sqlx::Error> {
let settings_read = sqlx::query_as!(
LogSettingsRead,
r#"
SELECT * FROM log_settings
WHERE guild_id = $1
"#,
self.id
id
)
.fetch_optional(pool)
.await?;
Expand All @@ -278,16 +281,16 @@ impl GuildEntity {

/// Get the welcome settings for a guild from the database.
pub async fn get_welcome_settings(
&self,
pool: &PgPool,
id: i64,
) -> Result<Option<WelcomeSettings>, sqlx::Error> {
let settings_read = sqlx::query_as!(
WelcomeSettingsRead,
r#"
SELECT * FROM welcome_settings
WHERE guild_id = $1
"#,
self.id
id
)
.fetch_optional(pool)
.await?;
Expand Down Expand Up @@ -325,8 +328,8 @@ impl GuildEntity {
.await
}
}?;
let welcome_settings = self.get_welcome_settings(pool).await?;
let log_settings = self.get_log_settings(pool).await?;
let welcome_settings = GuildEntity::get_welcome_settings(pool, self.id).await?;
let log_settings = GuildEntity::get_log_settings(pool, self.id).await?;
Ok(GuildSettings::from(settings)
.with_welcome_settings(welcome_settings)
.with_log_settings(log_settings))
Expand Down Expand Up @@ -418,7 +421,11 @@ impl GuildEntity {
.fetch_one(pool)
.await?;

let guild_settings = GuildSettings::from(guild_settings);
let welcome_settings = GuildEntity::get_welcome_settings(pool, guild_id).await?;
let log_settings = GuildEntity::get_log_settings(pool, guild_id).await?;
let guild_settings = GuildSettings::from(guild_settings)
.with_welcome_settings(welcome_settings)
.with_log_settings(log_settings);
(guild_entity, guild_settings)
}
};
Expand Down
31 changes: 19 additions & 12 deletions crack-core/src/guild/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,12 +387,8 @@ impl GuildSettings {
let guild_id = self.guild_id.get() as i64;
let name = self.guild_name.clone();
let prefix = self.prefix.clone();
let (guild, mut settings) =
let (_guild, settings) =
crate::db::GuildEntity::get_or_create(pool, guild_id, name, prefix).await?;
let welcome_settings = guild.get_welcome_settings(pool).await?;
let log_settings = guild.get_log_settings(pool).await?;
settings.welcome_settings = welcome_settings;
settings.log_settings = log_settings;
Ok(settings)
}

Expand All @@ -402,11 +398,7 @@ impl GuildSettings {
let prefix = self.prefix.clone();
let (guild, _guild_settings) =
crate::db::GuildEntity::get_or_create(pool, guild_id, name, prefix).await?;
let mut settings = guild.get_settings(pool).await?;
let welcome_settings = guild.get_welcome_settings(pool).await?;
let log_settings = guild.get_log_settings(pool).await?;
settings.welcome_settings = welcome_settings;
settings.log_settings = log_settings;
let settings = guild.get_settings(pool).await?;
Ok(settings)
}

Expand Down Expand Up @@ -531,6 +523,17 @@ impl GuildSettings {
pub fn check_admin_user_id(&self, user_id: UserId) -> bool {
self.authorized_users.get(&user_id.into()).unwrap_or(&0) >= &ADMIN_VAL
}

/// Set the volume level without mutating.
pub fn with_volume(self, volume: f32) -> Self {
Self {
old_volume: self.volume,
volume,
..self
}
}

/// Set the volume level with mutating.
pub fn set_volume(&mut self, volume: f32) -> &mut Self {
self.old_volume = self.volume;
self.volume = volume;
Expand All @@ -547,6 +550,10 @@ impl GuildSettings {
self
}

pub fn with_timeout(self, timeout: u32) -> Self {
Self { timeout, ..self }
}

pub fn set_welcome_settings(&mut self, welcome_settings: WelcomeSettings) -> &mut Self {
self.welcome_settings = Some(welcome_settings);
self
Expand Down Expand Up @@ -664,9 +671,9 @@ impl GuildSettings {

pub fn get_guild_name(&self) -> String {
if self.guild_name.is_empty() {
self.guild_id.to_string().to_ascii_lowercase()
self.guild_id.to_string()
} else {
self.guild_name.to_ascii_lowercase()
self.guild_name.clone()
}
}

Expand Down
9 changes: 9 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ services:
- crack_postgres
ports:
- "127.0.0.1:8000:8000"
pgadmin:
container_name: pgadmin
image: dpage/pgadmin4:latest
environment:
- PGADMIN_DEFAULT_EMAIL=${PGADMIN_MAIL}
- PGADMIN_DEFAULT_PASSWORD=${PGADMIN_PW}
ports:
- "127.0.0.1:5050:80"
restart: always
volumes:
pgdata:
external: true
Expand Down

0 comments on commit 8e39139

Please sign in to comment.