Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 additions & 15 deletions packages/bot/src/functions/music/commands/autoplay.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ describe('autoplay command', () => {
expect(interactionReplyMock).toHaveBeenCalled()
})

it('enables autoplay when no queue exists and settings are missing', async () => {
it('disables autoplay when no queue exists and settings are missing (default-on)', async () => {
const client = createClient({ directQueue: null })
const interaction = createInteraction()

Expand All @@ -188,16 +188,12 @@ describe('autoplay command', () => {
} as any)

expect(setGuildSettingsMock).toHaveBeenCalledWith('guild-1', {
autoPlayEnabled: true,
autoPlayEnabled: false,
})
expect(interactionReplyMock).toHaveBeenCalled()
const embedPayload = createEmbedMock.mock.calls[0]?.[0] as {
description: string
}
expect(embedPayload.description).toContain('Next time you use /play')
})

it('shows an error when enabling autoplay without a queue fails to persist', async () => {
it('shows an error when disabling autoplay without a queue fails to persist', async () => {
const client = createClient({ directQueue: null })
const interaction = createInteraction()
setGuildSettingsMock.mockResolvedValue(false)
Expand All @@ -209,7 +205,7 @@ describe('autoplay command', () => {

expect(warnLogMock).toHaveBeenCalledWith(
expect.objectContaining({
message: 'Failed to persist autoplay enabled preference',
message: 'Failed to persist autoplay disabled preference',
}),
)
expect(createEmbedMock).toHaveBeenCalledWith(
Expand Down Expand Up @@ -418,13 +414,11 @@ describe('autoplay command', () => {

it('returns silently when deferReply throws unknown interaction error (10062)', async () => {
const interaction = createInteraction()
interaction.deferReply = jest
.fn()
.mockRejectedValue(
Object.assign(new Error('Unknown interaction'), {
code: 10062,
}),
)
interaction.deferReply = jest.fn().mockRejectedValue(
Object.assign(new Error('Unknown interaction'), {
code: 10062,
}),
)
resolveGuildQueueMock.mockReturnValue({
queue: null,
source: 'miss',
Expand Down
2 changes: 1 addition & 1 deletion packages/bot/src/functions/music/commands/autoplay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ async function resolveCurrentAutoplayState(
return queue.repeatMode === QueueRepeatMode.AUTOPLAY
}
const settings = await guildSettingsService.getGuildSettings(guildId)
return settings?.autoPlayEnabled ?? false
return settings?.autoPlayEnabled ?? true
}

export default new Command({
Expand Down
9 changes: 9 additions & 0 deletions packages/bot/src/utils/music/queueManipulation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ jest.mock('@lucky/shared/utils', () => ({
errorLog: jest.fn(),
}))

const getTrackHistoryMock = jest.fn()

jest.mock('@lucky/shared/services', () => ({
trackHistoryService: {
getTrackHistory: (...args: unknown[]) => getTrackHistoryMock(...args),
},
}))

const getLastFmSeedTracksMock = jest.fn()

jest.mock('./autoplay/lastFmSeeds', () => ({
Expand Down Expand Up @@ -104,6 +112,7 @@ describe('queueManipulation.replenishQueue', () => {
dislikedTrackKeysMock.mockResolvedValue(new Set())
likedTrackKeysMock.mockResolvedValue(new Set())
getLastFmSeedTracksMock.mockResolvedValue([])
getTrackHistoryMock.mockResolvedValue([])
})

async function replenishWithSingleCandidate(options: {
Expand Down
31 changes: 21 additions & 10 deletions packages/bot/src/utils/music/queueManipulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { randomInt } from 'node:crypto'
import type { User } from 'discord.js'
import { debugLog, errorLog } from '@lucky/shared/utils'
import { recommendationFeedbackService } from '../../services/musicRecommendation/feedbackService'
import { trackHistoryService } from '@lucky/shared/services'
import { getLastFmSeedTracks } from './autoplay/lastFmSeeds'
import { cleanSearchQuery, cleanTitle, cleanAuthor } from './searchQueryCleaner'

Expand Down Expand Up @@ -204,25 +205,29 @@ export async function replenishQueue(queue: GuildQueue): Promise<void> {
HISTORY_SEED_LIMIT + 1,
)
const requestedBy = getRequestedBy(queue, currentTrack)
const [dislikedTrackKeys, likedTrackKeys] = await Promise.all([
recommendationFeedbackService.getDislikedTrackKeys(
queue.guild.id,
requestedBy?.id,
),
recommendationFeedbackService.getLikedTrackKeys(
queue.guild.id,
requestedBy?.id,
),
])
const [dislikedTrackKeys, likedTrackKeys, persistentHistory] =
await Promise.all([
recommendationFeedbackService.getDislikedTrackKeys(
queue.guild.id,
requestedBy?.id,
),
recommendationFeedbackService.getLikedTrackKeys(
queue.guild.id,
requestedBy?.id,
),
trackHistoryService.getTrackHistory(queue.guild.id, 20),
])
const excludedUrls = buildExcludedUrls(
queue,
currentTrack,
historyTracks,
persistentHistory,
)
const excludedKeys = buildExcludedKeys(
queue,
currentTrack,
historyTracks,
persistentHistory,
)
const recentArtists = buildRecentArtists(currentTrack, historyTracks)
const candidates = await collectRecommendationCandidates(
Expand Down Expand Up @@ -307,18 +312,21 @@ function buildExcludedUrls(
queue: GuildQueue,
currentTrack: Track,
historyTracks: Track[],
persistentHistory: { url: string }[] = [],
): Set<string> {
return new Set<string>([
currentTrack.url,
...historyTracks.map((track) => track.url),
...queue.tracks.toArray().map((track) => track.url),
...persistentHistory.map((entry) => entry.url).filter(Boolean),
])
}

function buildExcludedKeys(
queue: GuildQueue,
currentTrack: Track,
historyTracks: Track[],
persistentHistory: { title: string; author: string }[] = [],
): Set<string> {
return new Set<string>([
normalizeTrackKey(currentTrack.title, currentTrack.author),
Expand All @@ -328,6 +336,9 @@ function buildExcludedKeys(
...queue.tracks
.toArray()
.map((track) => normalizeTrackKey(track.title, track.author)),
...persistentHistory.map((entry) =>
normalizeTrackKey(entry.title, entry.author),
),
])
}

Expand Down