mirror of
https://github.com/thedevs-network/the-guard-bot
synced 2025-08-31 06:05:22 +00:00
Improve /user
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
// Utils
|
||||
const { parse, strip } = require('../../utils/parse');
|
||||
const { escapeHtml, scheduleDeletion } = require('../../utils/tg');
|
||||
const { scheduleDeletion } = require('../../utils/tg');
|
||||
|
||||
// Bot
|
||||
const { replyOptions } = require('../../bot/options');
|
||||
@@ -10,6 +10,24 @@ const { replyOptions } = require('../../bot/options');
|
||||
// DB
|
||||
const { getUser } = require('../../stores/user');
|
||||
|
||||
const html = require('tg-html');
|
||||
|
||||
const formatDate = date => date && date.toISOString().slice(0, 10);
|
||||
|
||||
const formatEntry = async (entry, defaultVal) => {
|
||||
if (!entry || !entry.by_id) return defaultVal;
|
||||
const { first_name } = await getUser({ id: entry.by_id }) || {};
|
||||
return html`${entry.reason} (${first_name}, ${formatDate(entry.date)})`;
|
||||
};
|
||||
|
||||
const formatWarn = async (warn, i) =>
|
||||
html`${i + 1}. ${await formatEntry(warn, warn)}`;
|
||||
|
||||
const optional = (header, content) =>
|
||||
content
|
||||
? header + content + '\n'
|
||||
: '';
|
||||
|
||||
const getWarnsHandler = async ({ from, message, reply }) => {
|
||||
if (!from) {
|
||||
return reply(
|
||||
@@ -22,7 +40,7 @@ const getWarnsHandler = async ({ from, message, reply }) => {
|
||||
|
||||
if (targets.length > 1) {
|
||||
return reply(
|
||||
'ℹ️ <b>Specify one user to promote.</b>',
|
||||
'ℹ️ <b>Specify one user.</b>',
|
||||
replyOptions
|
||||
).then(scheduleDeletion());
|
||||
}
|
||||
@@ -38,28 +56,22 @@ const getWarnsHandler = async ({ from, message, reply }) => {
|
||||
).then(scheduleDeletion());
|
||||
}
|
||||
|
||||
const { first_name, id, last_name, status, username, warns } = theUser;
|
||||
const { id, first_name, last_name } = theUser;
|
||||
|
||||
const userName = '<b>Name:</b> ' +
|
||||
`<code>${escapeHtml(first_name)} ${escapeHtml(last_name)}</code>\n`;
|
||||
const userName = html`<b>Name:</b> ${first_name} ${last_name}\n`;
|
||||
const userId = `<b>ID:</b> <code>${id}</code>\n`;
|
||||
const userStatus = `<b>Status:</b> <code>${status}</code>\n`;
|
||||
const userUsername = username
|
||||
? `<b>Username:</b> @${username}\n`
|
||||
: '';
|
||||
const banReason = theUser.ban_reason
|
||||
? '\n🚫 <b>Ban reason:</b>\n' +
|
||||
`<code>${escapeHtml(theUser.ban_reason)}</code>`
|
||||
: '';
|
||||
const userWarns = warns.length
|
||||
? '\n<b>⚠️ Warns:</b>\n' + warns
|
||||
.map((warn, i) => `${i + 1}. ${escapeHtml(warn.reason || warn)}`)
|
||||
.join('\n') + '\n'
|
||||
: '';
|
||||
const userUsername = optional('<b>Username:</b> @', theUser.username);
|
||||
const banReason = optional(
|
||||
'\n🚫 <b>Ban reason:</b> ',
|
||||
await formatEntry(theUser.ban_details, theUser.ban_reason)
|
||||
);
|
||||
const userWarns = optional(
|
||||
'\n<b>⚠️ Warns:</b>\n',
|
||||
(await Promise.all(theUser.warns.map(formatWarn))).join('\n')
|
||||
);
|
||||
|
||||
return reply(
|
||||
userName +
|
||||
userStatus +
|
||||
userId +
|
||||
userUsername +
|
||||
userWarns +
|
||||
|
14
package-lock.json
generated
14
package-lock.json
generated
@@ -16,7 +16,7 @@
|
||||
},
|
||||
"acorn-jsx": {
|
||||
"version": "3.0.1",
|
||||
"resolved": "http://registry.npmjs.org/acorn-jsx/-/acorn-jsx-3.0.1.tgz",
|
||||
"resolved": "https://registry.npmjs.org/acorn-jsx/-/acorn-jsx-3.0.1.tgz",
|
||||
"integrity": "sha1-r9+UiPsezvyDSPb7IvRk4ypYs2s=",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
@@ -25,7 +25,7 @@
|
||||
"dependencies": {
|
||||
"acorn": {
|
||||
"version": "3.3.0",
|
||||
"resolved": "http://registry.npmjs.org/acorn/-/acorn-3.3.0.tgz",
|
||||
"resolved": "https://registry.npmjs.org/acorn/-/acorn-3.3.0.tgz",
|
||||
"integrity": "sha1-ReN/s56No/JbruP/U2niu18iAXo=",
|
||||
"dev": true
|
||||
}
|
||||
@@ -130,7 +130,7 @@
|
||||
"dependencies": {
|
||||
"chalk": {
|
||||
"version": "1.1.3",
|
||||
"resolved": "http://registry.npmjs.org/chalk/-/chalk-1.1.3.tgz",
|
||||
"resolved": "https://registry.npmjs.org/chalk/-/chalk-1.1.3.tgz",
|
||||
"integrity": "sha1-qBFcVeSnAv5NFQq9OHKCKn4J/Jg=",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
@@ -143,7 +143,7 @@
|
||||
},
|
||||
"strip-ansi": {
|
||||
"version": "3.0.1",
|
||||
"resolved": "http://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz",
|
||||
"resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz",
|
||||
"integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
@@ -1066,7 +1066,7 @@
|
||||
},
|
||||
"pify": {
|
||||
"version": "2.3.0",
|
||||
"resolved": "http://registry.npmjs.org/pify/-/pify-2.3.0.tgz",
|
||||
"resolved": "https://registry.npmjs.org/pify/-/pify-2.3.0.tgz",
|
||||
"integrity": "sha1-7RQaasBDqEnqWISY59yosVMw6Qw=",
|
||||
"dev": true
|
||||
},
|
||||
@@ -1406,6 +1406,10 @@
|
||||
"integrity": "sha1-f17oI66AUgfACvLfSoTsP8+lcLQ=",
|
||||
"dev": true
|
||||
},
|
||||
"tg-html": {
|
||||
"version": "gist:f9e184b78bbfc4419bc1ee70c238ca6f#5588c7104cbaeb995c3d95e3fa0bc39b8f2d7454",
|
||||
"from": "gist:f9e184b78bbfc4419bc1ee70c238ca6f"
|
||||
},
|
||||
"thenify": {
|
||||
"version": "3.3.0",
|
||||
"resolved": "https://registry.npmjs.org/thenify/-/thenify-3.3.0.tgz",
|
||||
|
@@ -39,6 +39,7 @@
|
||||
"ramda": "^0.25.0",
|
||||
"require-directory": "^2.1.1",
|
||||
"telegraf": "^3.25.0",
|
||||
"tg-html": "gist:f9e184b78bbfc4419bc1ee70c238ca6f",
|
||||
"xregexp": "^4.2.0"
|
||||
},
|
||||
"engines": {
|
||||
|
@@ -109,23 +109,19 @@ const isAdmin = (user) => {
|
||||
return User.findOne({ id: user.id, status: 'admin' });
|
||||
};
|
||||
|
||||
const ban = ({ id }, ban_details) => {
|
||||
const ban_reason = ban_details.reason;
|
||||
return User.update(
|
||||
const ban = ({ id }, ban_details) =>
|
||||
User.update(
|
||||
{ id },
|
||||
{ $set: { ban_details, ban_reason, status: 'banned' } },
|
||||
{ $set: { ban_details, status: 'banned' } },
|
||||
{ upsert: true }
|
||||
);
|
||||
};
|
||||
|
||||
const batchBan = (users, ban_details) => {
|
||||
const ban_reason = ban_details.reason;
|
||||
return User.update(
|
||||
const batchBan = (users, ban_details) =>
|
||||
User.update(
|
||||
{ $or: users.map(strip), $not: { status: 'admin' } },
|
||||
{ $set: { ban_details, ban_reason, status: 'banned' } },
|
||||
{ $set: { ban_details, status: 'banned' } },
|
||||
{ multi: true, returnUpdatedDocs: true }
|
||||
).then(getUpdatedDocument);
|
||||
};
|
||||
|
||||
const ensureExists = ({ id }) =>
|
||||
id && User.insert({ id, status: 'member', warns: [] }).catch(R.F);
|
||||
|
Reference in New Issue
Block a user