2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-09-02 15:15:20 +00:00

Bans and warns

This commit is contained in:
Thomas Rory Gummerson
2017-07-24 13:44:14 +02:00
parent 57baf97cae
commit 9116038f4f
5 changed files with 73 additions and 2 deletions

3
.gitignore vendored
View File

@@ -1 +1,4 @@
node_modules/*
package-lock.json
config.json config.json
data/*

24
bans.js Normal file
View File

@@ -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
};

View File

@@ -3,12 +3,17 @@
// validate config // validate config
require('child_process').execSync('node init.js', { stdio: 'inherit' }); require('child_process').execSync('node init.js', { stdio: 'inherit' });
const { loadJSON } = require('./utils/json');
const Telegraf = require('telegraf'); const Telegraf = require('telegraf');
const { loadJSON } = require('./utils/json');
const bans = require('./bans');
const warns = require('./warns');
const config = loadJSON('config.json'); const config = loadJSON('config.json');
const bot = new Telegraf(config.token); const bot = new Telegraf(config.token);
bot.startPolling(); bot.startPolling();

View File

@@ -19,5 +19,9 @@
"bugs": { "bugs": {
"url": "https://github.com/TheDevs-Network/bot/issues" "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"
}
} }

35
warns.js Normal file
View File

@@ -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
};