mirror of
https://github.com/thedevs-network/the-guard-bot
synced 2025-09-05 08:35:22 +00:00
60 lines
1.4 KiB
JavaScript
60 lines
1.4 KiB
JavaScript
'use strict';
|
|
|
|
// DB
|
|
const { getUser } = require('../../stores/user');
|
|
|
|
const checkUsernameHandler = async ({ message }, next) => {
|
|
if (!message.entities) {
|
|
return next();
|
|
}
|
|
const messageArr = message.text ? message.text.split(' ') : '';
|
|
const isCommand = /^\/\w+/.test(messageArr[0]);
|
|
const hasMention = message.entities.some(entity =>
|
|
entity.type === 'mention');
|
|
const hasTextMention = message.entities.some(entity =>
|
|
entity.type === 'text_mention');
|
|
const hasId = /^\d+/.test(messageArr[1]);
|
|
|
|
if (!isCommand) {
|
|
return next();
|
|
}
|
|
|
|
if (hasMention) {
|
|
const [ , username ] = messageArr;
|
|
const isUsername = /^@\w+/.test(username);
|
|
if (!isUsername) {
|
|
return next();
|
|
}
|
|
const user = await getUser({ username: username.replace('@', '') });
|
|
if (user) {
|
|
message.text = message.text.replace(` ${username}`, '');
|
|
message.commandMention = user;
|
|
}
|
|
return next();
|
|
}
|
|
|
|
if (hasTextMention) {
|
|
const [ { user } ] = message.entities.filter(entity => entity.user);
|
|
const name = user.first_name;
|
|
if (name.split(' ')[0] !== messageArr[1]) {
|
|
return next();
|
|
}
|
|
message.text = message.text.replace(` ${name}`, '');
|
|
message.commandMention = user;
|
|
return next();
|
|
}
|
|
|
|
if (hasId) {
|
|
const [ , id ] = messageArr;
|
|
const user = await getUser({ id: Number(id) });
|
|
if (user) {
|
|
message.text = message.text.replace(` ${id}`, '');
|
|
message.commandMention = user;
|
|
}
|
|
return next();
|
|
}
|
|
return next();
|
|
};
|
|
|
|
module.exports = checkUsernameHandler;
|