Skip to content

Commit

Permalink
feat: add optional pageSize to /queue command
Browse files Browse the repository at this point in the history
  • Loading branch information
TiagoGrosso committed Aug 23, 2024
1 parent 1e17b94 commit 6e39c8d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added
- An optional `pageSize` to `/queue` command

## [2.9.3] - 2024-08-19

### Fixed
Expand Down
10 changes: 9 additions & 1 deletion src/commands/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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]});
}
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 6e39c8d

Please sign in to comment.