Skip to content
This repository has been archived by the owner on Jun 18, 2022. It is now read-only.

Commit

Permalink
Add proper DisTube event handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
vachanmn123 committed Nov 3, 2021
1 parent a93378e commit 5888998
Show file tree
Hide file tree
Showing 18 changed files with 1,307 additions and 1,108 deletions.
46 changes: 19 additions & 27 deletions bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ client.contextCommands = new Collection();
client.cooldowns = new Collection();
client.triggers = new Collection();
client.distube = new DisTube.default(client, {
searchSongs: 1,
searchSongs: 5,
searchCooldown: 30,
leaveOnEmpty: true,
emptyCooldown: 0,
leaveOnFinish: true,
leaveOnStop: true,
leaveOnFinish: false,
leaveOnStop: false,
plugins: [new SoundCloudPlugin.default(), new SpotifyPlugin.default()],
ytdlOptions: {
quality: "highestaudio",
Expand Down Expand Up @@ -255,31 +255,23 @@ for (const folder of triggerFolders) {
}

// Distube initialization.
const distubeEventFiles = fs
.readdirSync("./events/distubeEvents")
.filter((file) => file.endsWith(".js"));

// Loop through all files and execute the event when it is actually emmited.
const status = queue => `Volume: \`${queue.volume}%\` | Filter: \`${queue.filters.join(", ") || "Off"}\` | Loop: \`${queue.repeatMode ? queue.repeatMode === 2 ? "All Queue" : "This Song" : "Off"}\` | Autoplay: \`${queue.autoplay ? "On" : "Off"}\``
client.distube
.on("playSong", (queue, song) => queue.textChannel.send(
`Playing \`${song.name}\` - \`${song.formattedDuration}\`\nRequested by: ${song.user}\n${status(queue)}`
))
.on("addSong", (queue, song) => queue.textChannel.send(
`Added ${song.name} - \`${song.formattedDuration}\` to the queue by ${song.user}`
))
.on("addList", (queue, playlist) => queue.textChannel.send(
`Added \`${playlist.name}\` playlist (${playlist.songs.length} songs) to queue\n${status(queue)}`
))
// DisTubeOptions.searchSongs = true
.on("searchResult", (message, result) => {
let i = 0
message.channel.send(`**Choose an option from below**\n${result.map(song => `**${++i}**. ${song.name} - \`${song.formattedDuration}\``).join("\n")}\n*Enter anything else or wait 60 seconds to cancel*`)
})
// DisTubeOptions.searchSongs = true
.on("searchCancel", message => message.channel.send(`Searching canceled`))
.on("error", (channel, e) => {
channel.send(`An error encountered: ${e}`)
console.error(e)
})
.on("empty", channel => channel.send("Voice channel is empty! Leaving the channel..."))
.on("searchNoResult", message => message.channel.send(`No result found!`))
.on("finish", queue => queue.textChannel.send("Finished!"))
for (const file of distubeEventFiles) {
const event = require(`./events/distubeEvents/${file}`);
if (event) {
client.distube.once(event.name, (...args) => event.execute(...args, client));
} else {
client.distube.on(
event.name,
async (...args) => await event.execute(...args, client)
);
}
}

// Login into your client application with bot's token.

Expand Down
13 changes: 8 additions & 5 deletions commands/music/pause.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { MessageEmbed } = require('discord.js');

module.exports = {
name: "pause",
description: "pause the playing song",
Expand All @@ -14,11 +16,12 @@ module.exports = {
const client = message.client;
const queue = client.distube.getQueue(message)
if (!queue) return message.channel.send(`There is nothing in the queue right now!`)
if (queue.pause) {
queue.resume()
return message.channel.send("Resumed the song for you :)")
}
queue.pause()
message.channel.send("Paused the song for you :)")
const embed = new MessageEmbed()
.setColor('#0099ff')
.setTitle('Music Paused')
.setDescription(`${message.author} has paused the music!`)
.setTimestamp();
message.channel.send({ embeds: [embed] })
},
};
4 changes: 2 additions & 2 deletions commands/music/play.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ module.exports = {
* @param {String[]} args The Message Content of the received message seperated by spaces (' ') in an array, this excludes prefix and command/alias itself.
*/

execute(message, args) {
async execute(message, args) {
const distube = message.client.distube
searchString = args.join(' ');
if (!message.member.voice.channel) return message.reply('YOU ARENT IN A VC!')
if (!searchString) return message.reply('Please enter the search string or url')
try {
distube.play(message, args.join(' '))
distube.play(message, args.join(' '));
} catch (e) {
return message.reply(e)
}
Expand Down
10 changes: 9 additions & 1 deletion commands/music/queue.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { MessageEmbed } = require('discord.js');

module.exports = {
name: "queue",
description: "show the queue",
Expand All @@ -14,6 +16,12 @@ module.exports = {
const queue = message.client.distube.getQueue(message)
if (!queue) return message.channel.send(`There is nothing playing!`)
const q = queue.songs.map((song, i) => `${i === 0 ? "Playing:" : `${i}.`} ${song.name} - \`${song.formattedDuration}\``).join("\n")
message.channel.send(`**Server Queue**\n${q}`)
const embed = new MessageEmbed()
.setColor("#101010")
.setTitle("Queue")
.setDescription(q)
.setFooter(`${queue.songs.length} songs in queue`)
.setTimestamp();
message.channel.send({ embeds: [embed] })
},
};
28 changes: 28 additions & 0 deletions events/distubeEvents/addList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const { MessageEmbed } = require('discord.js');

/**
* @file disTube add playlist event.
* @author Vachan MN
* @since 1.0.0
*/

module.exports = {
name: "addList",

/**
* @description Executes the block of code when a playlist is added to the queue
* @param {object} queue queue Object
* @param {object} playlist playlist object
* @param {Object} client Main Application Client
*/
execute(queue, playlist, client) {
const embed = new MessageEmbed()
.setColor('#0099ff')
.setTitle(`Added playlist [${playlist.name}](${playlist.url})`)
.setDescription(`${playlist.tracks.length} tracks added`)
.addField('Number of songs', playlist.songs.length)
.setFooter(`Added by ${playlist.owner}`)
.setTimestamp();
queue.textChannel.send({ embeds: [embed] });
},
};
28 changes: 28 additions & 0 deletions events/distubeEvents/addSong.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const { MessageEmbed } = require('discord.js');

/**
* @file disTube add Song event.
* @author Vachan MN
* @since 1.0.0
*/

module.exports = {
name: "addSong",

/**
* @description Executes the block of code when a song is added to queue!
* @param {Object} queue queue Object
* @param {Object} song Song Object
* @param {Object} client Main Application Client
*/
execute(queue, song, client) {
const emb = new MessageEmbed()
.setTitle("Added Song to queue")
.setColor("#BFFF00")
.setDescription(`Song: [${song.name}](${song.url})`)
.addField("Duration", song.formattedDuration, true)
.setImage(song.thumbnail)
.setFooter(`Requested by ${song.user.username}#${song.user.discriminator}`);
queue.textChannel.send({ embeds: [emb] });
},
};
26 changes: 26 additions & 0 deletions events/distubeEvents/empty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const { MessageEmbed } = require('discord.js');

/**
* @file disTube empty event.
* @author Vachan MN
* @since 1.0.0
*/

module.exports = {
name: "empty",

/**
* @description Executes the block of code when the voice channel is empty
* @param {object} channel Channel Object
* @param {Object} client Main Application Client
*/
execute(channel, client) {
const emb = new MessageEmbed()
.setColor('#ff0000')
.setTitle('🔇 Voice Channel Empty')
.setDescription('The Voice channel is empty. Leaving the channel..')
.setFooter("Hope you enjoyed your time.")
.setTimestamp();
channel.textChannel.send({ embeds: [emb] });
},
};
27 changes: 27 additions & 0 deletions events/distubeEvents/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const { MessageEmbed } = require('discord.js');

/**
* @file disTube error event.
* @author Vachan MN
* @since 1.0.0
*/

module.exports = {
name: "error",

/**
* @description Executes the block of code when an error occurs in distube
* @param {object} channel Channel Object
* @param {object} error Error Object
* @param {Object} client Main Application Client
*/
execute(channel, error, client) {
const errEmb = new MessageEmbed()
.setTitle("Error Ocurred")
.setDescription(`${error.message}`)
.setColor("#ff0000")
.setFooter(`Contact <@!${client.owner}>`)
.setTimestamp();
channel.send({ embeds: [errEmb] });
},
};
26 changes: 26 additions & 0 deletions events/distubeEvents/finish.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const { MessageEmbed } = require('discord.js');

/**
* @file disTube finish event.
* @author Vachan MN
* @since 1.0.0
*/

module.exports = {
name: "finish",

/**
* @description Executes the block of code when the queue finishes
* @param {object} queue queue Object
* @param {Object} client Main Application Client
*/
execute(queue, client) {
const emb = new MessageEmbed()
.setColor('#0099ff')
.setTitle('Finished')
.setDescription(`Finished playing ${queue.songs[0].title}`)
.setThumbnail(queue.songs[0].thumbnail)
.setTimestamp();
queue.textChannel.send(emb);
},
};
29 changes: 29 additions & 0 deletions events/distubeEvents/playSong.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const { MessageEmbed } = require('discord.js');

/**
* @file disTube play Song event.
* @author Vachan MN
* @since 1.0.0
*/

module.exports = {
name: "playSong",

/**
* @description Executes the block of code when a song is played!
* @param {Object} queue queue Object
* @param {Object} song Song Object
* @param {Object} client Main Application Client
*/
execute(queue, song, client) {
const emb = new MessageEmbed()
.setTitle("Playing Song")
.setColor("#BFFF00")
.setDescription(`Now Playing: [${song.name}](${song.url})`)
.addField("Duration", song.formattedDuration, true)
.setImage(song.thumbnail)
.setFooter(`Requested by ${song.user.username}#${song.user.discriminator}`);
queue.textChannel.send({ embeds: [emb] });
console.log(`Song Played: ${song.name} | ${song.url}`);
},
};
25 changes: 25 additions & 0 deletions events/distubeEvents/searchCancel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const { MessageEmbed } = require('discord.js');

/**
* @file disTube searchCancel event.
* @author Vachan MN
* @since 1.0.0
*/

module.exports = {
name: "searchCancel",

/**
* @description Executes the block of code when a search gets canceled.
* @param {object} message message Object
* @param {Object} client Main Application Client
*/
execute(message, client) {
const emb = new MessageEmbed()
.setColor('#ff0000')
.setTitle('Search Canceled')
.setDescription('Search has been canceled.')
.setTimestamp();
message.channel.send({ embeds: [emb] });
},
};
20 changes: 20 additions & 0 deletions events/distubeEvents/searchDone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const { MessageEmbed } = require('discord.js');

/**
* @file disTube search done event.
* @author Vachan MN
* @since 1.0.0
*/

module.exports = {
name: "searchDone",

/**
* @description Executes the block of code when the Search is completed
* @param {Object} message The users search response message
* @param {Object} answer The answered message of user
*/
execute(message, answer, client) {
console.log("SEARCH COMPLETED")
},
};
26 changes: 26 additions & 0 deletions events/distubeEvents/searchInvalidAnswer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const { MessageEmbed } = require('discord.js');

/**
* @file disTube Search Invalid response event.
* @author Vachan MN
* @since 1.0.0
*/

module.exports = {
name: "searchInvalidAnswer",

/**
* @description Executes the block of code when the voice channel is empty
* @param {object} message original message Object
* @param {object} answer user answer message object
* @param {Object} client Main Application Client
*/
execute(message, answer, client) {
const embed = new MessageEmbed()
.setColor('#ff0000')
.setTitle('Invalid answer')
.setDescription('Please select a valid answer.')
.setFooter('Please try again.');
message.channel.send({ embeds: [embed] });
},
};
25 changes: 25 additions & 0 deletions events/distubeEvents/searchNoResult.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const { MessageEmbed } = require('discord.js');

/**
* @file disTube searchNoResult event.
* @author Vachan MN
* @since 1.0.0
*/

module.exports = {
name: "searchNoResult",

/**
* @description Executes the block of code when an there are no results for a search
* @param {object} message message Object
* @param {Object} client Main Application Client
*/
execute(message, client) {
const emb = new MessageEmbed()
.setColor('#ff0000')
.setTitle('No Results Found')
.setDescription('No results found for your search. Please try again.')
.setTimestamp();
message.channel.send({ embeds: [emb] });
},
};
Loading

0 comments on commit 5888998

Please sign in to comment.