diff --git a/bot.js b/bot.js index ad6933f..6619a83 100644 --- a/bot.js +++ b/bot.js @@ -36,6 +36,7 @@ const client = new Client({ const eventFiles = fs .readdirSync("./events") .filter((file) => file.endsWith(".js")); + // Loop through all files and execute the event when it is actually emmited. for (const file of eventFiles) { @@ -273,6 +274,8 @@ for (const file of distubeEventFiles) { } } + + // Login into your client application with bot's token. -client.login(token); +client.login(token); \ No newline at end of file diff --git a/commands/moderation/ban.js b/commands/moderation/ban.js new file mode 100644 index 0000000..8d77f21 --- /dev/null +++ b/commands/moderation/ban.js @@ -0,0 +1,31 @@ +module.exports = { + name: "ban", + description: "Ban the user", + category: "moderation", + usage: "ban ", + guildOnly: true, + /** + * @description Executes when the command is called by command handler. + * @author Vachan MN + * @param {Object} message The Message Object of the command. + * @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) { + // Check if the user has the permission to ban. + if (!message.member.hasPermission("BAN_MEMBERS")) { + return message.channel.send( + `${message.author} You don't have permission to ban members.` + ); + } + // Ban the user mentioned + if (message.mentions.users.size) { + const user = message.mentions.users.first(); + message.guild.member(user).ban(); + message.channel.send(`${message.author} Banned ${user}`); + } else { + message.channel.send(`${message.author} You need to mention a user to ban.`); + } + + }, +}; diff --git a/db.js b/db.js new file mode 100644 index 0000000..17d5703 --- /dev/null +++ b/db.js @@ -0,0 +1,108 @@ +/** + * @author: Vachan MN + * @file: This file contains the database object class + * @version: 1.0 + * +*/ + +const fs = require("fs"); + +class Database { + /** + * @description Constructor for the Database class + * @param {string} path Path to the database file + * @param {Object} client bot client + */ + constructor(path, client){ + this.path = path; + this.client = client; + let db = undefined; + try{ + db = fs.readdirSync(path); + console.log("Loaded database"); + }catch(e){ + console.log("Database not found, Creating new Database"); + fs.mkdirSync(path); + db = fs.readdirSync(path); + console.log("Loaded Database"); + } + const Guilds = client.guilds.cache.map(g => g); + for (const server of Guilds) { + try{ + fs.readdirSync(path + "/" + server.id); + console.log("Loaded server database for " + server.name); + } catch(e){ + console.log("Database not found, Creating new Database for " + server.name); + fs.mkdirSync(path + "/" + server.id); + } + } + } + + /** + * @description Get a server db object + * @param {string} serverId Server ID + * @returns {fs.Dir} Server Database Object + */ + + getServerDb(serverId){ + const path = this.path + "/" + serverId; + const db = fs.readdirSync(path); + return db; + } + + /** + * @description Add a new collection to the database + * @param {string} serverId Server ID + * @param {string} collectionName Collection Name + * @returns {Object} Collection json object + */ + addCollection(serverId, collectionName){ + const path = this.path + "/" + serverId + "/" + collectionName + ".json"; + let collection = undefined; + try{ + const collection = fs.readdirSync(path); + console.log("Collection already exists"); + } catch (e){ + fs.writeFileSync(path, JSON.stringify({})); + console.log("Collection created"); + collection = fs.readdirSync(path); + } + return db; + } + + /** + * @description Get a collection from the database + * @param {string} serverId Server ID + * @param {string} collectionName Collection Name + * @returns {Object} Collection json object + */ + getCollection(serverId, collectionName){ + const path = this.path + "/" + serverId + "/" + collectionName + ".json"; + let collection = undefined; + try{ + collection = fs.readdirSync(path); + console.log("Collection found"); + } catch (e){ + console.log("Collection not found"); + } + return collection; + } + + /** + * @description update the collection + * @param {string} serverId Server ID + * @param {string} collectionName Collection Name + * @param {Object} collection Collection Object + */ + updateCollection(serverId, collectionName, collection){ + const path = this.path + "/" + serverId + "/" + collectionName + ".json"; + try{ + fs.writeFileSync(path, JSON.stringify(collection)); + console.log("Collection updated"); + } catch (e){ + console.log("Collection not found"); + } + } +} + +module.exports = Database; \ No newline at end of file diff --git a/events/distubeEvents/addList.js b/events/distubeEvents/addList.js index 08f10a2..d6a4589 100644 --- a/events/distubeEvents/addList.js +++ b/events/distubeEvents/addList.js @@ -16,11 +16,17 @@ const { MessageEmbed } = require('discord.js'); * @param {Object} client Main Application Client */ execute(queue, playlist, client) { + let playlistLength = null + try{ + playlistLength = playlist.tracks.length; + } + catch(e) { + playlistLength = "a few" + } const embed = new MessageEmbed() .setColor('#0099ff') .setTitle(`Added playlist [${playlist.name}](${playlist.url})`) - .setDescription(`${playlist.tracks.length} tracks added`) - .addField('Number of songs in queue:', `queue.songs.length`) + .setDescription(`${playlistLength} tracks added`) .setFooter(`Added by ${playlist.owner}`) .setTimestamp(); queue.textChannel.send({ embeds: [embed] }); diff --git a/events/onReady.js b/events/onReady.js index 81b05e1..f910990 100644 --- a/events/onReady.js +++ b/events/onReady.js @@ -1,8 +1,10 @@ /** * @file Ready Event File. - * @author Naman Vrati + * @author Vachan MN * @since 1.0.0 */ +const fs = require("fs"); +const Database = require("../db"); module.exports = { name: "ready", @@ -17,5 +19,6 @@ module.exports = { type: "PLAYING", }); console.log(`Ready! Logged in as ${client.user.tag}`); + client.db = new Database("./db", client); }, }; diff --git a/interactions/slash/music/add.js b/interactions/slash/music/add.js new file mode 100644 index 0000000..04d647d --- /dev/null +++ b/interactions/slash/music/add.js @@ -0,0 +1,51 @@ +/** + * @file Add a song to the queue + * @author Vachan MN + * @since 3.0.0 + */ + +// Deconstructed the constants we need in this file. + +const { MessageEmbed } = require("discord.js"); +const { SlashCommandBuilder } = require("@discordjs/builders"); + +module.exports = { + // The data needed to register slash commands to Discord. + data: new SlashCommandBuilder() + .setName("add") + .setDescription( + "Add a song" + ) + .addStringOption((option) => + option + .setName("songsearch") + .setDescription("Enter the song's URL or name") + .setRequired(true) + ), + + /** + * @description Executes when the interaction is called by interaction handler. + * @author Vachan MN + * @param {*} interaction The interaction object of the command. + */ + + async execute(interaction) { + /** + * @type {Object[]} + * @description Array of all slash commands objects earlier registered. + */ + + const { guild, author, channel, args } = interaction; + const client = interaction.client; + const searchString = args.join(" "); + if (!author.voice.channel) return interaction.reply('YOU ARENT IN A VC!'); + if (!searchString) return interaction.reply("Please provide a search query!"); + try { + await interaction.channel.sendTyping(); + await client.distube.play(interaction.message, args.join(' '), {unshift: true}); + } catch (e) { + return interaction.reply(e); + } + + }, +};