Skip to content

Commit

Permalink
Refactor other commands
Browse files Browse the repository at this point in the history
Signed-off-by: TannerGabriel <gabrieltanner.code@gmail.com>
  • Loading branch information
TannerGabriel committed Jul 16, 2023
1 parent b05d881 commit 1ea4858
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 94 deletions.
4 changes: 2 additions & 2 deletions commands/ban.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ module.exports = {
const member = interaction.options.getUser('user');

if (!member) {
return message.reply('You need to mention the member you want to ban him');
return interaction.reply('You need to mention the member you want to ban him');
}

if (!interaction.member.permissions.has('BAN_MEMBERS')) {
return message.reply("I can't ban this user.");
return interaction.reply("I can't ban this user.");
}

const userinfo = client.users.cache.getMember(member);
Expand Down
10 changes: 5 additions & 5 deletions commands/loop.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const {GuildMember, ApplicationCommandOptionType} = require('discord.js');
const {QueueRepeatMode} = require('discord-player');
const {QueueRepeatMode, useQueue} = require('discord-player');

module.exports = {
name: 'loop',
Expand Down Expand Up @@ -51,17 +51,17 @@ module.exports = {

await interaction.deferReply();

const queue = player.getQueue(interaction.guildId);
if (!queue || !queue.playing) {
const queue = useQueue(interaction.guild.id)
if (!queue || !queue.currentTrack) {
return void interaction.followUp({content: '❌ | No music is being played!'});
}

const loopMode = interaction.options.getInteger('mode');
const success = queue.setRepeatMode(loopMode);
queue.setRepeatMode(loopMode);
const mode = loopMode === QueueRepeatMode.TRACK ? '🔂' : loopMode === QueueRepeatMode.QUEUE ? '🔁' : '▶';

return void interaction.followUp({
content: success ? `${mode} | Updated loop mode!` : '❌ | Could not update loop mode!',
content: `${mode} | Updated loop mode!`,
});
} catch (error) {
console.log(error);
Expand Down
11 changes: 6 additions & 5 deletions commands/move.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const {GuildMember, ApplicationCommandOptionType } = require('discord.js');
const {useQueue} = require("discord-player");

module.exports = {
name: 'move',
Expand Down Expand Up @@ -36,15 +37,15 @@ module.exports = {
}

await interaction.deferReply();
const queue = player.getQueue(interaction.guildId);
if (!queue || !queue.playing) return void interaction.followUp({content: '❌ | No music is being played!'});
const queue = useQueue(interaction.guild.id)
if (!queue || !queue.currentTrack) return void interaction.followUp({content: '❌ | No music is being played!'});
const queueNumbers = [interaction.options.getInteger('track') - 1, interaction.options.getInteger('position') - 1];
if (queueNumbers[0] > queue.tracks.length || queueNumbers[1] > queue.tracks.length)
if (queueNumbers[0] > queue.tracks.size || queueNumbers[1] > queue.tracks.size)
return void interaction.followUp({content: '❌ | Track number greater than queue depth!'});

try {
const track = queue.remove(queueNumbers[0]);
queue.insert(track, queueNumbers[1]);
const track = queue.node.remove(queueNumbers[0]);
queue.node.insert(track, queueNumbers[1]);
return void interaction.followUp({
content: `✅ | Moved **${track}**!`,
});
Expand Down
11 changes: 6 additions & 5 deletions commands/nowplaying.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const {GuildMember} = require('discord.js');
const {useQueue} = require("discord-player");

module.exports = {
name: 'nowplaying',
Expand All @@ -22,19 +23,19 @@ module.exports = {
}

await interaction.deferReply();
const queue = player.getQueue(interaction.guildId);
if (!queue || !queue.playing)
const queue = useQueue(interaction.guild.id)
if (!queue || !queue.currentTrack)
return void interaction.followUp({
content: '❌ | No music is being played!',
});
const progress = queue.createProgressBar();
const perc = queue.getPlayerTimestamp();
const progress = queue.node.createProgressBar()
const perc = queue.node.getTimestamp();

return void interaction.followUp({
embeds: [
{
title: 'Now Playing',
description: `🎶 | **${queue.current.title}**! (\`${perc.progress}%\`)`,
description: `🎶 | **${queue.currentTrack.title}**! (\`${perc.progress}%\`)`,
fields: [
{
name: '\u200b',
Expand Down
39 changes: 1 addition & 38 deletions commands/play.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ module.exports = {
],
async execute(interaction) {
try {
const channel = interaction.member.voice.channel;

const player = useMainPlayer()

if (!(interaction.member instanceof GuildMember) || !interaction.member.voice.channel) {
Expand Down Expand Up @@ -63,44 +61,9 @@ module.exports = {
content: `⏱ | Loading your ${searchResult.playlist ? 'playlist' : 'track'}...`,
});
} catch (error) {
//await interaction.editReply({ content: `${this.container.client.dev.error} | An **error** has occurred` });
// TODO: Return error via interaction
return console.log(error);
}

/*const queue = player?.queues.create(interaction.guild.id, {
metadata: {
channel: interaction.channel,
client: interaction.guild?.members.me
},
leaveOnEmptyCooldown: 300000,
leaveOnEmpty: true,
leaveOnEnd: false,
bufferingTimeout: 0,
volume: 10,
defaultFFmpegFilters: ['lofi', 'bassboost', 'normalizer'],
ytdlOptions: {
quality: "highest",
filter: "audioonly",
highWaterMark: 1 << 30,
dlChunkSize: 0,
},
});*/


/*try {
if (!queue.connection) await queue.connect(interaction.member.voice.channel);
} catch {
queue.delete();
return void interaction.followUp({
content: 'Could not join your voice channel!',
});
}
await interaction.followUp({
content: `⏱ | Loading your ${searchResult.playlist ? 'playlist' : 'track'}...`,
});
searchResult.playlist ? queue.addTrack(searchResult.tracks) : queue.addTrack(searchResult.tracks[0]);
if (!queue.playing) await queue.node.play();*/
} catch (error) {
await interaction.followUp({
content: 'There was an error trying to execute that command: ' + error.message,
Expand Down
17 changes: 4 additions & 13 deletions commands/playtop.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const {GuildMember, ApplicationCommandOptionType} = require('discord.js');
const {QueryType} = require('discord-player');
const {QueryType, useQueue} = require('discord-player');

module.exports = {
name: 'playtop',
Expand Down Expand Up @@ -43,20 +43,11 @@ module.exports = {
if (!searchResult || !searchResult.tracks.length)
return void interaction.followUp({content: 'No results were found!'});

const queue = await player.createQueue(interaction.guild, {
ytdlOptions: {
quality: "highest",
filter: "audioonly",
highWaterMark: 1 << 25,
dlChunkSize: 0,
},
metadata: interaction.channel,
});
const queue = useQueue(interaction.guild.id)

try {
if (!queue.connection) await queue.connect(interaction.member.voice.channel);
} catch {
void player.deleteQueue(interaction.guildId);
return void interaction.followUp({
content: 'Could not join your voice channel!',
});
Expand All @@ -65,8 +56,8 @@ module.exports = {
await interaction.followUp({
content: `⏱ | Loading your ${searchResult.playlist ? 'playlist' : 'track'}...`,
});
searchResult.playlist ? queue.insert(searchResult.tracks, 0) : queue.insert(searchResult.tracks[0], 0);
if (!queue.playing) await queue.play();
searchResult.playlist ? queue.node.insert(searchResult.tracks, 0) : queue.node.insert(searchResult.tracks[0], 0);
if (!queue.currentTrack) await player.play();
} catch (error) {
console.log(error);
interaction.followUp({
Expand Down
10 changes: 4 additions & 6 deletions commands/queue.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
const {GuildMember} = require('discord.js');
const {useQueue} = require("discord-player");

module.exports = {

name: 'queue',
description: 'View the queue of current songs!',

async execute (interaction, player) {

if (!(interaction.member instanceof GuildMember) || !interaction.member.voice.channel) {
return void interaction.reply({
content: 'You are not in a voice channel!',
Expand All @@ -23,14 +21,14 @@ module.exports = {
ephemeral: true,
});
}
var queue = player.getQueue(interaction.guildId);
const queue = useQueue(interaction.guild.id)
if (typeof(queue) != 'undefined') {
trimString = (str, max) => ((str.length > max) ? `${str.slice(0, max - 3)}...` : str);
const trimString = (str, max) => ((str.length > max) ? `${str.slice(0, max - 3)}...` : str);
return void interaction.reply({
embeds: [
{
title: 'Now Playing',
description: trimString(`The Current song playing is 🎶 | **${queue.current.title}**! \n 🎶 | ${queue}! `, 4095),
description: trimString(`The Current song playing is 🎶 | **${queue.currentTrack.title}**! \n 🎶 | ${queue}! `, 4095),
}
]
})
Expand Down
9 changes: 5 additions & 4 deletions commands/remove.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const {GuildMember, ApplicationCommandOptionType } = require('discord.js');
const {useQueue} = require("discord-player");

module.exports = {
name: 'remove',
Expand Down Expand Up @@ -30,12 +31,12 @@ module.exports = {
}

await interaction.deferReply();
const queue = player.getQueue(interaction.guildId);
if (!queue || !queue.playing) return void interaction.followUp({content: '❌ | No music is being played!'});
const queue = useQueue(interaction.guild.id);
if (!queue || !queue.currentTrack) return void interaction.followUp({content: '❌ | No music is being played!'});
const number = interaction.options.getInteger('number') - 1;
if (number > queue.tracks.length)
if (number > queue.tracks.size)
return void interaction.followUp({content: '❌ | Track number greater than queue depth!'});
const removedTrack = queue.remove(number);
const removedTrack = queue.node.remove(number);
return void interaction.followUp({
content: removedTrack ? `✅ | Removed **${removedTrack}**!` : '❌ | Something went wrong!',
});
Expand Down
11 changes: 6 additions & 5 deletions commands/shuffle.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const {GuildMember} = require('discord.js');
const {useQueue} = require("discord-player");

module.exports = {
name: 'shuffle',
Expand All @@ -22,17 +23,17 @@ module.exports = {
}

await interaction.deferReply();
const queue = player.getQueue(interaction.guildId);
if (!queue || !queue.playing) return void interaction.followUp({content: '❌ | No music is being played!'});
const queue = useQueue(interaction.guild.id)
if (!queue || !queue.currentTrack) return void interaction.followUp({content: '❌ | No music is being played!'});
try {
queue.shuffle();
trimString = (str, max) => (str.length > max ? `${str.slice(0, max - 3)}...` : str);
queue.tracks.shuffle();
const trimString = (str, max) => (str.length > max ? `${str.slice(0, max - 3)}...` : str);
return void interaction.followUp({
embeds: [
{
title: 'Now Playing',
description: trimString(
`The Current song playing is 🎶 | **${queue.current.title}**! \n 🎶 | ${queue}! `,
`The Current song playing is 🎶 | **${queue.currentTrack.title}**! \n 🎶 | ${queue}! `,
4095,
),
},
Expand Down
15 changes: 8 additions & 7 deletions commands/swap.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const {GuildMember, ApplicationCommandOptionType } = require('discord.js');
const {useQueue} = require("discord-player");

module.exports = {
name: 'swap',
Expand Down Expand Up @@ -36,21 +37,21 @@ module.exports = {
}

await interaction.deferReply();
const queue = player.getQueue(interaction.guildId);
if (!queue || !queue.playing) return void interaction.followUp({content: '❌ | No music is being played!'});
const queue = useQueue(interaction.guild.id);
if (!queue || !queue.currentTrack) return void interaction.followUp({content: '❌ | No music is being played!'});
const queueNumbers = [interaction.options.getInteger('track1') - 1, interaction.options.getInteger('track2') - 1];
// Sort so the lowest number is first for swap logic to work
queueNumbers.sort(function (a, b) {
return a - b;
});
if (queueNumbers[1] > queue.tracks.length)
if (queueNumbers[1] > queue.getSize())
return void interaction.followUp({content: '❌ | Track number greater than queue depth!'});

try {
const track2 = queue.remove(queueNumbers[1]); // Remove higher track first to avoid list order issues
const track1 = queue.remove(queueNumbers[0]);
queue.insert(track2, queueNumbers[0]); // Add track in lowest position first to avoid list order issues
queue.insert(track1, queueNumbers[1]);
const track2 = queue.node.remove(queueNumbers[1]); // Remove higher track first to avoid list order issues
const track1 = queue.node.remove(queueNumbers[0]);
queue.node.insert(track2, queueNumbers[0]); // Add track in lowest position first to avoid list order issues
queue.node.insert(track1, queueNumbers[1]);
return void interaction.followUp({
content: `✅ | Swapped **${track1}** & **${track2}**!`,
});
Expand Down
9 changes: 5 additions & 4 deletions commands/volume.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const {GuildMember, ApplicationCommandOptionType } = require('discord.js');
const { useQueue} = require("discord-player");

module.exports = {
name: 'volume',
Expand Down Expand Up @@ -30,16 +31,16 @@ module.exports = {
}

await interaction.deferReply();
const queue = player.getQueue(interaction.guildId);
if (!queue || !queue.playing)
const queue = useQueue(interaction.guild.id);
if (!queue || !queue.currentTrack)
return void interaction.followUp({
content: '❌ | No music is being played!',
});

var volume = interaction.options.getInteger('volume');
let volume = interaction.options.getInteger('volume');
volume = Math.max(0, volume);
volume = Math.min(200, volume);
const success = queue.setVolume(volume);
const success = queue.node.setVolume(volume);

return void interaction.followUp({
content: success ? `🔊 | Volume set to ${volume}!` : '❌ | Something went wrong!',
Expand Down

0 comments on commit 1ea4858

Please sign in to comment.