Skip to content

Instantly share code, notes, and snippets.

@ExperiBass
Last active February 3, 2022 00:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ExperiBass/f152fa4cf47b145dcc0d8c9ecdc9bc90 to your computer and use it in GitHub Desktop.
Save ExperiBass/f152fa4cf47b145dcc0d8c9ecdc9bc90 to your computer and use it in GitHub Desktop.
// The utility functions that interact with the database
// They don't have the "function" prefix as they're in a "module.exports".
// memberID is the Discord users ID.
async addMemberToDB(data, collection) {
try {
let existingData = await module.exports.getData({
memberID: data.memberID
}, collection)
// If the data already exists...
if (existingData) {
// Replace it
await collection.replaceOne({
memberID: existingData.memberID
}, data)
} else {
await collection.insertOne(data)
}
} catch (e) {
throw e
}
},
/**
*
* @param {object} filter Get the data that has the requested property.
* @param {MongoCollection} collection
* @param {boolean} array Whether to return a array or not. Defaults to false.
*/
async getData(filter, collection, array = false) {
let data = await collection.find(filter)
data = await data.toArray()
if (!array) {
data = data[0]
}
return data
},
// This is the method that adds the required data to the database.
async seedDB(members, currencyCollection, interactionsCollection,
inventoryCollection, levelCollection, relationshipCollection, sonaCollection,
infractionsCollection) {
for (let i = 0; i < members.length; i++) {
let member = members[i]
if (!member.id) {
return
}
const currencyData = {
memberID: member.id,
amount: 0
}
const interactionsData = {
memberID: member.id,
given: 0,
received: 0
}
const inventoryData = {
memberID: member.id,
inventory: {}
}
const levelData = {
memberID: member.id,
level: 0,
xp: 0,
totalXP: 0,
lastRole: ''
}
const relationshipData = {
memberID: member.id,
partnerID: null,
leash: null,
leashed: []
}
const sonaData = {
memberID: member.id,
sonas: [{
name: '',
age: '',
weight: '',
height: '',
nsfw: false,
gender: '',
sexuality: '',
species: '',
description: '',
image: ``
}]
}
const infractionsData = {
memberID: member.id,
data: []
}
// Add the member to the database
try {
// for each bit of data, write it to the correct collection.
await module.exports.addMemberToDB(currencyData, currencyCollection)
await module.exports.addMemberToDB(interactionsData, interactionsCollection)
await module.exports.addMemberToDB(inventoryData, inventoryCollection)
await module.exports.addMemberToDB(levelData, levelCollection)
await module.exports.addMemberToDB(relationshipData, relationshipCollection)
await module.exports.addMemberToDB(sonaData, sonaCollection)
await module.exports.addMemberToDB(infractionsData, infractionsCollection)
} catch (e) {
module.exports.log(`DB ERROR:\n\n${e.stack}`, 'ERROR')
throw e // Yes, the error is logged above, but its also thrown so the calling command knows something
// went wrong and doesn't silently fail.
}
}
}
// The code below is separate from the utility functions above.
// It's importing "seedDB" with this line:
const {seedDB} = require("../functions/functions.js")
// Now, here it is being used in the verification command.
try {
let member = message.mentions.members.first()
const existingData = await getData({
memberID: member.id
}, collections.currencyCollection)
if (!existingData) { // "!existingData" because "getData" returns undefined if the requested data doesn't exist.
await seedDB([member], collections.currencyCollection, collections.interactionsCollection, collections.inventoryCollection,
collections.levelCollection, collections.relationshipCollection, collections.sonaCollection, collections.infractionsCollection)
}
await member.roles.add(roles.matey)
await message.channel.send(`Congrats ${member.toString()}! You're now verified! Enjoy your stay on the Isle!`)
} catch (e) {
log(`VERIFICATION ERROR:\n${e.stack}`, 'ERROR')
throw e // Same as in "seedDB"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment