Merge pull request #370 from bokherus/playlist-limit-config

This commit is contained in:
Max Isom 2021-12-18 13:18:57 -05:00 committed by GitHub
commit ceb15794ef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 32 additions and 9 deletions

View file

@ -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). and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased] ## [Unreleased]
### Added
- A custom track limit can now be set when queueing playlists from Spotify (default stays at 50). See #370.
## [0.1.1] ## [0.1.1]
### Fixed ### Fixed

View file

@ -11,6 +11,7 @@ export default class implements Command {
public examples = [ public examples = [
['config prefix !', 'set the prefix to !'], ['config prefix !', 'set the prefix to !'],
['config channel music-commands', 'bind the bot to the music-commands channel'], ['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> { public async execute(msg: Message, args: string []): Promise<void> {
@ -21,7 +22,8 @@ export default class implements Command {
if (settings) { if (settings) {
let response = `prefix: \`${settings.prefix}\`\n`; let response = `prefix: \`${settings.prefix}\`\n`;
// eslint-disable-next-line @typescript-eslint/no-base-to-string // 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); await msg.channel.send(response);
} }
@ -74,6 +76,18 @@ export default class implements Command {
break; 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: default:
await msg.channel.send(errorMsg('I\'ve never met this setting in my life')); await msg.channel.send(errorMsg('I\'ve never met this setting in my life'));
} }

View file

@ -10,6 +10,7 @@ import LoadingMessage from '../utils/loading-message.js';
import errorMsg from '../utils/error-msg.js'; import errorMsg from '../utils/error-msg.js';
import Command from '.'; import Command from '.';
import GetSongs from '../services/get-songs.js'; import GetSongs from '../services/get-songs.js';
import Settings from '../models/settings.js';
@injectable() @injectable()
export default class implements Command { export default class implements Command {
@ -40,6 +41,8 @@ export default class implements Command {
// eslint-disable-next-line complexity // eslint-disable-next-line complexity
public async execute(msg: Message, args: string[]): Promise<void> { public async execute(msg: Message, args: string[]): Promise<void> {
const [targetVoiceChannel] = getMemberVoiceChannel(msg.member!) ?? getMostPopularVoiceChannel(msg.guild!); 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); const res = new LoadingMessage(msg.channel as TextChannel);
await res.start(); await res.start();
@ -101,13 +104,13 @@ export default class implements Command {
} }
} }
} else if (url.protocol === 'spotify:' || url.host === 'open.spotify.com') { } 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) { if (totalSongs > playlistLimit) {
extraMsg = 'a random sample of 50 songs was taken'; extraMsg = `a random sample of ${playlistLimit} songs was taken`;
} }
if (totalSongs > 50 && nSongsNotFound !== 0) { if (totalSongs > playlistLimit && nSongsNotFound !== 0) {
extraMsg += ' and '; extraMsg += ' and ';
} }

View file

@ -15,4 +15,8 @@ export default class Settings extends Model {
@Default(false) @Default(false)
@Column @Column
finishedSetup!: boolean; finishedSetup!: boolean;
@Default(50)
@Column
playlistLimit!: number;
} }

View file

@ -188,7 +188,7 @@ export default class {
return songsToReturn; 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); const parsed = spotifyURI.parse(url);
let tracks: SpotifyApi.TrackObjectSimplified[] = []; 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; const originalNSongs = tracks.length;
if (tracks.length > 50) { if (tracks.length > playlistLimit) {
const shuffled = shuffle(tracks); 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))); let songs = await Promise.all(tracks.map(async track => this.spotifyToYouTube(track, playlist)));