Skip to content

Commit

Permalink
chore: Add back command to play previous music
Browse files Browse the repository at this point in the history
Fyphen1223 committed May 13, 2024
1 parent c702ca9 commit 8d929be
Showing 4 changed files with 177 additions and 2 deletions.
84 changes: 84 additions & 0 deletions commands/music/back.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
const config = require('../../config.json');

const { getLocale } = require('../../lang/lang.js');
const { createMessageEmbed } = require('../../util/embed.js');
const { parseTimeToSeconds } = require('../../util/time.js');

const guilds = require('../../data/guilds.json');

const discord = require('discord.js');
const {
SlashCommandBuilder,
ButtonBuilder,
ButtonStyle,
ActionRowBuilder,
EmbedBuilder,
} = require('discord.js');
const listenEvents = require('../../util/playerEvent.js');

module.exports = {
data: new SlashCommandBuilder().setName('back').setDescription('Play previous music'),
async execute(interaction) {
await interaction.deferReply();

const guildId = interaction.guild.id;
if (!interaction.member.voice.channelId || !global.queue[guildId]) {
const noValidVCEmbed = createMessageEmbed(
getLocale(guilds[guildId].locale).vc.noVC,
interaction
);
await interaction.editReply({ embeds: [noValidVCEmbed] });
return;
}

if (global.queue[guildId].voiceChannel) {
if (
global.queue[guildId].voiceChannel.id !==
interaction.member.voice.channelId
) {
const differentVCEmbed = createMessageEmbed(
getLocale(guilds[guildId].locale).vc.differentVC,
interaction
);
await interaction.editReply({ embeds: [differentVCEmbed] });
return;
}
}

if (global.queue[guildId].queue.length === 0) {
const noMusicEmbed = createMessageEmbed(
getLocale(guilds[guildId].locale).vc.noMusic,
interaction
);
await interaction.editReply({ embeds: [noMusicEmbed] });
return;
}

const index = global.queue[guildId].index - 1;
global.queue[guildId].previous =
global.queue[guildId].queue[global.queue[guildId].index];

if (index < 0) {
const embed = createMessageEmbed(
getLocale(guilds[guildId].locale).vc.noMoreToBack
);
global.queue[guildId].textChannel.send({ embeds: [embed] });
return;
}

global.queue[guildId].index--;
global.queue[guildId].suppressEnd = true;
global.queue[guildId].player.position = 0;
global.queue[guildId].player.play({
track: {
encoded:
global.queue[guildId].queue[global.queue[guildId].index].data.encoded,
},
});

const backEmbed = createMessageEmbed(getLocale(guilds[guildId].locale).vc.backed);

await interaction.editReply({ embeds: [backEmbed] });
return;
},
};
86 changes: 86 additions & 0 deletions commands/music/skip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
const config = require('../../config.json');

const { getLocale } = require('../../lang/lang.js');
const { createMessageEmbed } = require('../../util/embed.js');
const { parseTimeToSeconds } = require('../../util/time.js');

const guilds = require('../../data/guilds.json');

const discord = require('discord.js');
const {
SlashCommandBuilder,
ButtonBuilder,
ButtonStyle,
ActionRowBuilder,
EmbedBuilder,
} = require('discord.js');
const listenEvents = require('../../util/playerEvent.js');

module.exports = {
data: new SlashCommandBuilder().setName('skip').setDescription('Skip music'),
async execute(interaction) {
await interaction.deferReply();

const guildId = interaction.guild.id;
if (!interaction.member.voice.channelId || !global.queue[guildId]) {
const noValidVCEmbed = createMessageEmbed(
getLocale(guilds[guildId].locale).vc.noVC,
interaction
);
await interaction.editReply({ embeds: [noValidVCEmbed] });
return;
}

if (global.queue[guildId].voiceChannel) {
if (
global.queue[guildId].voiceChannel.id !==
interaction.member.voice.channelId
) {
const differentVCEmbed = createMessageEmbed(
getLocale(guilds[guildId].locale).vc.differentVC,
interaction
);
await interaction.editReply({ embeds: [differentVCEmbed] });
return;
}
}

if (global.queue[guildId].queue.length === 0) {
const noMusicEmbed = createMessageEmbed(
getLocale(guilds[guildId].locale).vc.noMusic,
interaction
);
await interaction.editReply({ embeds: [noMusicEmbed] });
return;
}

const index = global.queue[guildId].index + 1;
global.queue[guildId].previous =
global.queue[guildId].queue[global.queue[guildId].index];

if (index >= global.queue[guildId].queue.length) {
const embed = createMessageEmbed(
getLocale(guilds[guildId].locale).vc.noMoreToSkip
);
global.queue[guildId].textChannel.send({ embeds: [embed] });
return;
}

global.queue[guildId].index++;
global.queue[guildId].suppressEnd = true;
global.queue[guildId].player.position = 0;
global.queue[guildId].player.play({
track: {
encoded:
global.queue[guildId].queue[global.queue[guildId].index].data.encoded,
},
});

const skipEmbed = createMessageEmbed(
getLocale(guilds[guildId].locale).vc.skipped
);

await interaction.editReply({ embeds: [skipEmbed] });
return;
},
};
7 changes: 6 additions & 1 deletion lang/en.json
Original file line number Diff line number Diff line change
@@ -7,7 +7,12 @@
"queueEnded": "👋 - Queue has ended.",
"invalidFormat": "🚫 - Invalid format. Please use the following format: `12h34m56s`",
"outOfLength": "🚫 - The time you provided is out of the length of the song.",
"seeked": "⭕ - Seeked to {time}"
"seeked": "⭕ - Seeked to {time}",
"noMusic": "🚫 - There is no music in the queue.",
"noMoreToSkip": "🚫 - There are no more songs to skip.",
"skipped": "⏭️ - Skipped the current song.",
"noMoreToBack": "🚫 - There are no more songs to go back.",
"backed": "⏮️ - Went back to the previous song."
},
"ping": "Pong! Current Ping is "
}
2 changes: 1 addition & 1 deletion util/playerEvent.js
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ const listenEvents = async (guildId) => {
global.queue[guildId].previous =
global.queue[guildId].queue[global.queue[guildId].index];
global.queue[guildId].player.status = 'finished';
if (index === global.queue[guildId].queue.length) {
if (index >= global.queue[guildId].queue.length) {
if (global.queue[guildId].autoReplay) {
//Do replay things
} else if (global.queue[guildId].autoPlay) {

0 comments on commit 8d929be

Please sign in to comment.