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

Commit

Permalink
a new db system's beginning
Browse files Browse the repository at this point in the history
  • Loading branch information
vachanmn123 committed Nov 22, 2021
1 parent 7c9e65b commit faf581b
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 4 deletions.
5 changes: 4 additions & 1 deletion bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -273,6 +274,8 @@ for (const file of distubeEventFiles) {
}
}



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

client.login(token);
client.login(token);
31 changes: 31 additions & 0 deletions commands/moderation/ban.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module.exports = {
name: "ban",
description: "Ban the user",
category: "moderation",
usage: "ban <Ping user>",
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.`);
}

},
};
108 changes: 108 additions & 0 deletions db.js
Original file line number Diff line number Diff line change
@@ -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;
10 changes: 8 additions & 2 deletions events/distubeEvents/addList.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] });
Expand Down
5 changes: 4 additions & 1 deletion events/onReady.js
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -17,5 +19,6 @@ module.exports = {
type: "PLAYING",
});
console.log(`Ready! Logged in as ${client.user.tag}`);
client.db = new Database("./db", client);
},
};
51 changes: 51 additions & 0 deletions interactions/slash/music/add.js
Original file line number Diff line number Diff line change
@@ -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);
}

},
};

0 comments on commit faf581b

Please sign in to comment.