diff --git a/CHANGELOG.md b/CHANGELOG.md index ffc1533..8035eab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Added +- A custom track limit can now be set when queueing playlists from Spotify (default stays at 50). See #370. ## [0.1.1] ### Fixed diff --git a/src/commands/config.ts b/src/commands/config.ts index 8f3e0aa..dbe0949 100644 --- a/src/commands/config.ts +++ b/src/commands/config.ts @@ -11,6 +11,7 @@ export default class implements Command { public examples = [ ['config prefix !', 'set the prefix to !'], ['config channel music-commands', 'bind the bot to the music-commands channel'], + ['config playlist-limit 30', 'set the playlist song limit to 30'], ]; public async execute(msg: Message, args: string []): Promise { @@ -21,7 +22,8 @@ export default class implements Command { if (settings) { let response = `prefix: \`${settings.prefix}\`\n`; // eslint-disable-next-line @typescript-eslint/no-base-to-string - response += `channel: ${msg.guild!.channels.cache.get(settings.channel)!.toString()}`; + response += `channel: ${msg.guild!.channels.cache.get(settings.channel)!.toString()}\n`; + response += `playlist-limit: ${settings.playlistLimit}`; await msg.channel.send(response); } @@ -74,6 +76,18 @@ export default class implements Command { break; } + case 'playlist-limit': { + const playlistLimit = parseInt(args[1], 10); + if (playlistLimit <= 0) { + await msg.channel.send(errorMsg('please enter a valid number')); + return; + } + + await Settings.update({playlistLimit}, {where: {guildId: msg.guild!.id}}); + await msg.channel.send(`👍 playlist-limit updated to ${playlistLimit}`); + break; + } + default: await msg.channel.send(errorMsg('I\'ve never met this setting in my life')); } diff --git a/src/commands/play.ts b/src/commands/play.ts index 856edec..6561fa9 100644 --- a/src/commands/play.ts +++ b/src/commands/play.ts @@ -10,6 +10,7 @@ import LoadingMessage from '../utils/loading-message.js'; import errorMsg from '../utils/error-msg.js'; import Command from '.'; import GetSongs from '../services/get-songs.js'; +import Settings from '../models/settings.js'; @injectable() export default class implements Command { @@ -40,6 +41,8 @@ export default class implements Command { // eslint-disable-next-line complexity public async execute(msg: Message, args: string[]): Promise { const [targetVoiceChannel] = getMemberVoiceChannel(msg.member!) ?? getMostPopularVoiceChannel(msg.guild!); + const settings = await Settings.findByPk(msg.guild!.id); + const {playlistLimit} = settings!; const res = new LoadingMessage(msg.channel as TextChannel); await res.start(); @@ -101,13 +104,13 @@ export default class implements Command { } } } else if (url.protocol === 'spotify:' || url.host === 'open.spotify.com') { - const [convertedSongs, nSongsNotFound, totalSongs] = await this.getSongs.spotifySource(args[0]); + const [convertedSongs, nSongsNotFound, totalSongs] = await this.getSongs.spotifySource(args[0], playlistLimit); - if (totalSongs > 50) { - extraMsg = 'a random sample of 50 songs was taken'; + if (totalSongs > playlistLimit) { + extraMsg = `a random sample of ${playlistLimit} songs was taken`; } - if (totalSongs > 50 && nSongsNotFound !== 0) { + if (totalSongs > playlistLimit && nSongsNotFound !== 0) { extraMsg += ' and '; } diff --git a/src/models/settings.ts b/src/models/settings.ts index 3318c47..f9756d3 100644 --- a/src/models/settings.ts +++ b/src/models/settings.ts @@ -15,4 +15,8 @@ export default class Settings extends Model { @Default(false) @Column finishedSetup!: boolean; + + @Default(50) + @Column + playlistLimit!: number; } diff --git a/src/services/get-songs.ts b/src/services/get-songs.ts index e7e3d5a..ebf779a 100644 --- a/src/services/get-songs.ts +++ b/src/services/get-songs.ts @@ -188,7 +188,7 @@ export default class { return songsToReturn; } - async spotifySource(url: string): Promise<[QueuedSongWithoutChannel[], number, number]> { + async spotifySource(url: string, playlistLimit: number): Promise<[QueuedSongWithoutChannel[], number, number]> { const parsed = spotifyURI.parse(url); let tracks: SpotifyApi.TrackObjectSimplified[] = []; @@ -252,13 +252,13 @@ export default class { } } - // Get 50 random songs if many + // Get random songs if the playlist is larger than limit const originalNSongs = tracks.length; - if (tracks.length > 50) { + if (tracks.length > playlistLimit) { const shuffled = shuffle(tracks); - tracks = shuffled.slice(0, 50); + tracks = shuffled.slice(0, playlistLimit); } let songs = await Promise.all(tracks.map(async track => this.spotifyToYouTube(track, playlist)));