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

merged warn db into user db & improved db methods

This commit is contained in:
Pouria Ezzati
2017-09-27 12:34:26 +03:30
parent 40720d8ff9
commit 7f27a30297
12 changed files with 92 additions and 53 deletions

View File

@@ -12,9 +12,13 @@ const { masterID } = loadJSON('config.json');
const { replyOptions } = require('../../bot/options');
// DB
const { isAdmin, admin } = require('../../stores/user');
const { isBanned } = require('../../stores/user');
const { getWarns, nowarns } = require('../../stores/warn');
const {
isAdmin,
admin,
isBanned,
getWarns,
nowarns
} = require('../../stores/user');
const adminHandler = async ({ message, reply }) => {
if (message.from.id !== masterID) {

View File

@@ -10,8 +10,7 @@ const { replyOptions } = require('../../bot/options');
// DB
const { listGroups } = require('../../stores/group');
const { isBanned, ban } = require('../../stores/user');
const { isAdmin } = require('../../stores/user');
const { isAdmin, isBanned, ban } = require('../../stores/user');
const banHandler = async ({ chat, message, reply, telegram }) => {
if (!await isAdmin(message.from)) {

View File

@@ -7,8 +7,7 @@ const { link } = require('../../utils/tg');
const { replyOptions } = require('../../bot/options');
// DB
const { getWarns } = require('../../stores/warn');
const { isAdmin } = require('../../stores/user');
const { isAdmin, getWarns } = require('../../stores/user');
const getWarnsHandler = async ({ message, reply }) => {
if (!await isAdmin(message.from)) {
@@ -25,13 +24,13 @@ const getWarnsHandler = async ({ message, reply }) => {
}
let i = 0;
const warns = await getWarns(theUser);
if (warns.length < 1) {
if (!warns) {
return reply(`✅ <b>no warns for:</b> ${link(theUser)}`, replyOptions);
}
return reply(`⚠️ <b>Warns for</b> ${link(theUser)}:\n\n` +
warns
.map(warn => ++i + '. ' + warn)
.join('\n\n'), replyOptions);
.join('\n'), replyOptions);
};
module.exports = getWarnsHandler;

View File

@@ -8,8 +8,7 @@ const { logError } = require('../../utils/log');
const { replyOptions } = require('../../bot/options');
// DB
const { getWarns, nowarns } = require('../../stores/warn');
const { isAdmin } = require('../../stores/user');
const { isAdmin, getWarns, nowarns } = require('../../stores/user');
const nowarnsHandler = async ({ message, reply }) => {
if (!await isAdmin(message.from)) {
@@ -29,7 +28,7 @@ const nowarnsHandler = async ({ message, reply }) => {
const warns = await getWarns(userToUnwarn);
if (warns.length < 1) {
if (!warns) {
return reply(` ${link(userToUnwarn)} <b>already has no warnings.</b>`,
replyOptions);
}

View File

@@ -9,8 +9,7 @@ const { replyOptions } = require('../../bot/options');
// DB
const { listGroups } = require('../../stores/group');
const { isBanned, unban } = require('../../stores/user');
const { isAdmin } = require('../../stores/user');
const { isAdmin, isBanned, unban } = require('../../stores/user');
const noop = Function.prototype;

View File

@@ -7,8 +7,7 @@ const { link } = require('../../utils/tg');
const { replyOptions } = require('../../bot/options');
// DB
const Warn = require('../../stores/warn');
const { isAdmin } = require('../../stores/user');
const { isAdmin, getWarns, unwarn } = require('../../stores/user');
const unwarnHandler = async ({ message, reply }) => {
if (!await isAdmin(message.from)) {
@@ -26,12 +25,19 @@ const unwarnHandler = async ({ message, reply }) => {
replyOptions);
}
const allWarns = await Warn.getWarns(userToUnwarn);
const warn = await Warn.unwarn(userToUnwarn);
const allWarns = await getWarns(userToUnwarn);
if (!allWarns) {
return reply(` ${link(userToUnwarn)} <b>already has no warnings.</b>`,
replyOptions);
}
await unwarn(userToUnwarn);
return reply(
`${link(message.from)} <b>pardoned</b> ${link(userToUnwarn)} ` +
`<b>for:</b>\n\n${warn} (${allWarns.length}/3)`,
`<b>for:</b>\n\n${allWarns[allWarns.length - 1]}` +
`(${allWarns.length}/3)`,
replyOptions);
};

View File

@@ -13,9 +13,7 @@ const bot = require('../../bot');
const { replyOptions } = require('../../bot/options');
// DB
const { warn } = require('../../stores/warn');
const { ban } = require('../../stores/user');
const { isAdmin } = require('../../stores/user');
const { isAdmin, ban, getWarns, warn } = require('../../stores/user');
const warnHandler = async ({ message, chat, reply }) => {
if (!await isAdmin(message.from)) {
@@ -43,7 +41,8 @@ const warnHandler = async ({ message, chat, reply }) => {
return reply(' <b>Can\'t warn other admins.</b>', replyOptions);
}
const warnCount = await warn(userToWarn, reason);
await warn(userToWarn, reason);
const warnCount = await getWarns(userToWarn);
const promises = [
bot.telegram.deleteMessage(chat.id, message.message_id)
];
@@ -54,10 +53,10 @@ const warnHandler = async ({ message, chat, reply }) => {
message.reply_to_message.message_id));
}
if (warnCount < numberOfWarnsToBan) {
if (warnCount.length < numberOfWarnsToBan) {
promises.push(reply(
`⚠️ ${link(message.from)} <b>warned</b> ${link(userToWarn)} ` +
`<b>for:</b>\n\n ${reason} (${warnCount}/3)`,
`<b>for:</b>\n\n ${reason} (${warnCount.length}/3)`,
replyOptions));
} else {
promises.push(bot.telegram.kickChatMember(chat.id, userToWarn.id));
@@ -65,7 +64,7 @@ const warnHandler = async ({ message, chat, reply }) => {
promises.push(reply(
`🚫 ${link(message.from)} <b>banned</b> ${link(userToWarn)} ` +
'<b>for:</b>\n\nReached max number of warnings ' +
`(${warnCount}/3)\n\n`,
`(${warnCount.length}/3)\n\n`,
replyOptions));
}