From 9116038f4fd98f1768ceb4f9311ab2ef487ab3ee Mon Sep 17 00:00:00 2001 From: Thomas Rory Gummerson Date: Mon, 24 Jul 2017 13:44:14 +0200 Subject: [PATCH] Bans and warns --- .gitignore | 3 +++ bans.js | 24 ++++++++++++++++++++++++ index.js | 7 ++++++- package.json | 6 +++++- warns.js | 35 +++++++++++++++++++++++++++++++++++ 5 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 bans.js create mode 100644 warns.js diff --git a/.gitignore b/.gitignore index a107dee..786ed0d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ +node_modules/* +package-lock.json config.json +data/* diff --git a/bans.js b/bans.js new file mode 100644 index 0000000..b5489f3 --- /dev/null +++ b/bans.js @@ -0,0 +1,24 @@ +'use strict'; + +const Datastore = require('nedb-promise'); + +const bans = new Datastore({ + filename: 'data/bans.db', + autoload: true +}); + +bans.ensureIndex({ + fieldName: 'id', + unique: true +}); + +const ban = (user, reason) => + bans.insert(Object.assign({}, user, { reason })).then(() => reason); + +const unban = user => + bans.remove({ id: user.id }); + +module.exports = { + ban, + unban +}; diff --git a/index.js b/index.js index 36d7d0c..1c3322e 100644 --- a/index.js +++ b/index.js @@ -3,12 +3,17 @@ // validate config require('child_process').execSync('node init.js', { stdio: 'inherit' }); -const { loadJSON } = require('./utils/json'); const Telegraf = require('telegraf'); +const { loadJSON } = require('./utils/json'); + +const bans = require('./bans'); +const warns = require('./warns'); + const config = loadJSON('config.json'); const bot = new Telegraf(config.token); + bot.startPolling(); diff --git a/package.json b/package.json index 7fa55f2..4d9aac4 100644 --- a/package.json +++ b/package.json @@ -19,5 +19,9 @@ "bugs": { "url": "https://github.com/TheDevs-Network/bot/issues" }, - "homepage": "https://github.com/TheDevs-Network/bot#readme" + "homepage": "https://github.com/TheDevs-Network/bot#readme", + "dependencies": { + "nedb-promise": "^2.0.1", + "telegraf": "^3.9.0" + } } diff --git a/warns.js b/warns.js new file mode 100644 index 0000000..470cd9e --- /dev/null +++ b/warns.js @@ -0,0 +1,35 @@ +'use strict'; + +const Datastore = require('nedb-promise'); + +const warns = new Datastore({ + filename: 'data/warns.db', + autoload: true +}); + +warns.ensureIndex({ + fieldName: 'id', + unique: true +}); + + +const warn = (user, reason) => + warns.findOne({ id: user.id }).then(exists => + exists || warns.insert(Object.assign({}, user, { warns: [] }))) + .then(user => (warns.update( + { id: user.id }, + { $push: { warns: reason } }), user)) + .then(user => user.warns.length + 1); + +const unwarn = user => + warns.findOne({ id: user.id }).then(exists => + exists && exists.warns.pop()) + .then(warn => + (warn && warns.update({ id: user.id }, { $pop: { warns: 1 } })), + warn); + + +module.exports = { + warn, + unwarn +};