From 3d7a85843fc4c8028ac87a0e5df98c477f2fffc1 Mon Sep 17 00:00:00 2001 From: JoaoCostaIFG Date: Sat, 6 Jul 2024 01:01:39 +0100 Subject: Feat: allow users to change dotenv file with env var This facilitates the usage of docker secrets for the keys/tokens. --- src/services/config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/services/config.ts b/src/services/config.ts index b6b9aea..019df07 100644 --- a/src/services/config.ts +++ b/src/services/config.ts @@ -5,7 +5,7 @@ import path from 'path'; import xbytes from 'xbytes'; import {ConditionalKeys} from 'type-fest'; import {ActivityType, PresenceStatusData} from 'discord.js'; -dotenv.config(); +dotenv.config({path: process.env.ENV_FILE ?? path.resolve(process.cwd(), '.env')}); export const DATA_DIR = path.resolve(process.env.DATA_DIR ? process.env.DATA_DIR : './data'); -- cgit v1.2.3 From 6e39c8d09ed8f7460f54fb766efddd507f368523 Mon Sep 17 00:00:00 2001 From: Tiago Grosso Date: Fri, 23 Aug 2024 21:18:08 +0100 Subject: feat: add optional pageSize to /queue command --- src/commands/queue.ts | 10 +++++++++- src/utils/build-embed.ts | 10 ++++------ 2 files changed, 13 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/commands/queue.ts b/src/commands/queue.ts index 5196ca9..dcc674a 100644 --- a/src/commands/queue.ts +++ b/src/commands/queue.ts @@ -14,6 +14,10 @@ export default class implements Command { .addIntegerOption(option => option .setName('page') .setDescription('page of queue to show [default: 1]') + .setRequired(false)) + .addIntegerOption(option => option + .setName('pageSize') + .setDescription('how many items to display per page [default: 10]') .setRequired(false)); private readonly playerManager: PlayerManager; @@ -25,7 +29,11 @@ export default class implements Command { public async execute(interaction: ChatInputCommandInteraction) { const player = this.playerManager.get(interaction.guild!.id); - const embed = buildQueueEmbed(player, interaction.options.getInteger('page') ?? 1); + const embed = buildQueueEmbed( + player, + interaction.options.getInteger('page') ?? 1, + interaction.options.getInteger('pageSize') ?? 10, + ); await interaction.reply({embeds: [embed]}); } diff --git a/src/utils/build-embed.ts b/src/utils/build-embed.ts index b8e725c..23db0b9 100644 --- a/src/utils/build-embed.ts +++ b/src/utils/build-embed.ts @@ -5,8 +5,6 @@ import getProgressBar from './get-progress-bar.js'; import {prettyTime} from './time.js'; import {truncate} from './string.js'; -const PAGE_SIZE = 10; - const getMaxSongTitleLength = (title: string) => { // eslint-disable-next-line no-control-regex const nonASCII = /[^\x00-\x7F]+/; @@ -77,7 +75,7 @@ export const buildPlayingMessageEmbed = (player: Player): EmbedBuilder => { return message; }; -export const buildQueueEmbed = (player: Player, page: number): EmbedBuilder => { +export const buildQueueEmbed = (player: Player, page: number, pageSize: number): EmbedBuilder => { const currentlyPlaying = player.getCurrent(); if (!currentlyPlaying) { @@ -85,14 +83,14 @@ export const buildQueueEmbed = (player: Player, page: number): EmbedBuilder => { } const queueSize = player.queueSize(); - const maxQueuePage = Math.ceil((queueSize + 1) / PAGE_SIZE); + const maxQueuePage = Math.ceil((queueSize + 1) / pageSize); if (page > maxQueuePage) { throw new Error('the queue isn\'t that big'); } - const queuePageBegin = (page - 1) * PAGE_SIZE; - const queuePageEnd = queuePageBegin + PAGE_SIZE; + const queuePageBegin = (page - 1) * pageSize; + const queuePageEnd = queuePageBegin + pageSize; const queuedSongs = player .getQueue() .slice(queuePageBegin, queuePageEnd) -- cgit v1.2.3 From c46153f62086a06fe6aedf240bfec9f264295565 Mon Sep 17 00:00:00 2001 From: Tiago Grosso Date: Fri, 23 Aug 2024 21:38:50 +0100 Subject: fix: fix page-size option name --- src/commands/queue.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/commands/queue.ts b/src/commands/queue.ts index dcc674a..d627c7f 100644 --- a/src/commands/queue.ts +++ b/src/commands/queue.ts @@ -16,7 +16,7 @@ export default class implements Command { .setDescription('page of queue to show [default: 1]') .setRequired(false)) .addIntegerOption(option => option - .setName('pageSize') + .setName('page-size') .setDescription('how many items to display per page [default: 10]') .setRequired(false)); @@ -32,7 +32,7 @@ export default class implements Command { const embed = buildQueueEmbed( player, interaction.options.getInteger('page') ?? 1, - interaction.options.getInteger('pageSize') ?? 10, + interaction.options.getInteger('page-size') ?? 10, ); await interaction.reply({embeds: [embed]}); -- cgit v1.2.3 From 0912d957918cb93b0d0607d305fd9d92c1f1cce5 Mon Sep 17 00:00:00 2001 From: Tiago Grosso Date: Sat, 24 Aug 2024 23:08:05 +0100 Subject: feat: add setting for default queue page size --- src/commands/config.ts | 27 +++++++++++++++++++++++++++ src/commands/queue.ts | 13 ++++++++++--- 2 files changed, 37 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/commands/config.ts b/src/commands/config.ts index f866e82..c9aa0e3 100644 --- a/src/commands/config.ts +++ b/src/commands/config.ts @@ -56,6 +56,15 @@ export default class implements Command { .setMinValue(0) .setMaxValue(100) .setRequired(true))) + .addSubcommand(subcommand => subcommand + .setName('set-default-queue-page-size') + .setDescription('set the default page size of the /queue command') + .addIntegerOption(option => option + .setName('page-size') + .setDescription('page size of the /queue command') + .setMinValue(1) + .setMaxValue(50) + .setRequired(true))) .addSubcommand(subcommand => subcommand .setName('get') .setDescription('show all settings')); @@ -171,6 +180,23 @@ export default class implements Command { break; } + case 'set-default-queue-page-size': { + const value = interaction.options.getInteger('page-size')!; + + await prisma.setting.update({ + where: { + guildId: interaction.guild!.id, + }, + data: { + defaultQueuePageSize: value, + }, + }); + + await interaction.reply('👍 default queue page size updated'); + + break; + } + case 'get': { const embed = new EmbedBuilder().setTitle('Config'); @@ -185,6 +211,7 @@ export default class implements Command { 'Auto announce next song in queue': config.autoAnnounceNextSong ? 'yes' : 'no', 'Add to queue reponses show for requester only': config.autoAnnounceNextSong ? 'yes' : 'no', 'Default Volume': config.defaultVolume, + 'Default queue page size': config.defaultQueuePageSize, }; let description = ''; diff --git a/src/commands/queue.ts b/src/commands/queue.ts index d627c7f..60472b0 100644 --- a/src/commands/queue.ts +++ b/src/commands/queue.ts @@ -5,6 +5,7 @@ import {TYPES} from '../types.js'; import PlayerManager from '../managers/player.js'; import Command from './index.js'; import {buildQueueEmbed} from '../utils/build-embed.js'; +import {getGuildSettings} from '../utils/get-guild-settings.js'; @injectable() export default class implements Command { @@ -17,7 +18,9 @@ export default class implements Command { .setRequired(false)) .addIntegerOption(option => option .setName('page-size') - .setDescription('how many items to display per page [default: 10]') + .setDescription('how many items to display per page [default: 10, max: 50]') + .setMinValue(1) + .setMaxValue(50) .setRequired(false)); private readonly playerManager: PlayerManager; @@ -27,12 +30,16 @@ export default class implements Command { } public async execute(interaction: ChatInputCommandInteraction) { - const player = this.playerManager.get(interaction.guild!.id); + const guildId = interaction.guild!.id; + const player = this.playerManager.get(guildId); + + const pageSizeFromOptions = interaction.options.getInteger('page-size'); + const pageSize = pageSizeFromOptions ?? (await getGuildSettings(guildId)).defaultQueuePageSize; const embed = buildQueueEmbed( player, interaction.options.getInteger('page') ?? 1, - interaction.options.getInteger('page-size') ?? 10, + pageSize, ); await interaction.reply({embeds: [embed]}); -- cgit v1.2.3 From 8e7e12c8dfa39d4ce779b8fdcbdfd0cfe7425269 Mon Sep 17 00:00:00 2001 From: Tiago Grosso Date: Tue, 27 Aug 2024 11:11:43 +0100 Subject: fix: limit queue size to 30 --- src/commands/config.ts | 2 +- src/commands/queue.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/commands/config.ts b/src/commands/config.ts index c9aa0e3..91b2578 100644 --- a/src/commands/config.ts +++ b/src/commands/config.ts @@ -63,7 +63,7 @@ export default class implements Command { .setName('page-size') .setDescription('page size of the /queue command') .setMinValue(1) - .setMaxValue(50) + .setMaxValue(30) .setRequired(true))) .addSubcommand(subcommand => subcommand .setName('get') diff --git a/src/commands/queue.ts b/src/commands/queue.ts index 60472b0..fd3674a 100644 --- a/src/commands/queue.ts +++ b/src/commands/queue.ts @@ -20,7 +20,7 @@ export default class implements Command { .setName('page-size') .setDescription('how many items to display per page [default: 10, max: 50]') .setMinValue(1) - .setMaxValue(50) + .setMaxValue(30) .setRequired(false)); private readonly playerManager: PlayerManager; -- cgit v1.2.3 From a87078c2ada84adabc8d9bb5b527feec6490e513 Mon Sep 17 00:00:00 2001 From: Harry Jenkins Date: Wed, 28 Aug 2024 16:41:12 +1000 Subject: update page-size option description to reflect maximum value of 30 --- src/commands/queue.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/commands/queue.ts b/src/commands/queue.ts index fd3674a..fd36e43 100644 --- a/src/commands/queue.ts +++ b/src/commands/queue.ts @@ -18,7 +18,7 @@ export default class implements Command { .setRequired(false)) .addIntegerOption(option => option .setName('page-size') - .setDescription('how many items to display per page [default: 10, max: 50]') + .setDescription('how many items to display per page [default: 10, max: 30]') .setMinValue(1) .setMaxValue(30) .setRequired(false)); -- cgit v1.2.3 From af639159d1647ffb7b4c5c5f7ad035fe8766b6dc Mon Sep 17 00:00:00 2001 From: sofushn Date: Sat, 7 Sep 2024 13:09:45 +0200 Subject: feat: allow running without spotify --- src/commands/play.ts | 8 +- src/inversify.config.ts | 16 ++-- src/services/add-query-to-queue.ts | 70 +------------- src/services/config.ts | 4 +- src/services/get-songs.ts | 102 +++++++++++++++++++-- src/services/youtube-api.ts | 2 +- .../get-youtube-and-spotify-suggestions-for.ts | 84 +++++++++-------- 7 files changed, 160 insertions(+), 126 deletions(-) (limited to 'src') diff --git a/src/commands/play.ts b/src/commands/play.ts index 25aef1c..7851eb7 100644 --- a/src/commands/play.ts +++ b/src/commands/play.ts @@ -1,7 +1,7 @@ import {AutocompleteInteraction, ChatInputCommandInteraction} from 'discord.js'; import {URL} from 'url'; import {SlashCommandBuilder} from '@discordjs/builders'; -import {inject, injectable} from 'inversify'; +import {inject, injectable, optional} from 'inversify'; import Spotify from 'spotify-web-api-node'; import Command from './index.js'; import {TYPES} from '../types.js'; @@ -36,12 +36,12 @@ export default class implements Command { public requiresVC = true; - private readonly spotify: Spotify; + private readonly spotify?: Spotify; private readonly cache: KeyValueCacheProvider; private readonly addQueryToQueue: AddQueryToQueue; - constructor(@inject(TYPES.ThirdParty) thirdParty: ThirdParty, @inject(TYPES.KeyValueCache) cache: KeyValueCacheProvider, @inject(TYPES.Services.AddQueryToQueue) addQueryToQueue: AddQueryToQueue) { - this.spotify = thirdParty.spotify; + constructor(@inject(TYPES.ThirdParty) @optional() thirdParty: ThirdParty, @inject(TYPES.KeyValueCache) cache: KeyValueCacheProvider, @inject(TYPES.Services.AddQueryToQueue) addQueryToQueue: AddQueryToQueue) { + this.spotify = thirdParty?.spotify; this.cache = cache; this.addQueryToQueue = addQueryToQueue; } diff --git a/src/inversify.config.ts b/src/inversify.config.ts index 2f2005e..8e621cb 100644 --- a/src/inversify.config.ts +++ b/src/inversify.config.ts @@ -57,11 +57,20 @@ container.bind(TYPES.Client).toConstantValue(new Client({intents})); // Managers container.bind(TYPES.Managers.Player).to(PlayerManager).inSingletonScope(); +// Config values +container.bind(TYPES.Config).toConstantValue(new ConfigProvider()); + // Services container.bind(TYPES.Services.GetSongs).to(GetSongs).inSingletonScope(); container.bind(TYPES.Services.AddQueryToQueue).to(AddQueryToQueue).inSingletonScope(); container.bind(TYPES.Services.YoutubeAPI).to(YoutubeAPI).inSingletonScope(); -container.bind(TYPES.Services.SpotifyAPI).to(SpotifyAPI).inSingletonScope(); + +// Only instanciate spotify dependencies if the Spotify client ID and secret are set +const config = container.get(TYPES.Config); +if (config.SPOTIFY_CLIENT_ID !== '' && config.SPOTIFY_CLIENT_SECRET !== '') { + container.bind(TYPES.Services.SpotifyAPI).to(SpotifyAPI).inSingletonScope(); + container.bind(TYPES.ThirdParty).to(ThirdParty); +} // Commands [ @@ -91,12 +100,7 @@ container.bind(TYPES.Services.SpotifyAPI).to(SpotifyAPI).inSingleton container.bind(TYPES.Command).to(command).inSingletonScope(); }); -// Config values -container.bind(TYPES.Config).toConstantValue(new ConfigProvider()); - // Static libraries -container.bind(TYPES.ThirdParty).to(ThirdParty); - container.bind(TYPES.FileCache).to(FileCacheProvider); container.bind(TYPES.KeyValueCache).to(KeyValueCacheProvider); diff --git a/src/services/add-query-to-queue.ts b/src/services/add-query-to-queue.ts index 401ad90..95e16ff 100644 --- a/src/services/add-query-to-queue.ts +++ b/src/services/add-query-to-queue.ts @@ -1,6 +1,5 @@ /* eslint-disable complexity */ import {ChatInputCommandInteraction, GuildMember} from 'discord.js'; -import {URL} from 'node:url'; import {inject, injectable} from 'inversify'; import shuffle from 'array-shuffle'; import {TYPES} from '../types.js'; @@ -60,74 +59,7 @@ export default class AddQueryToQueue { await interaction.deferReply({ephemeral: queueAddResponseEphemeral}); - let newSongs: SongMetadata[] = []; - let extraMsg = ''; - - // Test if it's a complete URL - try { - const url = new URL(query); - - const YOUTUBE_HOSTS = [ - 'www.youtube.com', - 'youtu.be', - 'youtube.com', - 'music.youtube.com', - 'www.music.youtube.com', - ]; - - if (YOUTUBE_HOSTS.includes(url.host)) { - // YouTube source - if (url.searchParams.get('list')) { - // YouTube playlist - newSongs.push(...await this.getSongs.youtubePlaylist(url.searchParams.get('list')!, shouldSplitChapters)); - } else { - const songs = await this.getSongs.youtubeVideo(url.href, shouldSplitChapters); - - if (songs) { - newSongs.push(...songs); - } else { - throw new Error('that doesn\'t exist'); - } - } - } else if (url.protocol === 'spotify:' || url.host === 'open.spotify.com') { - const [convertedSongs, nSongsNotFound, totalSongs] = await this.getSongs.spotifySource(query, playlistLimit, shouldSplitChapters); - - if (totalSongs > playlistLimit) { - extraMsg = `a random sample of ${playlistLimit} songs was taken`; - } - - if (totalSongs > playlistLimit && nSongsNotFound !== 0) { - extraMsg += ' and '; - } - - if (nSongsNotFound !== 0) { - if (nSongsNotFound === 1) { - extraMsg += '1 song was not found'; - } else { - extraMsg += `${nSongsNotFound.toString()} songs were not found`; - } - } - - newSongs.push(...convertedSongs); - } else { - const song = await this.getSongs.httpLiveStream(query); - - if (song) { - newSongs.push(song); - } else { - throw new Error('that doesn\'t exist'); - } - } - } catch (_: unknown) { - // Not a URL, must search YouTube - const songs = await this.getSongs.youtubeVideoSearch(query, shouldSplitChapters); - - if (songs) { - newSongs.push(...songs); - } else { - throw new Error('that doesn\'t exist'); - } - } + let [newSongs, extraMsg] = await this.getSongs.getSongs(query, playlistLimit, shouldSplitChapters); if (newSongs.length === 0) { throw new Error('no songs found'); diff --git a/src/services/config.ts b/src/services/config.ts index b6b9aea..a5af5a4 100644 --- a/src/services/config.ts +++ b/src/services/config.ts @@ -12,8 +12,8 @@ export const DATA_DIR = path.resolve(process.env.DATA_DIR ? process.env.DATA_DIR const CONFIG_MAP = { DISCORD_TOKEN: process.env.DISCORD_TOKEN, YOUTUBE_API_KEY: process.env.YOUTUBE_API_KEY, - SPOTIFY_CLIENT_ID: process.env.SPOTIFY_CLIENT_ID, - SPOTIFY_CLIENT_SECRET: process.env.SPOTIFY_CLIENT_SECRET, + SPOTIFY_CLIENT_ID: process.env.SPOTIFY_CLIENT_ID ?? '', + SPOTIFY_CLIENT_SECRET: process.env.SPOTIFY_CLIENT_SECRET ?? '', REGISTER_COMMANDS_ON_BOT: process.env.REGISTER_COMMANDS_ON_BOT === 'true', DATA_DIR, CACHE_DIR: path.join(DATA_DIR, 'cache'), diff --git a/src/services/get-songs.ts b/src/services/get-songs.ts index b957734..dcaec0c 100644 --- a/src/services/get-songs.ts +++ b/src/services/get-songs.ts @@ -1,34 +1,120 @@ -import {inject, injectable} from 'inversify'; +import {inject, injectable, optional} from 'inversify'; import * as spotifyURI from 'spotify-uri'; import {SongMetadata, QueuedPlaylist, MediaSource} from './player.js'; import {TYPES} from '../types.js'; import ffmpeg from 'fluent-ffmpeg'; import YoutubeAPI from './youtube-api.js'; import SpotifyAPI, {SpotifyTrack} from './spotify-api.js'; +import {URL} from 'node:url'; @injectable() export default class { private readonly youtubeAPI: YoutubeAPI; - private readonly spotifyAPI: SpotifyAPI; + private readonly spotifyAPI?: SpotifyAPI; - constructor(@inject(TYPES.Services.YoutubeAPI) youtubeAPI: YoutubeAPI, @inject(TYPES.Services.SpotifyAPI) spotifyAPI: SpotifyAPI) { + constructor(@inject(TYPES.Services.YoutubeAPI) youtubeAPI: YoutubeAPI, @inject(TYPES.Services.SpotifyAPI) @optional() spotifyAPI?: SpotifyAPI) { this.youtubeAPI = youtubeAPI; this.spotifyAPI = spotifyAPI; } - async youtubeVideoSearch(query: string, shouldSplitChapters: boolean): Promise { + async getSongs(query: string, playlistLimit: number, shouldSplitChapters: boolean): Promise<[SongMetadata[], string]> { + let newSongs: SongMetadata[] = []; + let extraMsg = ''; + + // Test if it's a complete URL + try { + const url = new URL(query); + + const YOUTUBE_HOSTS = [ + 'www.youtube.com', + 'youtu.be', + 'youtube.com', + 'music.youtube.com', + 'www.music.youtube.com', + ]; + + if (YOUTUBE_HOSTS.includes(url.host)) { + // YouTube source + if (url.searchParams.get('list')) { + // YouTube playlist + newSongs.push(...await this.youtubePlaylist(url.searchParams.get('list')!, shouldSplitChapters)); + } else { + const songs = await this.youtubeVideo(url.href, shouldSplitChapters); + + if (songs) { + newSongs.push(...songs); + } else { + throw new Error('that doesn\'t exist'); + } + } + } else if (url.protocol === 'spotify:' || url.host === 'open.spotify.com') { + if (this.spotifyAPI === undefined) { + throw new Error('Spotify is not enabled!') + } + + const [convertedSongs, nSongsNotFound, totalSongs] = await this.spotifySource(query, playlistLimit, shouldSplitChapters); + + if (totalSongs > playlistLimit) { + extraMsg = `a random sample of ${playlistLimit} songs was taken`; + } + + if (totalSongs > playlistLimit && nSongsNotFound !== 0) { + extraMsg += ' and '; + } + + if (nSongsNotFound !== 0) { + if (nSongsNotFound === 1) { + extraMsg += '1 song was not found'; + } else { + extraMsg += `${nSongsNotFound.toString()} songs were not found`; + } + } + + newSongs.push(...convertedSongs); + } else { + const song = await this.httpLiveStream(query); + + if (song) { + newSongs.push(song); + } else { + throw new Error('that doesn\'t exist'); + } + } + } catch (err: any) { + if (err = 'spotify not enabled') { + throw err; + } + + // Not a URL, must search YouTube + const songs = await this.youtubeVideoSearch(query, shouldSplitChapters); + + if (songs) { + newSongs.push(...songs); + } else { + throw new Error('that doesn\'t exist'); + } + } + + return [newSongs, extraMsg]; + } + + private async youtubeVideoSearch(query: string, shouldSplitChapters: boolean): Promise { return this.youtubeAPI.search(query, shouldSplitChapters); } - async youtubeVideo(url: string, shouldSplitChapters: boolean): Promise { + private async youtubeVideo(url: string, shouldSplitChapters: boolean): Promise { return this.youtubeAPI.getVideo(url, shouldSplitChapters); } - async youtubePlaylist(listId: string, shouldSplitChapters: boolean): Promise { + private async youtubePlaylist(listId: string, shouldSplitChapters: boolean): Promise { return this.youtubeAPI.getPlaylist(listId, shouldSplitChapters); } - async spotifySource(url: string, playlistLimit: number, shouldSplitChapters: boolean): Promise<[SongMetadata[], number, number]> { + private async spotifySource(url: string, playlistLimit: number, shouldSplitChapters: boolean): Promise<[SongMetadata[], number, number]> { + if (this.spotifyAPI === undefined) { + return [[], 0, 0]; + } + const parsed = spotifyURI.parse(url); switch (parsed.type) { @@ -58,7 +144,7 @@ export default class { } } - async httpLiveStream(url: string): Promise { + private async httpLiveStream(url: string): Promise { return new Promise((resolve, reject) => { ffmpeg(url).ffprobe((err, _) => { if (err) { diff --git a/src/services/youtube-api.ts b/src/services/youtube-api.ts index 143033a..216a2c0 100644 --- a/src/services/youtube-api.ts +++ b/src/services/youtube-api.ts @@ -95,7 +95,7 @@ export default class { } if (!firstVideo) { - throw new Error('No video found.'); + return []; } return this.getVideo(firstVideo.url, shouldSplitChapters); diff --git a/src/utils/get-youtube-and-spotify-suggestions-for.ts b/src/utils/get-youtube-and-spotify-suggestions-for.ts index 6594b52..c33cbee 100644 --- a/src/utils/get-youtube-and-spotify-suggestions-for.ts +++ b/src/utils/get-youtube-and-spotify-suggestions-for.ts @@ -14,28 +14,19 @@ const filterDuplicates = (items: T[]) => { return results; }; -const getYouTubeAndSpotifySuggestionsFor = async (query: string, spotify: SpotifyWebApi, limit = 10): Promise => { - const [youtubeSuggestions, spotifyResults] = await Promise.all([ - getYouTubeSuggestionsFor(query), - spotify.search(query, ['track', 'album'], {limit: 5}), - ]); +const getYouTubeAndSpotifySuggestionsFor = async (query: string, spotify?: SpotifyWebApi, limit = 10): Promise => { + + // Only search Spotify if enabled + let spotifySuggestionPromise = spotify !== undefined + ? spotify.search(query, ['album', 'track'], {limit}) + : undefined; + + const youtubeSuggestions = await getYouTubeSuggestionsFor(query); const totalYouTubeResults = youtubeSuggestions.length; + const numOfYouTubeSuggestions = Math.min(limit, totalYouTubeResults); - const spotifyAlbums = filterDuplicates(spotifyResults.body.albums?.items ?? []); - const spotifyTracks = filterDuplicates(spotifyResults.body.tracks?.items ?? []); - - const totalSpotifyResults = spotifyAlbums.length + spotifyTracks.length; - - // Number of results for each source should be roughly the same. - // If we don't have enough Spotify suggestions, prioritize YouTube results. - const maxSpotifySuggestions = Math.floor(limit / 2); - const numOfSpotifySuggestions = Math.min(maxSpotifySuggestions, totalSpotifyResults); - - const maxYouTubeSuggestions = limit - numOfSpotifySuggestions; - const numOfYouTubeSuggestions = Math.min(maxYouTubeSuggestions, totalYouTubeResults); - - const suggestions: APIApplicationCommandOptionChoice[] = []; + let suggestions: APIApplicationCommandOptionChoice[] = []; suggestions.push( ...youtubeSuggestions @@ -45,24 +36,45 @@ const getYouTubeAndSpotifySuggestionsFor = async (query: string, spotify: Spotif value: suggestion, }), )); + + + if (spotify !== undefined && spotifySuggestionPromise !== undefined) { + + const spotifyResponse = (await spotifySuggestionPromise).body + const spotifyAlbums = filterDuplicates(spotifyResponse.albums?.items ?? []); + const spotifyTracks = filterDuplicates(spotifyResponse.tracks?.items ?? []); + + const totalSpotifyResults = spotifyAlbums.length + spotifyTracks.length; + + // Number of results for each source should be roughly the same. + // If we don't have enough Spotify suggestions, prioritize YouTube results. + const maxSpotifySuggestions = Math.floor(limit / 2); + const numOfSpotifySuggestions = Math.min(maxSpotifySuggestions, totalSpotifyResults); + + + const maxSpotifyAlbums = Math.floor(numOfSpotifySuggestions / 2); + const numOfSpotifyAlbums = Math.min(maxSpotifyAlbums, spotifyResponse.albums?.items.length ?? 0); + const maxSpotifyTracks = numOfSpotifySuggestions - numOfSpotifyAlbums; + + // make room for spotify results + const maxYouTubeSuggestions = limit - numOfSpotifySuggestions; + suggestions = suggestions.slice(0, maxYouTubeSuggestions) + + suggestions.push( + ...spotifyAlbums.slice(0, maxSpotifyAlbums).map(album => ({ + name: `Spotify: 💿 ${album.name}${album.artists.length > 0 ? ` - ${album.artists[0].name}` : ''}`, + value: `spotify:album:${album.id}`, + })), + ); + + suggestions.push( + ...spotifyTracks.slice(0, maxSpotifyTracks).map(track => ({ + name: `Spotify: 🎵 ${track.name}${track.artists.length > 0 ? ` - ${track.artists[0].name}` : ''}`, + value: `spotify:track:${track.id}`, + })), + ); - const maxSpotifyAlbums = Math.floor(numOfSpotifySuggestions / 2); - const numOfSpotifyAlbums = Math.min(maxSpotifyAlbums, spotifyResults.body.albums?.items.length ?? 0); - const maxSpotifyTracks = numOfSpotifySuggestions - numOfSpotifyAlbums; - - suggestions.push( - ...spotifyAlbums.slice(0, maxSpotifyAlbums).map(album => ({ - name: `Spotify: 💿 ${album.name}${album.artists.length > 0 ? ` - ${album.artists[0].name}` : ''}`, - value: `spotify:album:${album.id}`, - })), - ); - - suggestions.push( - ...spotifyTracks.slice(0, maxSpotifyTracks).map(track => ({ - name: `Spotify: 🎵 ${track.name}${track.artists.length > 0 ? ` - ${track.artists[0].name}` : ''}`, - value: `spotify:track:${track.id}`, - })), - ); + } return suggestions; }; -- cgit v1.2.3 From 107464e22244e5f488212269f780675bc1494190 Mon Sep 17 00:00:00 2001 From: sofushn Date: Sat, 7 Sep 2024 21:49:10 +0200 Subject: chore: make linter parse --- src/services/get-songs.ts | 8 ++++---- .../get-youtube-and-spotify-suggestions-for.ts | 23 +++++++++------------- 2 files changed, 13 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/services/get-songs.ts b/src/services/get-songs.ts index dcaec0c..c48d87d 100644 --- a/src/services/get-songs.ts +++ b/src/services/get-songs.ts @@ -18,7 +18,7 @@ export default class { } async getSongs(query: string, playlistLimit: number, shouldSplitChapters: boolean): Promise<[SongMetadata[], string]> { - let newSongs: SongMetadata[] = []; + const newSongs: SongMetadata[] = []; let extraMsg = ''; // Test if it's a complete URL @@ -49,7 +49,7 @@ export default class { } } else if (url.protocol === 'spotify:' || url.host === 'open.spotify.com') { if (this.spotifyAPI === undefined) { - throw new Error('Spotify is not enabled!') + throw new Error('Spotify is not enabled!'); } const [convertedSongs, nSongsNotFound, totalSongs] = await this.spotifySource(query, playlistLimit, shouldSplitChapters); @@ -81,7 +81,7 @@ export default class { } } } catch (err: any) { - if (err = 'spotify not enabled') { + if (err instanceof Error && err.message === 'Spotify is not enabled!') { throw err; } @@ -112,7 +112,7 @@ export default class { private async spotifySource(url: string, playlistLimit: number, shouldSplitChapters: boolean): Promise<[SongMetadata[], number, number]> { if (this.spotifyAPI === undefined) { - return [[], 0, 0]; + return [[], 0, 0]; } const parsed = spotifyURI.parse(url); diff --git a/src/utils/get-youtube-and-spotify-suggestions-for.ts b/src/utils/get-youtube-and-spotify-suggestions-for.ts index c33cbee..bd6f1c8 100644 --- a/src/utils/get-youtube-and-spotify-suggestions-for.ts +++ b/src/utils/get-youtube-and-spotify-suggestions-for.ts @@ -15,12 +15,11 @@ const filterDuplicates = (items: T[]) => { }; const getYouTubeAndSpotifySuggestionsFor = async (query: string, spotify?: SpotifyWebApi, limit = 10): Promise => { - // Only search Spotify if enabled - let spotifySuggestionPromise = spotify !== undefined - ? spotify.search(query, ['album', 'track'], {limit}) - : undefined; - + const spotifySuggestionPromise = spotify === undefined + ? undefined + : spotify.search(query, ['album', 'track'], {limit}); + const youtubeSuggestions = await getYouTubeSuggestionsFor(query); const totalYouTubeResults = youtubeSuggestions.length; @@ -36,11 +35,9 @@ const getYouTubeAndSpotifySuggestionsFor = async (query: string, spotify?: Spoti value: suggestion, }), )); - - + if (spotify !== undefined && spotifySuggestionPromise !== undefined) { - - const spotifyResponse = (await spotifySuggestionPromise).body + const spotifyResponse = (await spotifySuggestionPromise).body; const spotifyAlbums = filterDuplicates(spotifyResponse.albums?.items ?? []); const spotifyTracks = filterDuplicates(spotifyResponse.tracks?.items ?? []); @@ -51,14 +48,13 @@ const getYouTubeAndSpotifySuggestionsFor = async (query: string, spotify?: Spoti const maxSpotifySuggestions = Math.floor(limit / 2); const numOfSpotifySuggestions = Math.min(maxSpotifySuggestions, totalSpotifyResults); - const maxSpotifyAlbums = Math.floor(numOfSpotifySuggestions / 2); const numOfSpotifyAlbums = Math.min(maxSpotifyAlbums, spotifyResponse.albums?.items.length ?? 0); const maxSpotifyTracks = numOfSpotifySuggestions - numOfSpotifyAlbums; - // make room for spotify results + // Make room for spotify results const maxYouTubeSuggestions = limit - numOfSpotifySuggestions; - suggestions = suggestions.slice(0, maxYouTubeSuggestions) + suggestions = suggestions.slice(0, maxYouTubeSuggestions); suggestions.push( ...spotifyAlbums.slice(0, maxSpotifyAlbums).map(album => ({ @@ -66,14 +62,13 @@ const getYouTubeAndSpotifySuggestionsFor = async (query: string, spotify?: Spoti value: `spotify:album:${album.id}`, })), ); - + suggestions.push( ...spotifyTracks.slice(0, maxSpotifyTracks).map(track => ({ name: `Spotify: 🎵 ${track.name}${track.artists.length > 0 ? ` - ${track.artists[0].name}` : ''}`, value: `spotify:track:${track.id}`, })), ); - } return suggestions; -- cgit v1.2.3 From aae98255b19e83ce78aaea389627dbae504910f8 Mon Sep 17 00:00:00 2001 From: Yitong Xiao Date: Sun, 15 Sep 2024 17:53:05 -0400 Subject: feat: automatically turn down volume when people talks --- src/managers/player.ts | 7 ++++-- src/services/config.ts | 4 +++ src/services/player.ts | 67 +++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 75 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/managers/player.ts b/src/managers/player.ts index 420cf48..577d9f3 100644 --- a/src/managers/player.ts +++ b/src/managers/player.ts @@ -2,22 +2,25 @@ import {inject, injectable} from 'inversify'; import {TYPES} from '../types.js'; import Player from '../services/player.js'; import FileCacheProvider from '../services/file-cache.js'; +import Config from '../services/config.js'; @injectable() export default class { private readonly guildPlayers: Map; private readonly fileCache: FileCacheProvider; + private readonly config: Config; - constructor(@inject(TYPES.FileCache) fileCache: FileCacheProvider) { + constructor(@inject(TYPES.FileCache) fileCache: FileCacheProvider, @inject(TYPES.Config) config: Config) { this.guildPlayers = new Map(); this.fileCache = fileCache; + this.config = config; } get(guildId: string): Player { let player = this.guildPlayers.get(guildId); if (!player) { - player = new Player(this.fileCache, guildId); + player = new Player(this.fileCache, guildId, this.config); this.guildPlayers.set(guildId, player); } diff --git a/src/services/config.ts b/src/services/config.ts index b6b9aea..ef2a7c9 100644 --- a/src/services/config.ts +++ b/src/services/config.ts @@ -14,6 +14,8 @@ const CONFIG_MAP = { YOUTUBE_API_KEY: process.env.YOUTUBE_API_KEY, SPOTIFY_CLIENT_ID: process.env.SPOTIFY_CLIENT_ID, SPOTIFY_CLIENT_SECRET: process.env.SPOTIFY_CLIENT_SECRET, + TURN_DOWN_VOLUME_WHEN_PEOPLE_SPEAK: process.env.TURN_DOWN_VOLUME_WHEN_PEOPLE_SPEAK === 'true', + TURN_DOWN_VOLUME_WHEN_PEOPLE_SPEAK_TARGET: process.env.TURN_DOWN_VOLUME_WHEN_PEOPLE_SPEAK_TARGET ?? 20, REGISTER_COMMANDS_ON_BOT: process.env.REGISTER_COMMANDS_ON_BOT === 'true', DATA_DIR, CACHE_DIR: path.join(DATA_DIR, 'cache'), @@ -43,6 +45,8 @@ export default class Config { readonly DATA_DIR!: string; readonly CACHE_DIR!: string; readonly CACHE_LIMIT_IN_BYTES!: number; + readonly TURN_DOWN_VOLUME_WHEN_PEOPLE_SPEAK!: boolean; + readonly TURN_DOWN_VOLUME_WHEN_PEOPLE_SPEAK_TARGET!: number; readonly BOT_STATUS!: PresenceStatusData; readonly BOT_ACTIVITY_TYPE!: Exclude; readonly BOT_ACTIVITY_URL!: string; diff --git a/src/services/player.ts b/src/services/player.ts index 5e284a6..00081ca 100644 --- a/src/services/player.ts +++ b/src/services/player.ts @@ -20,6 +20,7 @@ import FileCacheProvider from './file-cache.js'; import debug from '../utils/debug.js'; import {getGuildSettings} from '../utils/get-guild-settings.js'; import {buildPlayingMessageEmbed} from '../utils/build-embed.js'; +import Config from './config.js'; export enum MediaSource { Youtube, @@ -82,9 +83,13 @@ export default class { private readonly fileCache: FileCacheProvider; private disconnectTimer: NodeJS.Timeout | null = null; - constructor(fileCache: FileCacheProvider, guildId: string) { + private readonly channelToSpeakingUsers: Map> = new Map(); + private readonly config: Config; + + constructor(fileCache: FileCacheProvider, guildId: string, config: Config) { this.fileCache = fileCache; this.guildId = guildId; + this.config = config; } async connect(channel: VoiceChannel): Promise { @@ -96,6 +101,7 @@ export default class { this.voiceConnection = joinVoiceChannel({ channelId: channel.id, guildId: channel.guild.id, + selfDeaf: false, adapterCreator: channel.guild.voiceAdapterCreator as DiscordGatewayAdapterCreator, }); @@ -115,6 +121,9 @@ export default class { /* eslint-enable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call */ this.currentChannel = channel; + if (newState.status === VoiceConnectionStatus.Ready) { + this.registerVoiceActivityListener(); + } }); } @@ -302,6 +311,62 @@ export default class { } } + registerVoiceActivityListener(): void { + if (!this.config.TURN_DOWN_VOLUME_WHEN_PEOPLE_SPEAK || !this.voiceConnection) { + return; + } + + this.voiceConnection.receiver.speaking.on('start', (userId: string) => { + if (!this.currentChannel) { + return; + } + + const member = this.currentChannel.members.get(userId); + const channelId = this.currentChannel?.id; + + if (member) { + if (!this.channelToSpeakingUsers.has(channelId)) { + this.channelToSpeakingUsers.set(channelId, new Set()); + } + + this.channelToSpeakingUsers.get(channelId)?.add(member.id); + } + + this.suppressVoiceWhenPeopleAreSpeaking(); + }); + + this.voiceConnection.receiver.speaking.on('end', (userId: string) => { + if (!this.currentChannel) { + return; + } + + const member = this.currentChannel.members.get(userId); + const channelId = this.currentChannel.id; + if (member) { + if (!this.channelToSpeakingUsers.has(channelId)) { + this.channelToSpeakingUsers.set(channelId, new Set()); + } + + this.channelToSpeakingUsers.get(channelId)?.delete(member.id); + } + + this.suppressVoiceWhenPeopleAreSpeaking(); + }); + } + + suppressVoiceWhenPeopleAreSpeaking(): void { + if (!this.currentChannel) { + return; + } + + const speakingUsers = this.channelToSpeakingUsers.get(this.currentChannel.id); + if (speakingUsers && speakingUsers.size > 0) { + this.setVolume(this.config.TURN_DOWN_VOLUME_WHEN_PEOPLE_SPEAK_TARGET); + } else { + this.setVolume(this.defaultVolume); + } + } + canGoForward(skip: number) { return (this.queuePosition + skip - 1) < this.queue.length; } -- cgit v1.2.3 From 66e022489ff8caaf1d9dcdbd34c93fe702dfa024 Mon Sep 17 00:00:00 2001 From: sofushn Date: Mon, 28 Oct 2024 14:57:08 +0100 Subject: fix: Spotify URL is part of query description even when disabled --- src/commands/play.ts | 48 +++++++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/commands/play.ts b/src/commands/play.ts index 7851eb7..c87ccd5 100644 --- a/src/commands/play.ts +++ b/src/commands/play.ts @@ -1,6 +1,6 @@ import {AutocompleteInteraction, ChatInputCommandInteraction} from 'discord.js'; import {URL} from 'url'; -import {SlashCommandBuilder} from '@discordjs/builders'; +import {SlashCommandBuilder, SlashCommandSubcommandsOnlyBuilder} from '@discordjs/builders'; import {inject, injectable, optional} from 'inversify'; import Spotify from 'spotify-web-api-node'; import Command from './index.js'; @@ -13,26 +13,7 @@ import AddQueryToQueue from '../services/add-query-to-queue.js'; @injectable() export default class implements Command { - public readonly slashCommand = new SlashCommandBuilder() - .setName('play') - .setDescription('play a song') - .addStringOption(option => option - .setName('query') - .setDescription('YouTube URL, Spotify URL, or search query') - .setAutocomplete(true) - .setRequired(true)) - .addBooleanOption(option => option - .setName('immediate') - .setDescription('add track to the front of the queue')) - .addBooleanOption(option => option - .setName('shuffle') - .setDescription('shuffle the input if you\'re adding multiple tracks')) - .addBooleanOption(option => option - .setName('split') - .setDescription('if a track has chapters, split it')) - .addBooleanOption(option => option - .setName('skip') - .setDescription('skip the currently playing track')); + public readonly slashCommand: Partial & Pick; public requiresVC = true; @@ -44,6 +25,31 @@ export default class implements Command { this.spotify = thirdParty?.spotify; this.cache = cache; this.addQueryToQueue = addQueryToQueue; + + const queryDescription = thirdParty === undefined + ? 'YouTube URL or search query' + : 'YouTube URL, Spotify URL, or search query'; + + this.slashCommand = new SlashCommandBuilder() + .setName('play') + .setDescription('play a song') + .addStringOption(option => option + .setName('query') + .setDescription(queryDescription) + .setAutocomplete(true) + .setRequired(true)) + .addBooleanOption(option => option + .setName('immediate') + .setDescription('add track to the front of the queue')) + .addBooleanOption(option => option + .setName('shuffle') + .setDescription('shuffle the input if you\'re adding multiple tracks')) + .addBooleanOption(option => option + .setName('split') + .setDescription('if a track has chapters, split it')) + .addBooleanOption(option => option + .setName('skip') + .setDescription('skip the currently playing track')); } public async execute(interaction: ChatInputCommandInteraction): Promise { -- cgit v1.2.3 From b42f27eba9db787c1986e9d2146f742157905e91 Mon Sep 17 00:00:00 2001 From: Hazzajenko Date: Fri, 1 Nov 2024 17:36:01 +1100 Subject: Added commands to set whether to reduce the volume when people speak and to set the target volume. --- src/commands/config.ts | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'src') diff --git a/src/commands/config.ts b/src/commands/config.ts index 91b2578..01d9fe9 100644 --- a/src/commands/config.ts +++ b/src/commands/config.ts @@ -40,6 +40,22 @@ export default class implements Command { .setName('value') .setDescription('whether bot responses to queue additions are only displayed to the requester') .setRequired(true))) + .addSubcommand(subcommand => subcommand + .setName('set-reduce-vol-when-voice') + .setDescription('set whether to turn down the volume when people speak') + .addBooleanOption(option => option + .setName('value') + .setDescription('whether to turn down the volume when people speak') + .setRequired(true))) + .addSubcommand(subcommand => subcommand + .setName('set-reduce-vol-when-voice-target') + .setDescription('set the target volume when people speak') + .addIntegerOption(option => option + .setName('volume') + .setDescription('volume percentage (0 is muted, 100 is max & default)') + .setMinValue(0) + .setMaxValue(100) + .setRequired(true))) .addSubcommand(subcommand => subcommand .setName('set-auto-announce-next-song') .setDescription('set whether to announce the next song in the queue automatically') @@ -197,6 +213,40 @@ export default class implements Command { break; } + case 'set-reduce-vol-when-voice': { + const value = interaction.options.getBoolean('value')!; + + await prisma.setting.update({ + where: { + guildId: interaction.guild!.id, + }, + data: { + turnDownVolumeWhenPeopleSpeak: value, + }, + }); + + await interaction.reply('👍 turn down volume setting updated'); + + break; + } + + case 'set-reduce-vol-when-voice-target': { + const value = interaction.options.getInteger('volume')!; + + await prisma.setting.update({ + where: { + guildId: interaction.guild!.id, + }, + data: { + turnDownVolumeWhenPeopleSpeakTarget: value, + }, + }); + + await interaction.reply('👍 turn down volume target setting updated'); + + break; + } + case 'get': { const embed = new EmbedBuilder().setTitle('Config'); @@ -212,6 +262,7 @@ export default class implements Command { 'Add to queue reponses show for requester only': config.autoAnnounceNextSong ? 'yes' : 'no', 'Default Volume': config.defaultVolume, 'Default queue page size': config.defaultQueuePageSize, + 'Reduce volume when people speak': config.turnDownVolumeWhenPeopleSpeak ? 'yes' : 'no', }; let description = ''; -- cgit v1.2.3 From 53af0074fccad9ddb888e121ecebc497f20aab7f Mon Sep 17 00:00:00 2001 From: Hazzajenko Date: Fri, 1 Nov 2024 17:38:23 +1100 Subject: Update dotenv line to latest version of muse. Removed obsolete TURN_DOWN_VOLUME_WHEN_PEOPLE_SPEAK and TURN_DOWN_VOLUME_WHEN_PEOPLE_SPEAK_TARGET environment variables and corresponding class properties as we will use the discord commands instead. --- src/services/config.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'src') diff --git a/src/services/config.ts b/src/services/config.ts index ef2a7c9..019df07 100644 --- a/src/services/config.ts +++ b/src/services/config.ts @@ -5,7 +5,7 @@ import path from 'path'; import xbytes from 'xbytes'; import {ConditionalKeys} from 'type-fest'; import {ActivityType, PresenceStatusData} from 'discord.js'; -dotenv.config(); +dotenv.config({path: process.env.ENV_FILE ?? path.resolve(process.cwd(), '.env')}); export const DATA_DIR = path.resolve(process.env.DATA_DIR ? process.env.DATA_DIR : './data'); @@ -14,8 +14,6 @@ const CONFIG_MAP = { YOUTUBE_API_KEY: process.env.YOUTUBE_API_KEY, SPOTIFY_CLIENT_ID: process.env.SPOTIFY_CLIENT_ID, SPOTIFY_CLIENT_SECRET: process.env.SPOTIFY_CLIENT_SECRET, - TURN_DOWN_VOLUME_WHEN_PEOPLE_SPEAK: process.env.TURN_DOWN_VOLUME_WHEN_PEOPLE_SPEAK === 'true', - TURN_DOWN_VOLUME_WHEN_PEOPLE_SPEAK_TARGET: process.env.TURN_DOWN_VOLUME_WHEN_PEOPLE_SPEAK_TARGET ?? 20, REGISTER_COMMANDS_ON_BOT: process.env.REGISTER_COMMANDS_ON_BOT === 'true', DATA_DIR, CACHE_DIR: path.join(DATA_DIR, 'cache'), @@ -45,8 +43,6 @@ export default class Config { readonly DATA_DIR!: string; readonly CACHE_DIR!: string; readonly CACHE_LIMIT_IN_BYTES!: number; - readonly TURN_DOWN_VOLUME_WHEN_PEOPLE_SPEAK!: boolean; - readonly TURN_DOWN_VOLUME_WHEN_PEOPLE_SPEAK_TARGET!: number; readonly BOT_STATUS!: PresenceStatusData; readonly BOT_ACTIVITY_TYPE!: Exclude; readonly BOT_ACTIVITY_URL!: string; -- cgit v1.2.3 From ba3f1d60c3c1edcfc057005ea617a22519d75304 Mon Sep 17 00:00:00 2001 From: Hazzajenko Date: Fri, 1 Nov 2024 17:42:09 +1100 Subject: Remove Config injection from PlayerManager as we are getting the values from prisma. --- src/managers/player.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/managers/player.ts b/src/managers/player.ts index 577d9f3..420cf48 100644 --- a/src/managers/player.ts +++ b/src/managers/player.ts @@ -2,25 +2,22 @@ import {inject, injectable} from 'inversify'; import {TYPES} from '../types.js'; import Player from '../services/player.js'; import FileCacheProvider from '../services/file-cache.js'; -import Config from '../services/config.js'; @injectable() export default class { private readonly guildPlayers: Map; private readonly fileCache: FileCacheProvider; - private readonly config: Config; - constructor(@inject(TYPES.FileCache) fileCache: FileCacheProvider, @inject(TYPES.Config) config: Config) { + constructor(@inject(TYPES.FileCache) fileCache: FileCacheProvider) { this.guildPlayers = new Map(); this.fileCache = fileCache; - this.config = config; } get(guildId: string): Player { let player = this.guildPlayers.get(guildId); if (!player) { - player = new Player(this.fileCache, guildId, this.config); + player = new Player(this.fileCache, guildId); this.guildPlayers.set(guildId, player); } -- cgit v1.2.3 From 825c9a0c530d39b2b1c2f2f02904a866b7398001 Mon Sep 17 00:00:00 2001 From: Hazzajenko Date: Fri, 1 Nov 2024 17:43:39 +1100 Subject: Refactor player service to use getGuildSettings instead of passing in the config. --- src/services/player.ts | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/services/player.ts b/src/services/player.ts index 00081ca..b833022 100644 --- a/src/services/player.ts +++ b/src/services/player.ts @@ -20,7 +20,7 @@ import FileCacheProvider from './file-cache.js'; import debug from '../utils/debug.js'; import {getGuildSettings} from '../utils/get-guild-settings.js'; import {buildPlayingMessageEmbed} from '../utils/build-embed.js'; -import Config from './config.js'; +import {Setting} from '@prisma/client'; export enum MediaSource { Youtube, @@ -84,12 +84,10 @@ export default class { private disconnectTimer: NodeJS.Timeout | null = null; private readonly channelToSpeakingUsers: Map> = new Map(); - private readonly config: Config; - constructor(fileCache: FileCacheProvider, guildId: string, config: Config) { + constructor(fileCache: FileCacheProvider, guildId: string) { this.fileCache = fileCache; this.guildId = guildId; - this.config = config; } async connect(channel: VoiceChannel): Promise { @@ -105,6 +103,8 @@ export default class { adapterCreator: channel.guild.voiceAdapterCreator as DiscordGatewayAdapterCreator, }); + const guildSettings = await getGuildSettings(this.guildId); + // Workaround to disable keepAlive this.voiceConnection.on('stateChange', (oldState, newState) => { /* eslint-disable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call */ @@ -122,7 +122,7 @@ export default class { this.currentChannel = channel; if (newState.status === VoiceConnectionStatus.Ready) { - this.registerVoiceActivityListener(); + this.registerVoiceActivityListener(guildSettings); } }); } @@ -311,8 +311,9 @@ export default class { } } - registerVoiceActivityListener(): void { - if (!this.config.TURN_DOWN_VOLUME_WHEN_PEOPLE_SPEAK || !this.voiceConnection) { + registerVoiceActivityListener(guildSettings: Setting) { + const {turnDownVolumeWhenPeopleSpeak, turnDownVolumeWhenPeopleSpeakTarget} = guildSettings; + if (!turnDownVolumeWhenPeopleSpeak || !this.voiceConnection) { return; } @@ -332,7 +333,7 @@ export default class { this.channelToSpeakingUsers.get(channelId)?.add(member.id); } - this.suppressVoiceWhenPeopleAreSpeaking(); + this.suppressVoiceWhenPeopleAreSpeaking(turnDownVolumeWhenPeopleSpeakTarget); }); this.voiceConnection.receiver.speaking.on('end', (userId: string) => { @@ -350,18 +351,18 @@ export default class { this.channelToSpeakingUsers.get(channelId)?.delete(member.id); } - this.suppressVoiceWhenPeopleAreSpeaking(); + this.suppressVoiceWhenPeopleAreSpeaking(turnDownVolumeWhenPeopleSpeakTarget); }); } - suppressVoiceWhenPeopleAreSpeaking(): void { + suppressVoiceWhenPeopleAreSpeaking(turnDownVolumeWhenPeopleSpeakTarget: number): void { if (!this.currentChannel) { return; } const speakingUsers = this.channelToSpeakingUsers.get(this.currentChannel.id); if (speakingUsers && speakingUsers.size > 0) { - this.setVolume(this.config.TURN_DOWN_VOLUME_WHEN_PEOPLE_SPEAK_TARGET); + this.setVolume(turnDownVolumeWhenPeopleSpeakTarget); } else { this.setVolume(this.defaultVolume); } -- cgit v1.2.3