Add stop functionality, fix duration
This commit is contained in:
@@ -1,22 +1,25 @@
|
||||
const {
|
||||
createAudioResource,
|
||||
createAudioPlayer,
|
||||
const {
|
||||
createAudioResource,
|
||||
createAudioPlayer,
|
||||
NoSubscriberBehavior,
|
||||
AudioPlayerStatus,
|
||||
} = require('@discordjs/voice');
|
||||
|
||||
const { EmbedBuilder } = require('discord.js');
|
||||
const musicQueue = require('../musicQueue');
|
||||
|
||||
const { progressBar } = require('../utils/progress');
|
||||
|
||||
async function musicPlayer(guildId, connection, interaction) {
|
||||
const serverQueue = musicQueue.getQueue(guildId);
|
||||
|
||||
|
||||
if (serverQueue.length === 0) {
|
||||
connection.destroy();
|
||||
return;
|
||||
connection.destroy();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
const song = serverQueue[0];
|
||||
|
||||
if(song.stream == null){
|
||||
|
||||
if (song.stream == null) {
|
||||
musicQueue.removeFromQueue(guildId);
|
||||
musicPlayer(guildId, connection);
|
||||
return;
|
||||
@@ -25,7 +28,7 @@ async function musicPlayer(guildId, connection, interaction) {
|
||||
let resource = createAudioResource(song.stream, {
|
||||
inputType: song.type
|
||||
})
|
||||
|
||||
|
||||
let player = createAudioPlayer({
|
||||
behaviors: {
|
||||
noSubscriber: NoSubscriberBehavior.Play
|
||||
@@ -33,16 +36,36 @@ async function musicPlayer(guildId, connection, interaction) {
|
||||
})
|
||||
|
||||
player.play(resource)
|
||||
|
||||
|
||||
connection.subscribe(player)
|
||||
interaction.followUp(`Added **${song.title}** to the queue.`).then(message =>
|
||||
setTimeout(() =>
|
||||
message.delete(),
|
||||
song.duration + 10000));
|
||||
|
||||
progressBar(0, 0, true)
|
||||
|
||||
if(interaction.commandName == "play") {
|
||||
interaction.followUp(`~🎵~`).then(message => {
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor("#E0B0FF")
|
||||
.setTitle("Now playing: " + song.title)
|
||||
.setDescription(progressBar(song.duration, 10).progressBarString);
|
||||
|
||||
message.edit({ embeds: [embed] });
|
||||
|
||||
inter = setInterval(() => {
|
||||
const { progressBarString, isDone } = progressBar(song.duration, 10);
|
||||
if (isDone) {
|
||||
clearInterval(inter);
|
||||
message.delete();
|
||||
}
|
||||
embed.setDescription(progressBarString);
|
||||
message.edit({ embeds: [embed] });
|
||||
}, 2000)
|
||||
});
|
||||
}
|
||||
|
||||
player.on(AudioPlayerStatus.Idle, async () => {
|
||||
console.log('Song ended:', song.title);
|
||||
await musicQueue.removeFromQueue(guildId)
|
||||
console.log('Song ended:', song.title);
|
||||
await musicQueue.removeFromQueue(guildId)
|
||||
musicPlayer(guildId, connection, interaction);
|
||||
});
|
||||
|
||||
return player;
|
||||
|
||||
38
utils/progress.js
Normal file
38
utils/progress.js
Normal file
@@ -0,0 +1,38 @@
|
||||
let startTime;
|
||||
let current = 0;
|
||||
let percentage;
|
||||
|
||||
const progressBar = (totalInMilliseconds, size, reset = false) => {
|
||||
if (reset) {
|
||||
startTime = Date.now();
|
||||
current = 0;
|
||||
}
|
||||
|
||||
if (!startTime) {
|
||||
startTime = Date.now();
|
||||
}
|
||||
|
||||
current = Date.now() - startTime;
|
||||
|
||||
const totalInSeconds = totalInMilliseconds / 1000;
|
||||
percentage = Math.min((current / 1000) / totalInSeconds, 1);
|
||||
const progress = Math.round((size * percentage));
|
||||
const emptyProgress = size - progress;
|
||||
|
||||
const progressText = '▇'.repeat(progress);
|
||||
const emptyProgressText = '—'.repeat(emptyProgress);
|
||||
const percentageText = Math.round(percentage * 100) + '%';
|
||||
|
||||
let elapsedTimeText = new Date(current).toISOString().slice(11, -5);
|
||||
let totalTimeText = new Date(totalInMilliseconds).toISOString().slice(11, -5);
|
||||
|
||||
if (totalTimeText.startsWith('00:')) {
|
||||
elapsedTimeText = elapsedTimeText.slice(3);
|
||||
totalTimeText = totalTimeText.slice(3);
|
||||
}
|
||||
|
||||
const progressBarString = elapsedTimeText + ' [' + progressText + emptyProgressText + ']' + percentageText + ' ' + totalTimeText; // Creating and returning the bar
|
||||
|
||||
return { progressBarString, isDone: percentage === 1 };
|
||||
}
|
||||
module.exports.progressBar = progressBar;
|
||||
@@ -1,47 +1,56 @@
|
||||
const { SlashCommandBuilder } = require('@discordjs/builders');
|
||||
const { REST } = require('@discordjs/rest');
|
||||
const { Routes } = require('discord-api-types/v9');
|
||||
const { SlashCommandBuilder } = require("@discordjs/builders");
|
||||
const { REST } = require("@discordjs/rest");
|
||||
const { Routes } = require("discord-api-types/v9");
|
||||
|
||||
async function registerCommands(clientId, token) {
|
||||
const commands = [
|
||||
new SlashCommandBuilder()
|
||||
.setName('play')
|
||||
.setDescription('Plays songs!')
|
||||
.addStringOption(option =>
|
||||
option.setName('input')
|
||||
.setDescription('Play song from YouTube, Spotify, SoundCloud, etc.')
|
||||
.setName("play")
|
||||
.setDescription("Plays songs!")
|
||||
.addStringOption((option) =>
|
||||
option
|
||||
.setName("input")
|
||||
.setDescription("Play song from YouTube, Spotify, SoundCloud, etc.")
|
||||
.setRequired(true)
|
||||
),
|
||||
new SlashCommandBuilder()
|
||||
.setName('queue')
|
||||
.setDescription('Adds a song to the queue!')
|
||||
.addStringOption(option =>
|
||||
option.setName('song')
|
||||
.setDescription('Add song from YouTube, Spotify, SoundCloud, etc. to the queue')
|
||||
.setName("queue")
|
||||
.setDescription("Adds a song to the queue!")
|
||||
.addStringOption((option) =>
|
||||
option
|
||||
.setName("song")
|
||||
.setDescription(
|
||||
"Add song from YouTube, Spotify, SoundCloud, etc. to the queue"
|
||||
)
|
||||
.setRequired(true)
|
||||
),
|
||||
new SlashCommandBuilder()
|
||||
.setName('pause')
|
||||
.setDescription('Pauses the current song!'),
|
||||
.setName("pause")
|
||||
.setDescription("Pauses the current song!"),
|
||||
new SlashCommandBuilder()
|
||||
.setName('resume')
|
||||
.setDescription('Resumes the current song!'),
|
||||
.setName("resume")
|
||||
.setDescription("Resumes the current song!"),
|
||||
new SlashCommandBuilder()
|
||||
.setName('loop')
|
||||
.setDescription('Loops the current song! (toggle)'),
|
||||
.setName("loop")
|
||||
.setDescription("Loops the current song! (toggle)"),
|
||||
new SlashCommandBuilder()
|
||||
.setName("stop")
|
||||
.setDescription("Stops the current song!"),
|
||||
];
|
||||
|
||||
const rest = new REST({ version: '9' }).setToken(token);
|
||||
const rest = new REST({
|
||||
version: "9",
|
||||
})
|
||||
.setToken(token);
|
||||
|
||||
try {
|
||||
console.log('Started refreshing application (/) commands.');
|
||||
console.log("\x1b[35m", "Started refreshing application (/) commands.");
|
||||
|
||||
await rest.put(
|
||||
Routes.applicationCommands(clientId),
|
||||
{ body: commands },
|
||||
);
|
||||
await rest.put(Routes.applicationCommands(clientId), {
|
||||
body: commands,
|
||||
});
|
||||
|
||||
console.log('Successfully reloaded application (/) commands.');
|
||||
console.log("\x1b[35m", "Successfully reloaded application (/) commands.");
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user