Skip to content

Commit

Permalink
Enhance: Update seek command to accept mm:ss format for timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
LakhindarPal committed Jun 29, 2024
1 parent db26f83 commit 0681c71
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/commands/music/remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ export function execute(interaction, queue) {
embeds: [ErrorEmbed("The queue is empty.")],
});

const position = interaction.options.getNumber("position", true) - 1;
const index = interaction.options.getNumber("position", true) - 1;

if (position >= queue.size)
if (index >= queue.size)
return interaction.reply({
ephemeral: true,
embeds: [ErrorEmbed("The provided position is not valid.")],
});

const removed = queue.node.remove(position);
const removed = queue.node.remove(index);

return interaction.reply({
embeds: [
Expand Down
2 changes: 1 addition & 1 deletion src/commands/music/replay.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function execute(interaction, queue) {
if (!queue.currentTrack) {
return interaction.reply({
ephemeral: true,
embeds: [ErrorEmbed("There is no song currently playing.")],
embeds: [ErrorEmbed("No song is currently playing.")],
});
}

Expand Down
17 changes: 9 additions & 8 deletions src/commands/music/seek.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { ApplicationCommandOptionType } from "discord.js";
import { SuccessEmbed, ErrorEmbed } from "../../modules/Embeds.js";
import { Util } from "discord-player";
import { SuccessEmbed, ErrorEmbed } from "../../modules/Embeds.js";
import { timeToMs } from "../../modules/utils.js";

export const data = {
name: "seek",
description: "Seek to a specific timestamp in the current track.",
description: "Seek the player to a specific timestamp.",
options: [
{
name: "timestamp",
description: "The timestamp to seek to (in seconds).",
type: ApplicationCommandOptionType.Number,
description: "The timestamp to seek to (mm:ss).",
type: ApplicationCommandOptionType.String,
required: true,
min_value: 0,
},
],
category: "music",
Expand All @@ -20,12 +20,13 @@ export const data = {
};

export async function execute(interaction, queue) {
const timestamp = interaction.options.getNumber("timestamp", true) * 1000;
const timestring = interaction.options.getString("timestamp", true);
const timestamp = timeToMs(timestring);

if (!queue.currentTrack) {
return interaction.reply({
ephemeral: true,
embeds: [ErrorEmbed("There is no song currently playing.")],
embeds: [ErrorEmbed("No song is currently playing.")],
});
}

Expand All @@ -34,7 +35,7 @@ export async function execute(interaction, queue) {
ephemeral: true,
embeds: [
ErrorEmbed(
`Please provide a valid timestamp within 0 and ${queue.currentTrack.durationMS / 1000}.`
`Provide a valid timestamp within 0 and ${queue.currentTrack.duration}.`
),
],
});
Expand Down
25 changes: 21 additions & 4 deletions src/modules/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,32 @@ export function formatNumber(number) {
}

/**
* Converts a string to title case.
* @param {string} str - The input string to be converted to title case.
* @returns {string} The input string converted to title case.
* Converts a string to title case, replacing hyphens and underscores with spaces.
* @param {string} str - The string to convert.
* @returns {string} - The string in title case.
*/
export function titleCase(str) {
if (!str) return "";

return str
.trim()
.toLowerCase()
.replace(/(?:^|\s|-|_)\S/g, (match) => match.toUpperCase());
.replace(/[-_]/g, " ")
.replace(/\b\w/g, (match) => match.toUpperCase());
}

/**
* Converts a time string in "hh:mm:ss", "mm:ss", or "ss" format to milliseconds.
* @param {string} time - The time string to convert.
* @returns {number} - The equivalent time in milliseconds, or 0 if input is invalid.
*/
export function timeToMs(time = "0") {
if (typeof time !== "string" || !/^\d{1,2}(:\d{1,2}){0,2}$/.test(time)) {
return 0;
}

const parts = time.split(":").map(Number).reverse();
const [seconds = 0, minutes = 0, hours = 0] = parts;

return (hours * 3600 + minutes * 60 + seconds) * 1000;
}

0 comments on commit 0681c71

Please sign in to comment.