From efcaa6ad3af0402ab37714b0f9f859238f16791b Mon Sep 17 00:00:00 2001 From: Andrew Mike Date: Fri, 10 Mar 2023 23:46:29 -0500 Subject: [PATCH 1/7] added hacky auto-announce when new song comes on --- src/services/player.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/services/player.ts b/src/services/player.ts index 9c1a1226..1eb44846 100644 --- a/src/services/player.ts +++ b/src/services/player.ts @@ -18,7 +18,8 @@ import { } from '@discordjs/voice'; import FileCacheProvider from './file-cache.js'; import debug from '../utils/debug.js'; -import {getGuildSettings} from '../utils/get-guild-settings'; +import {getGuildSettings} from '../utils/get-guild-settings' +import {buildPlayingMessageEmbed} from '../utils/build-embed.js';; export enum MediaSource { Youtube, @@ -61,6 +62,7 @@ export default class { public status = STATUS.PAUSED; public guildId: string; public loopCurrentSong = false; + public currentChannel: VoiceChannel; private queue: QueuedSong[] = []; private queuePosition = 0; @@ -99,6 +101,8 @@ export default class { oldNetworking?.off('stateChange', networkStateChangeHandler); newNetworking?.on('stateChange', networkStateChangeHandler); /* 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; }); } @@ -543,6 +547,9 @@ export default class { if (newState.status === AudioPlayerStatus.Idle && this.status === STATUS.PLAYING) { await this.forward(1); + await this.currentChannel.send({ + embeds: this.getCurrent() ? [buildPlayingMessageEmbed(this)]: [], + }); } } From dc8815f29105741052b768bc1fb6fe50db2d7176 Mon Sep 17 00:00:00 2001 From: Andrew Mike Date: Fri, 1 Mar 2024 19:14:39 -0500 Subject: [PATCH 2/7] Fixilinting errors --- src/services/player.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/services/player.ts b/src/services/player.ts index 7d165187..865eb7b4 100644 --- a/src/services/player.ts +++ b/src/services/player.ts @@ -18,8 +18,8 @@ import { } from '@discordjs/voice'; 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 {getGuildSettings} from '../utils/get-guild-settings.js'; +import {buildPlayingMessageEmbed} from '../utils/build-embed.js'; export enum MediaSource { Youtube, @@ -66,8 +66,7 @@ export default class { public loopCurrentSong = false; public loopCurrentQueue = false; public currentChannel: VoiceChannel; - - private queue: QueuedSong[] = []; + private queue: QueuedSong[] = []; private queuePosition = 0; private audioPlayer: AudioPlayer | null = null; private nowPlaying: QueuedSong | null = null; @@ -564,7 +563,7 @@ export default class { if (newState.status === AudioPlayerStatus.Idle && this.status === STATUS.PLAYING) { await this.forward(1); await this.currentChannel.send({ - embeds: this.getCurrent() ? [buildPlayingMessageEmbed(this)]: [], + embeds: this.getCurrent() ? [buildPlayingMessageEmbed(this)] : [], }); } } From 6e43d695eb0e85849335e1945cc250ead0ca590d Mon Sep 17 00:00:00 2001 From: Andrew Mike Date: Fri, 1 Mar 2024 19:42:06 -0500 Subject: [PATCH 3/7] Wire up configuration option for auto announce next song --- schema.prisma | 1 + src/commands/config.ts | 25 +++++++++++++++++++++++++ src/services/player.ts | 16 +++++++++++----- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/schema.prisma b/schema.prisma index 556cd6b7..10cc6247 100644 --- a/schema.prisma +++ b/schema.prisma @@ -28,6 +28,7 @@ model Setting { playlistLimit Int @default(50) secondsToWaitAfterQueueEmpties Int @default(30) leaveIfNoListeners Boolean @default(true) + autoAnnounceNextSong Boolean @default(false) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt } diff --git a/src/commands/config.ts b/src/commands/config.ts index 7f25a2e7..9e6c1deb 100644 --- a/src/commands/config.ts +++ b/src/commands/config.ts @@ -33,6 +33,13 @@ export default class implements Command { .setName('value') .setDescription('whether to leave when everyone else leaves') .setRequired(true))) + .addSubcommand(subcommand => subcommand + .setName('set-auto-announce-new-song') + .setDescription('set whether to announce the next song in the queue automatically') + .addBooleanOption(option => option + .setName('value') + .setDescription('whether to announce the next song in the queue automatically') + .setRequired(true))) .addSubcommand(subcommand => subcommand .setName('get') .setDescription('show all settings')); @@ -97,6 +104,23 @@ export default class implements Command { break; } + case 'set-auto-announce-next-song': { + const value = interaction.options.getBoolean('value')!; + + await prisma.setting.update({ + where: { + guildId: interaction.guild!.id, + }, + data: { + autoAnnounceNextSong: value, + }, + }); + + await interaction.reply('👍 auto announce setting updated'); + + break; + } + case 'get': { const embed = new EmbedBuilder().setTitle('Config'); @@ -108,6 +132,7 @@ export default class implements Command { ? 'never leave' : `${config.secondsToWaitAfterQueueEmpties}s`, 'Leave if there are no listeners': config.leaveIfNoListeners ? 'yes' : 'no', + 'Auto announce next song in queue': config.autoAnnounceNextSong ? 'yes' : 'no', }; let description = ''; diff --git a/src/services/player.ts b/src/services/player.ts index 865eb7b4..ab31b328 100644 --- a/src/services/player.ts +++ b/src/services/player.ts @@ -65,8 +65,8 @@ export default class { public guildId: string; public loopCurrentSong = false; public loopCurrentQueue = false; - public currentChannel: VoiceChannel; - private queue: QueuedSong[] = []; + public currentChannel: VoiceChannel = null; + private queue: QueuedSong[] = []; private queuePosition = 0; private audioPlayer: AudioPlayer | null = null; private nowPlaying: QueuedSong | null = null; @@ -562,9 +562,15 @@ export default class { if (newState.status === AudioPlayerStatus.Idle && this.status === STATUS.PLAYING) { await this.forward(1); - await this.currentChannel.send({ - embeds: this.getCurrent() ? [buildPlayingMessageEmbed(this)] : [], - }); + + //auto announce the next song if set up to + const settings = await getGuildSettings(this.guildId); + const {autoAnnounceNextSong} = settings; + if (autoAnnounceNextSong == true) { + await this.currentChannel.send({ + embeds: this.getCurrent() ? [buildPlayingMessageEmbed(this)] : [], + }); + } } } From e78937e6c32b9e89d1255fdd7b9d18949195525d Mon Sep 17 00:00:00 2001 From: Andrew Mike Date: Fri, 1 Mar 2024 20:04:47 -0500 Subject: [PATCH 4/7] Lint fixes --- src/services/player.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/services/player.ts b/src/services/player.ts index ab31b328..d720fd15 100644 --- a/src/services/player.ts +++ b/src/services/player.ts @@ -65,7 +65,7 @@ export default class { public guildId: string; public loopCurrentSong = false; public loopCurrentQueue = false; - public currentChannel: VoiceChannel = null; + public currentChannel: VoiceChannel | null = null; private queue: QueuedSong[] = []; private queuePosition = 0; private audioPlayer: AudioPlayer | null = null; @@ -562,11 +562,11 @@ export default class { if (newState.status === AudioPlayerStatus.Idle && this.status === STATUS.PLAYING) { await this.forward(1); - - //auto announce the next song if set up to + + // Auto announce the next song if configured to const settings = await getGuildSettings(this.guildId); const {autoAnnounceNextSong} = settings; - if (autoAnnounceNextSong == true) { + if (autoAnnounceNextSong) { await this.currentChannel.send({ embeds: this.getCurrent() ? [buildPlayingMessageEmbed(this)] : [], }); From abe7eebb78a0e0cfb4188202118f99511c930959 Mon Sep 17 00:00:00 2001 From: Andrew Mike Date: Fri, 1 Mar 2024 20:06:06 -0500 Subject: [PATCH 5/7] more typing fixes --- src/services/player.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services/player.ts b/src/services/player.ts index d720fd15..8419332e 100644 --- a/src/services/player.ts +++ b/src/services/player.ts @@ -65,7 +65,7 @@ export default class { public guildId: string; public loopCurrentSong = false; public loopCurrentQueue = false; - public currentChannel: VoiceChannel | null = null; + public currentChannel: VoiceChannel; private queue: QueuedSong[] = []; private queuePosition = 0; private audioPlayer: AudioPlayer | null = null; From 195a619e1f58d3c33aa7976b1cc6341e0133a958 Mon Sep 17 00:00:00 2001 From: Andrew Mike Date: Fri, 1 Mar 2024 21:31:18 -0500 Subject: [PATCH 6/7] fixed linting and type errors --- src/commands/config.ts | 2 +- src/services/player.ts | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/commands/config.ts b/src/commands/config.ts index 9e6c1deb..9441dab2 100644 --- a/src/commands/config.ts +++ b/src/commands/config.ts @@ -34,7 +34,7 @@ export default class implements Command { .setDescription('whether to leave when everyone else leaves') .setRequired(true))) .addSubcommand(subcommand => subcommand - .setName('set-auto-announce-new-song') + .setName('set-auto-announce-next-song') .setDescription('set whether to announce the next song in the queue automatically') .addBooleanOption(option => option .setName('value') diff --git a/src/services/player.ts b/src/services/player.ts index 8419332e..bfd29ad5 100644 --- a/src/services/player.ts +++ b/src/services/player.ts @@ -65,7 +65,7 @@ export default class { public guildId: string; public loopCurrentSong = false; public loopCurrentQueue = false; - public currentChannel: VoiceChannel; + private currentChannel: VoiceChannel | undefined; private queue: QueuedSong[] = []; private queuePosition = 0; private audioPlayer: AudioPlayer | null = null; @@ -562,11 +562,10 @@ export default class { if (newState.status === AudioPlayerStatus.Idle && this.status === STATUS.PLAYING) { await this.forward(1); - // Auto announce the next song if configured to const settings = await getGuildSettings(this.guildId); const {autoAnnounceNextSong} = settings; - if (autoAnnounceNextSong) { + if (autoAnnounceNextSong && this.currentChannel) { await this.currentChannel.send({ embeds: this.getCurrent() ? [buildPlayingMessageEmbed(this)] : [], }); From 187c651fc39d930936284b2af96cd61cdaa27979 Mon Sep 17 00:00:00 2001 From: Andrew Mike Date: Fri, 1 Mar 2024 21:48:56 -0500 Subject: [PATCH 7/7] Add missing database migration --- .../migration.sql | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 migrations/20240301214725_add_auto_announce_next_song/migration.sql diff --git a/migrations/20240301214725_add_auto_announce_next_song/migration.sql b/migrations/20240301214725_add_auto_announce_next_song/migration.sql new file mode 100644 index 00000000..f8631451 --- /dev/null +++ b/migrations/20240301214725_add_auto_announce_next_song/migration.sql @@ -0,0 +1,17 @@ +-- RedefineTables +PRAGMA foreign_keys=OFF; +CREATE TABLE "new_Setting" ( + "guildId" TEXT NOT NULL PRIMARY KEY, + "playlistLimit" INTEGER NOT NULL DEFAULT 50, + "secondsToWaitAfterQueueEmpties" INTEGER NOT NULL DEFAULT 30, + "leaveIfNoListeners" BOOLEAN NOT NULL DEFAULT true, + "autoAnnounceNextSong" BOOLEAN NOT NULL DEFAULT false, + "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" DATETIME NOT NULL +); +INSERT INTO "new_Setting" ("createdAt", "guildId", "leaveIfNoListeners", "playlistLimit", "secondsToWaitAfterQueueEmpties", "updatedAt") SELECT "createdAt", "guildId", "leaveIfNoListeners", "playlistLimit", "secondsToWaitAfterQueueEmpties", "updatedAt" FROM "Setting"; +DROP TABLE "Setting"; +ALTER TABLE "new_Setting" RENAME TO "Setting"; +PRAGMA foreign_key_check; +PRAGMA foreign_keys=ON; +