Skip to content

Commit

Permalink
Merge pull request #241 from MoC-OSS/bugfix/UABOT-82/username
Browse files Browse the repository at this point in the history
fix(UABOT-82): add first_name & last_name in case when no username
  • Loading branch information
DrSmile444 authored Dec 3, 2022
2 parents 0f88366 + 36bdcee commit 565c0fe
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/utils/telegram.util.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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}` : ''}`;
}
}

0 comments on commit 565c0fe

Please sign in to comment.