2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-08-31 14:15:25 +00:00

added error handling for db methods

This commit is contained in:
Pouria Ezzati
2017-09-25 09:56:39 +03:30
parent ac6780f4f5
commit a270210248
5 changed files with 33 additions and 10 deletions

View File

@@ -1,5 +1,8 @@
'use strict';
// Utils
const { logError } = require('../utils/log');
const Datastore = require('nedb-promise');
const Admin = new Datastore({
@@ -16,13 +19,15 @@ const admin = user =>
Admin.insert({
first_name: user.first_name,
user_id: user.id,
});
})
.catch(logError(process.env.DEBUG));
const allAdmins = () =>
Admin.find({});
const unadmin = user =>
Admin.remove({ user_id: user.id });
Admin.remove({ user_id: user.id })
.catch(logError(process.env.DEBUG));
const isAdmin = user =>
Admin.findOne({ user_id: user.id });

View File

@@ -1,5 +1,8 @@
'use strict';
// Utils
const { logError } = require('../utils/log');
const Datastore = require('nedb-promise');
const Ban = new Datastore({
@@ -13,10 +16,12 @@ Ban.ensureIndex({
});
const ban = (user, reason) =>
Ban.insert({ reason, user_id: user.id });
Ban.insert({ reason, user_id: user.id })
.catch(logError(process.env.DEBUG));
const unban = user =>
Ban.remove({ user_id: user.id });
Ban.remove({ user_id: user.id })
.catch(logError(process.env.DEBUG));
const isBanned = user =>
Ban.findOne({ user_id: user.id }).then(bannedUser =>

View File

@@ -1,5 +1,8 @@
'use strict';
// Utils
const { logError } = require('../utils/log');
const Datastore = require('nedb-promise');
const groups = new Datastore({
@@ -13,7 +16,8 @@ groups.ensureIndex({
});
const addGroup = group =>
groups.insert(group);
groups.insert(group)
.catch(logError(process.env.DEBUG));
const listGroups = () =>
groups.find({});

View File

@@ -1,5 +1,8 @@
'use strict';
// Utils
const { logError } = require('../utils/log');
const Datastore = require('nedb-promise');
const User = new Datastore({
@@ -13,7 +16,8 @@ User.ensureIndex({
});
const addUser = ({ id, first_name = '', last_name = '', username = '' }) =>
User.insert({ first_name, id, last_name, username });
User.insert({ first_name, id, last_name, username })
.catch(logError(process.env.DEBUG));
const isUser = ({ id }) =>
User.findOne({ id });

View File

@@ -1,5 +1,8 @@
'use strict';
// Utils
const { logError } = require('../utils/log');
const Datastore = require('nedb-promise');
const Warn = new Datastore({
@@ -21,7 +24,8 @@ const warn = (user, reason) =>
Warn.update(
{ user_id: loadedUser.user_id },
{ $push: { reasons: reason } })
.then(() => loadedUser.reasons.length + 1));
.then(() => loadedUser.reasons.length + 1))
.catch(logError(process.env.DEBUG));
const unwarn = user =>
Warn.findOne({ user_id: user.id })
@@ -30,15 +34,16 @@ const unwarn = user =>
lastWarn && Warn.update(
{ user_id: user.id },
{ $pop: { reasons: 1 } })
.then(() => lastWarn));
.then(() => lastWarn))
.catch(logError(process.env.DEBUG));
const nowarns = user =>
Warn.findOne({ user_id: user.id })
.then(isUser => isUser && Warn.update(
{ user_id: user.id },
{ $set: { reasons: [] } })
.then(updatedUser => updatedUser)
);
.then(updatedUser => updatedUser))
.catch(logError(process.env.DEBUG));
const getWarns = user =>
Warn.findOne({ user_id: user.id })