Skip to content

Instantly share code, notes, and snippets.

@westonpace
Created December 19, 2018 14:09
Show Gist options
  • Save westonpace/df879848678988ef9f9136151f785917 to your computer and use it in GitHub Desktop.
Save westonpace/df879848678988ef9f9136151f785917 to your computer and use it in GitHub Desktop.
Server configuration service backed by mongodb for use with discord
const MongoClient = require('mongodb').MongoClient;
const configService = require('./server-config-service');
function randint(max) {
return Math.round(Math.random() * max);
}
function randomstring() {
const letters = 'ABCDEFHIJKLMNOPQRSTUVWXYZ';
let result = '';
for (let i = 0; i < 10; i++) {
let index = randint(letters.length - 1);
result += letters[index];
}
return result;
}
let mongoClient;
async function start() {
mongoClient = await MongoClient.connect('mongodb://localhost:27017', { useNewUrlParser: true });
const db = mongoClient.db('my-cool-bot-db');
await configService.initialize(db.collection('server-configs'));
console.log(`Channel Id For Server A Before Set: ${await configService.get('A', 'channel-id')}`);
console.log(`Channel Id For Server B Before Set: ${await configService.get('B', 'channel-id')}`);
await configService.set('A', 'channel-id', randomstring());
await configService.set('B', 'channel-id', randomstring());
console.log(`Channel Id For Server A Before Set: ${await configService.get('A', 'channel-id')}`);
console.log(`Channel Id For Server B Before Set: ${await configService.get('B', 'channel-id')}`);
}
start().then(() => {
mongoClient.close().catch(err => {
console.log('Error closing database: ' + err);
});
}).catch(err => {
console.log('Error running demo: ' + err);
mongoClient.close().catch(err => {
console.log('Error closing database: ' + err);
});
});
const AsyncLock = require('async-lock');
let configMap = new Map();
let lock = new AsyncLock();
let configCollection;
async function initialize(collection) {
configCollection = collection;
const configs = await configCollection.find().toArray();
for(const configRecord of configs) {
configMap.set(configRecord.serverId, configRecord.config);
}
}
async function set(serverId, configKey, value) {
await lock.acquire('key', async () => {
const serverConfig = await findOrCreateServerConfig(serverId);
serverConfig[configKey] = value;
await configCollection.updateOne({ serverId }, { $set: { config: serverConfig } });
});
}
async function get(serverId, configKey) {
const serverConfig = await findOrUndefinedServerConfig(serverId);
if (serverConfig) {
return serverConfig[configKey];
}
return undefined;
}
async function findOrCreateServerConfig(serverId) {
let result = findOrUndefinedServerConfig(serverId);
if (!result) {
result = {};
configMap.set(serverId, result);
await configCollection.insertOne({ serverId, config: result });
}
return result;
}
function findOrUndefinedServerConfig(serverId) {
return configMap.get(serverId);
}
module.exports = {
initialize,
get,
set
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment