Skip to content

Commit

Permalink
[Update] added prefixRepository fucntion which has database methods
Browse files Browse the repository at this point in the history
  • Loading branch information
naseif committed Oct 3, 2021
1 parent 3a101e9 commit 7d32943
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 19 deletions.
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { Client, Collection, Intents } = require("discord.js");
const { logger } = require("./modules/logger.js");
const { token, mongourl } = require("./config.json");
const { Database } = require("quickmongo");
const { connectDatabase } = require("./modules/DatabaseConnection");
const { commandsHelper } = require("./modules/commandsHelper");

Expand All @@ -20,6 +21,7 @@ const player = new Player(client);
client.player = player;
client.commands = new Collection();
client.logger = logger;
client.db = new Database(mongourl)

// Register everything...
commandsHelper.registerAllCommands(__dirname + "/commands", client);
Expand Down
40 changes: 21 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"discord.js": "^13.1.0",
"ffmpeg-static": "^4.4.0",
"moment": "^2.29.1",
"mongodb": "^4.1.2",
"mongoose": "^6.0.8",
"node-fetch": "^2.6.1",
"play-dl": "^1.0.3",
Expand Down
61 changes: 61 additions & 0 deletions repositories/PrefixRepository.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/***
* PrefixRepository
*
* We want to enable our bot to be able to remember custom prefixes for the
* prefixed commands using mongodb.
*/
const { Collection: MongoCollection, MongoClient } = require("mongodb");
const { Collection, Fields } = require("quickmongo");

let prefixRepository = function (connectionString) {
let api = {};

api._connectionString = connectionString;

const schema = new Fields.ObjectField({
prefixe: new Fields.ArrayField(new Fields.StringField())
});

async function execute(callback) {
api._mongo = new MongoClient(api._connectionString);
api._mongo.connect().then(async() => {
console.log("Connected to the database!");
try {
await callback();
} catch (error) {
console.log(error);
} finally {
await api._mongo.close();
}
});
}

api.getPrefixes = async function() {

return await execute( async() => {
const mongoCollection = api._mongo.db().collection("JSON");
const db = new Collection(mongoCollection, schema);

api.fetchedPrefixes = await db.get("prefixes");
return api.fetchedPrefixes
});

}

api.setPrefixes = async function(prefixes) {
await execute( async() => {
console.log("setting prefixes to " + prefixes);

const mongoCollection = api._mongo.db().collection("JSON");
const db = new Collection(mongoCollection, schema);

await db.set("prefixes", prefixes);
console.log("complete");
return;
});
}

return api;
};

module.exports = prefixRepository;

0 comments on commit 7d32943

Please sign in to comment.