-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Update] added prefixRepository fucntion which has database methods
- Loading branch information
Showing
4 changed files
with
85 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |