From 36bdceee629a8f2fc23bc7d8b909746fc4de9ac3 Mon Sep 17 00:00:00 2001 From: mzabolotnev Date: Sat, 3 Dec 2022 15:22:24 +0200 Subject: [PATCH] fix(UABOT-82): add first_name & last_name in case when no username Refactor and move condition logic to method getUserMentionOrName. --- src/utils/telegram.util.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/utils/telegram.util.ts b/src/utils/telegram.util.ts index 16ec7c5c..bffa621c 100644 --- a/src/utils/telegram.util.ts +++ b/src/utils/telegram.util.ts @@ -1,4 +1,4 @@ -import type { Chat, ChatFromGetChat } from '@grammyjs/types/manage'; +import type { Chat, ChatFromGetChat, User } from '@grammyjs/types/manage'; import type { ChatMemberOwner } from 'typegram'; import type { GrammyContext } from '../types'; @@ -42,9 +42,19 @@ export class TelegramUtil { const promoteAdmins = admins.filter((user) => user.status === 'creator' || (user.can_promote_members && !!user.user.username)); const finalAdmins = [...new Set([creator, ...promoteAdmins].filter(Boolean))]; - const adminsString = finalAdmins.length > 0 ? `${finalAdmins.map((user) => `@${user.user.username || ''}`).join(', ')} ` : ''; + const adminsString = finalAdmins.length > 0 ? `${finalAdmins.map((user) => this.getUserMentionOrName(user.user)).join(', ')} ` : ''; return { creator, admins, promoteAdmins, adminsString, finalAdmins }; }); } + + /** + * @param {User} user + */ + getUserMentionOrName(user: User): string { + if (user.username) { + return `@${user.username}`; + } + return `${user.first_name}${user.last_name ? ` ${user.last_name}` : ''}`; + } }