Skip to content

Commit

Permalink
downvote command
Browse files Browse the repository at this point in the history
  • Loading branch information
cycle-five committed Feb 16, 2024
1 parent 96397a7 commit d2ce627
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 3 deletions.
31 changes: 31 additions & 0 deletions crack-core/src/commands/music/skip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,37 @@ pub async fn create_skip_response(
}
}

/// Downvote and skip song causing it to *not* be used in music recommendations.
#[cfg(not(tarpaulin_include))]
#[poise::command(prefix_command, slash_command, guild_only)]
pub async fn downvote(ctx: Context<'_>) -> Result<(), Error> {
let guild_id = ctx.guild_id().ok_or(CrackedError::GuildOnly)?;
let manager = songbird::get(ctx.serenity_context())
.await
.ok_or(CrackedError::NoSongbird)?;
let call = match manager.get(guild_id) {
Some(call) => call,
None => {
tracing::warn!(
"Not in voice channel: manager.get({}) returned None",
guild_id
);
return Ok(());
}
};

let handler = call.lock().await;
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?;

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

/// Do the actual skipping of the top track.
#[cfg(not(tarpaulin_include))]
pub async fn force_skip_top_track(
Expand Down
41 changes: 41 additions & 0 deletions crack-core/src/db/play_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use poise::futures_util::StreamExt;
use sqlx::types::chrono::NaiveDateTime;
use sqlx::{Error, PgPool};

use crate::db::Metadata;

#[derive(Debug, Clone)]
pub struct PlayLog {
pub id: i64,
Expand Down Expand Up @@ -75,6 +77,7 @@ impl PlayLog {
}
}

///
pub async fn get_last_played_by_guild(
conn: &PgPool,
guild_id: i64,
Expand All @@ -100,6 +103,44 @@ impl PlayLog {
Ok(last_played.into_iter().map(|t| t.to_string()).collect())
}

pub async fn get_last_played_by_guild_metadata(
conn: &PgPool,
guild_id: i64,
) -> Result<Vec<i64>, Error> {
//let last_played: Vec<TitleArtist> = sqlx::query_as!(
// pub id: i32,
// pub track: Option<String>,
// pub artist: Option<String>,
// pub album: Option<String>,
// pub date: Option<chrono::NaiveDate>,
// pub channels: Option<i16>,
// pub channel: Option<String>,
// pub start_time: i64,
// pub duration: i64,
// pub sample_rate: Option<i32>,
// pub source_url: Option<String>,
// pub title: Option<String>,
// pub thumbnail: Option<String>,
let mut last_played: Vec<Metadata> = Vec::new();
let mut last_played_stream = sqlx::query_as!(
Metadata,
r#"
select metadata.id, title, artist, album, track, date, channels, channel, start_time, duration, sample_rate, source_url, thumbnail
from play_log
join metadata on
play_log.metadata_id = metadata.id
where guild_id = $1 order by created_at desc limit 5
"#,
guild_id
)
.fetch(conn);
while let Some(item) = last_played_stream.next().await {
// Process the item
last_played.push(item?);
}
Ok(last_played.into_iter().map(|t| t.id as i64).collect())
}

pub async fn get_last_played_by_user(
conn: &PgPool,
user_id: i64,
Expand Down
2 changes: 1 addition & 1 deletion crack-core/src/db/track_reaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl TrackReaction {
play_log_id = $1
RETURNING
play_log_id, likes, dislikes, skip_votes, created_at"#,
play_log_id,
play_log_id.into(),
)
.fetch_one(pool)
.await
Expand Down
18 changes: 18 additions & 0 deletions crack-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use crate::guild::settings::DEFAULT_PREFIX;
use crate::handlers::event_log::LogEntry;
use chrono::DateTime;
use chrono::Utc;
use db::PlayLog;
use db::TrackReaction;
use errors::CrackedError;
use guild::settings::GuildSettings;
use guild::settings::DEFAULT_DB_URL;
Expand Down Expand Up @@ -382,6 +384,22 @@ impl std::ops::Deref for Data {
}

impl Data {
pub async fn downvote_track(&self, guild_id: GuildId, _track: &str) -> Result<(), Error> {
let play_log_id = PlayLog::get_last_played_by_guild_metadata(
&self.database_pool.as_ref().unwrap(),
guild_id.into(),
)
.await?;
// let mut guild_cache_map = self.guild_cache_map.lock().unwrap();
TrackReaction::add_dislike(
&self.database_pool.as_ref().unwrap(),
*play_log_id.first().unwrap() as i32,
)
.await?;

Ok(())
}

/// Add a message to the cache
pub fn add_msg_to_cache(&self, guild_id: GuildId, msg: Message) -> Option<Message> {
let now = chrono::Utc::now();
Expand Down
2 changes: 1 addition & 1 deletion cracktunes.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
# volume = 1.0
# prefix = "r!"
# owners = [ ]
# database_url = "postgresql://postgres:mysecretpassword@localhost:5433/postgres"
database_url = "postgresql://postgres:mysecretpassword@localhost:5432/postgres"
4 changes: 3 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ services:
# - PORT=${PUB_PORT:-5432}
- PGDATA=/var/lib/postgresql/data
ports:
- "127.0.0.1:${PUB_PORT}:5432"
- "${PUB_PORT}:5432"
expose:
- "${PUB_PORT}"
restart: always
cracktunes:
container_name: cracktunes
Expand Down

0 comments on commit d2ce627

Please sign in to comment.