Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] Auto-announce when new song comes on #914

Merged
merged 9 commits into from
Mar 3, 2024
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
Original file line number Diff line number Diff line change
@@ -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;

1 change: 1 addition & 0 deletions schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
25 changes: 25 additions & 0 deletions src/commands/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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-next-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'));
Expand Down Expand Up @@ -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');

Expand All @@ -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 = '';
Expand Down
13 changes: 12 additions & 1 deletion src/services/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
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';

export enum MediaSource {
Youtube,
Expand Down Expand Up @@ -64,7 +65,7 @@ export default class {
public guildId: string;
public loopCurrentSong = false;
public loopCurrentQueue = false;

private currentChannel: VoiceChannel | undefined;
private queue: QueuedSong[] = [];
private queuePosition = 0;
private audioPlayer: AudioPlayer | null = null;
Expand Down Expand Up @@ -102,6 +103,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;
});
}

Expand Down Expand Up @@ -559,6 +562,14 @@ 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 && this.currentChannel) {
await this.currentChannel.send({
embeds: this.getCurrent() ? [buildPlayingMessageEmbed(this)] : [],
});
}
}
}

Expand Down
Loading