diff options
| author | Max Isom <[email protected]> | 2021-12-18 13:18:57 -0500 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-12-18 13:18:57 -0500 |
| commit | ceb15794ef3ef4c99fd9c2cd1a19c6861c4c72c8 (patch) | |
| tree | 6d18995c8c3492387683399817327f5709cf97ec /src | |
| parent | d4827b86d5c848c68cb9868bfde28a2db932c630 (diff) | |
| parent | f75a76aaa68f647a2540c8074ce7b688bfffeb83 (diff) | |
| download | muse-ceb15794ef3ef4c99fd9c2cd1a19c6861c4c72c8.tar.xz muse-ceb15794ef3ef4c99fd9c2cd1a19c6861c4c72c8.zip | |
Merge pull request #370 from bokherus/playlist-limit-config
Diffstat (limited to 'src')
| -rw-r--r-- | src/commands/config.ts | 16 | ||||
| -rw-r--r-- | src/commands/play.ts | 11 | ||||
| -rw-r--r-- | src/models/settings.ts | 4 | ||||
| -rw-r--r-- | src/services/get-songs.ts | 8 |
4 files changed, 30 insertions, 9 deletions
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<void> { @@ -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<void> { 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))); |
