AnixartBot/commands/profile.js
2022-08-10 13:06:23 +02:00

41 lines
2.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Хэндлинг команды profile
*/
const errorHandler = require("../utils/error-handler");
const functions = require("../utils/functions");
const api = require("../network/api");
const keyboards = require("../utils/keyboards");
const modelUser = require('../models/user');
module.exports = bot => async (msg, match) => {
try {
if(msg.text.startsWith(`/account`) || msg.text.startsWith(`/anime`)) return; //Из-за небольшого бага пришлось сделать этот фикс-костыль
if(msg.from.is_bot) return;
let accountName = match.input.replace(`/profile`, ``)
let chatId = msg.chat.id;
let body
if(accountName) {
accountName = accountName.substr(1)
if (accountName.length < 3) return bot.sendMessage(chatId, `🚫 Никнейм пользователя должен состоять минимум из 3 символов.`);
body = await api.getProfileByName(accountName)
} else {
accountName = accountName.substr(1)
let user = await modelUser.findOne({ _id: msg.from.id })
if(!user) return bot.sendMessage(chatId, "*🚫 Вы не можете просмотреть свой профиль.*\nДля доступа к этой команде Вам нужно войти в аккаунт (требуется только Ваш *ник* или *айди*)\n\n*Используйте:* /account\n\n*Если вы не хотите авторизироваться, то можете просто посмотреть свой профиль, написав:*\n/profile \`(ваш ник на аниксарте без скобок)\`", { parse_mode: 'Markdown' })
body = await api.getProfileById(user.anixartId)
}
if (body == null || body.code == 2 || body.profile == null) return bot.sendMessage(chatId, "🚫 Профиля с таким именем не существует или его невозможно найти.");
let profile = body.profile;
const opts = { //Опции (клавиатура и т.д.)
parse_mode: 'Markdown', //Форматирование текста
reply_markup: await keyboards.profileKeyboard(msg.from.id, profile)
};
return bot.sendMessage(chatId, await functions.genProfileText(profile), opts);
} catch (error) {
errorHandler(bot, msg.chat.id, error);
}
};