Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Music Button Handler #488

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 141 additions & 0 deletions src/handlers/button.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
const { LoopType } = require("@lavaclient/queue");

/**
* @param {import('@src/structures').BotClient} client
* @param {import('discord.js').ButtonInteraction} interaction
*/
async function pauseButton(client, interaction) {
await interaction.deferUpdate();
const player = client.musicManager.getPlayer(interaction.guildId);

if (!interaction.member.voice.channel) {
return interaction.channel.send("🚫 No music is being played!");
};
if (!interaction.guild.members.me.voice.channel) {
return interaction.channel.send("🚫 You need to join my voice channel.");
};
if (interaction.guild.members.me.voice.channel && !interaction.member.voice.channel.equals(interaction.guild.members.me.voice.channel)) {
return interaction.channel.send("🚫 You're not in the same voice channel.");
};

if (!player.paused) {
await player.pause();
return interaction.channel.send("⏸️ Paused the music player.");
};
if (player.paused) {
await player.resume();
return interaction.channel.send("▶️ Resumed the music player");
};
};

/**
* @param {import('@src/structures').BotClient} client
* @param {import('discord.js').ButtonInteraction} interaction
*/
async function skipButton(client, interaction) {
await interaction.deferUpdate();
const player = client.musicManager.getPlayer(interaction.guildId);

if (!interaction.member.voice.channel) {
return interaction.channel.send("🚫 No music is being played!");
};
if (!interaction.guild.members.me.voice.channel) {
return interaction.channel.send("🚫 You need to join my voice channel.");
};
if (interaction.guild.members.me.voice.channel && !interaction.member.voice.channel.equals(interaction.guild.members.me.voice.channel)) {
return interaction.channel.send("🚫 You're not in the same voice channel.");
};

const { title } = player.queue.current;
return player.queue.next() ? `⏯️ ${title} was skipped.` : "⏯️ There is no song to skip.";
};

/**
* @param {import('@src/structures').BotClient} client
* @param {import('discord.js').ButtonInteraction} interaction
*/
async function stopButton(client, interaction) {
await interaction.deferUpdate();
const player = client.musicManager.getPlayer(interaction.guildId);

if (!interaction.member.voice.channel) {
return interaction.channel.send("🚫 No music is being played!");
};
if (!interaction.guild.members.me.voice.channel) {
return interaction.channel.send("🚫 You need to join my voice channel.");
};
if (interaction.guild.members.me.voice.channel && !interaction.member.voice.channel.equals(interaction.guild.members.me.voice.channel)) {
return interaction.channel.send("🚫 You're not in the same voice channel.");
};

if (player.playing) {
player.disconnect();
await client.musicManager.destroyPlayer(interaction.guildId);
return interaction.channel.send("🎶 The music player is stopped and queue has been cleared");
};
};

/**
* @param {import('@src/structures').BotClient} client
* @param {import('discord.js').ButtonInteraction} interaction
*/
async function loopButton(client, interaction) {
await interaction.deferUpdate();
const player = client.musicManager.getPlayer(interaction.guildId);

if (!interaction.member.voice.channel) {
return interaction.channel.send("🚫 No music is being played!");
};
if (!interaction.guild.members.me.voice.channel) {
return interaction.channel.send("🚫 You need to join my voice channel.");
};
if (interaction.guild.members.me.voice.channel && !interaction.member.voice.channel.equals(interaction.guild.members.me.voice.channel)) {
return interaction.channel.send("🚫 You're not in the same voice channel.");
};

// Looping Track
if (player.queue.loop.type === 0) {
player.queue.setLoop(LoopType.Song);
return interaction.channel.send("Loop mode is set to `track`");
};
// Looping Queue
if (player.queue.loop.type === 2) {
player.queue.setLoop(LoopType.Queue);
return interaction.channel.send("Loop mode is set to `queue`");
};
// Turn OFF Looping
if (player.queue.loop.type === 1) {
player.queue.setLoop(LoopType.None);
return interaction.channel.send("Loop mode is set to `none`");
};
};

/**
* @param {import('@src/structures').BotClient} client
* @param {import('discord.js').ButtonInteraction} interaction
*/
async function shuffleButton(client, interaction) {
await interaction.deferUpdate();
const player = client.musicManager.getPlayer(interaction.guildId);

if (!interaction.member.voice.channel) {
return interaction.channel.send("🚫 No music is being played!");
};
if (!interaction.guild.members.me.voice.channel) {
return interaction.channel.send("🚫 You need to join my voice channel.");
};
if (interaction.guild.members.me.voice.channel && !interaction.member.voice.channel.equals(interaction.guild.members.me.voice.channel)) {
return interaction.channel.send("🚫 You're not in the same voice channel.");
};

player.queue.shuffle();
return interaction.channel.send("🎶 Queue has been shuffled");
};

module.exports = {
pauseButton,
skipButton,
stopButton,
loopButton,
shuffleButton,
};