Skip to content

Commit

Permalink
Merge branch 'TiagoGrosso-configurable-queue-page-size'
Browse files Browse the repository at this point in the history
  • Loading branch information
Hazzajenko committed Aug 28, 2024
2 parents 1e17b94 + a87078c commit dede552
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added
- An optional `page-size` to `/queue` command
- Add `default-queue-page-size` setting

## [2.9.3] - 2024-08-19

### Fixed
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- 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,
"queueAddResponseEphemeral" BOOLEAN NOT NULL DEFAULT false,
"autoAnnounceNextSong" BOOLEAN NOT NULL DEFAULT false,
"defaultVolume" INTEGER NOT NULL DEFAULT 100,
"defaultQueuePageSize" INTEGER NOT NULL DEFAULT 10,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL
);
INSERT INTO "new_Setting" ("autoAnnounceNextSong", "createdAt", "defaultVolume", "guildId", "leaveIfNoListeners", "playlistLimit", "queueAddResponseEphemeral", "secondsToWaitAfterQueueEmpties", "updatedAt") SELECT "autoAnnounceNextSong", "createdAt", "defaultVolume", "guildId", "leaveIfNoListeners", "playlistLimit", "queueAddResponseEphemeral", "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 @@ -31,6 +31,7 @@ model Setting {
queueAddResponseEphemeral Boolean @default(false)
autoAnnounceNextSong Boolean @default(false)
defaultVolume Int @default(100)
defaultQueuePageSize Int @default(10)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
Expand Down
27 changes: 27 additions & 0 deletions src/commands/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(30)
.setRequired(true)))
.addSubcommand(subcommand => subcommand
.setName('get')
.setDescription('show all settings'));
Expand Down Expand Up @@ -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');

Expand All @@ -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 = '';
Expand Down
19 changes: 17 additions & 2 deletions src/commands/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -14,6 +15,12 @@ export default class implements Command {
.addIntegerOption(option => option
.setName('page')
.setDescription('page of queue to show [default: 1]')
.setRequired(false))
.addIntegerOption(option => option
.setName('page-size')
.setDescription('how many items to display per page [default: 10, max: 30]')
.setMinValue(1)
.setMaxValue(30)
.setRequired(false));

private readonly playerManager: PlayerManager;
Expand All @@ -23,9 +30,17 @@ 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);
const embed = buildQueueEmbed(
player,
interaction.options.getInteger('page') ?? 1,
pageSize,
);

await interaction.reply({embeds: [embed]});
}
Expand Down
10 changes: 4 additions & 6 deletions src/utils/build-embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]+/;
Expand Down Expand Up @@ -77,22 +75,22 @@ 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) {
throw new Error('queue is empty');
}

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)
Expand Down

0 comments on commit dede552

Please sign in to comment.