2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-08-25 03:17:09 +00:00
the-guard-bot/utils/config.js

40 lines
921 B
JavaScript
Raw Normal View History

2020-03-09 23:27:19 +01:00
// @ts-check
2018-05-04 19:51:49 +02:00
'use strict';
2020-03-09 23:27:19 +01:00
/** @type { import('../typings/config').Config } */
// @ts-ignore
2018-05-04 19:51:49 +02:00
const config = require('../config');
const eq = require('./eq');
2020-05-08 21:30:49 +02:00
const ms = require('millisecond');
const { expireWarnsAfter = Infinity } = config;
const isNewerThan = date => warning => warning.date >= date;
/** @param {Date} date */
const isWarnNotExpired = date =>
isNewerThan(date.getTime() - ms(expireWarnsAfter));
2019-01-28 15:20:00 +01:00
const stringOrNumber = x => [ 'string', 'number' ].includes(typeof x);
2018-05-04 19:51:49 +02:00
2020-03-09 23:27:19 +01:00
// @ts-ignore
2019-01-28 15:20:00 +01:00
const masters = [].concat(config.master);
if (!masters.every(x => stringOrNumber(x) && /^@?\w+$/.test(x))) {
2018-05-04 19:51:49 +02:00
throw new Error('Invalid value for `master` in config file: ' +
config.master);
}
2019-01-28 15:20:00 +01:00
const isMaster = user =>
user && masters.some(x =>
user.id === Number(x) ||
user.username && eq.username(user.username, String(x)));
2018-05-04 19:51:49 +02:00
module.exports = {
2020-03-09 23:27:19 +01:00
config,
2018-05-04 19:51:49 +02:00
isMaster,
2020-05-08 21:30:49 +02:00
isWarnNotExpired,
expireWarnsAfter,
2018-05-04 19:51:49 +02:00
};