mirror of
https://github.com/thedevs-network/the-guard-bot
synced 2025-09-01 22:55:24 +00:00
Bans and warns
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1 +1,4 @@
|
||||
node_modules/*
|
||||
package-lock.json
|
||||
config.json
|
||||
data/*
|
||||
|
24
bans.js
Normal file
24
bans.js
Normal 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
|
||||
};
|
7
index.js
7
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();
|
||||
|
@@ -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"
|
||||
}
|
||||
}
|
||||
|
35
warns.js
Normal file
35
warns.js
Normal 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
|
||||
};
|
Reference in New Issue
Block a user