Skip to content

Commit

Permalink
feat: implement util.whois (#3)
Browse files Browse the repository at this point in the history
- Closes #2.

Co-authored-by: Roj <[email protected]>
  • Loading branch information
ArnabXD and rojvv authored Jan 29, 2022
1 parent a281562 commit 6959489
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 5 deletions.
25 changes: 25 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
"big-integer": "^1.6.51",
"dotenv": "^14.3.2",
"envalid": "^7.2.2",
"html-escape": "^2.0.0",
"telegram": "^2.4.3"
},
"devDependencies": {
"@types/html-escape": "^2.0.0",
"@typescript-eslint/eslint-plugin": "^5.10.1",
"@typescript-eslint/parser": "^5.10.1",
"eslint": "^8.7.0",
Expand Down
56 changes: 56 additions & 0 deletions src/modules/util/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { TelegramClient, Api } from 'telegram'
import { Entity } from 'telegram/define'

import escape from 'html-escape'

const kv = (k: string, v: unknown) => `${k}: ${String(v)}\n`

export async function whois(
entity: Entity,
client: TelegramClient,
escape_ = true
) {
let whois = ''
if (entity instanceof Api.Chat) {
const { fullChat } = await client.invoke(
new Api.messages.GetFullChat({ chatId: entity.id })
)
whois += kv('ID', entity.id)
whois += kv(
'DC',
entity.photo instanceof Api.ChatPhoto ? entity.photo.dcId : 'N/A'
)
whois += kv('Name', entity.title)
whois += kv('Membership', entity.left ? 'Not member' : 'Member')
whois += '\n' + fullChat.about ?? ''
} else if (entity instanceof Api.Channel) {
const { fullChat } = await client.invoke(
new Api.channels.GetFullChannel({ channel: entity.id })
)
whois += kv('ID', entity.id)
whois += kv(
'DC',
entity.photo instanceof Api.ChatPhoto ? entity.photo.dcId : 'N/A'
)
whois += kv('Name', entity.title)
whois += kv('Username', entity.username)
whois += kv('Membership', entity.left ? 'Not member' : 'Member')
whois == '\n' + fullChat.about ?? ''
} else if (entity instanceof Api.User) {
const { fullUser } = await client.invoke(
new Api.users.GetFullUser({ id: entity.id })
)
whois += kv('ID', entity.id)
whois += kv(
'DC',
entity.photo instanceof Api.UserProfilePhoto ? entity.photo.dcId : 'N/A'
)
whois += kv('Name', entity.firstName + ' ' + (entity.lastName ?? ''))
whois += kv('Username', entity.username)
whois += '\n' + fullUser.about
} else {
whois += 'Could not resolve whois'
}
if (escape_) whois = escape(whois)
return whois
}
47 changes: 42 additions & 5 deletions src/modules/util.ts → src/modules/util/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { exec, spawn } from 'child_process'

import { zero } from 'big-integer'
import { Api, version as telegramVersion } from 'telegram'
import { Api, TelegramClient, version as telegramVersion } from 'telegram'

import { Module } from '../module'
import { version } from '../constants'
import { CommandHandler } from '../handlers'
import escape from 'html-escape'

import { Module } from '../../module'
import { version } from '../../constants'
import { CommandHandler } from '../../handlers'
import { EntityLike } from 'telegram/define'
import { whois } from './helpers'

const util: Module = {
handlers: [
Expand Down Expand Up @@ -103,7 +107,40 @@ const util: Module = {
})
},
['v']
)
),
new CommandHandler('whois', async (client, event, args) => {
let info = ''
if (args[0] !== undefined && args[0].length != 0) {
const entity = await client.getEntity(args[0])
info += (await whois(entity, client)).trim() + '\n\n'
}
await event.getInputChat() // https://t.me/gramjschat/25585
const chat = await event.getChat()
if (chat) {
info += '<b>Here</b>' + '\n'
info += (await whois(chat, client)).trim() + '\n\n'
}
const reply = await event.message.getReplyMessage()
if (reply) {
const sender = await reply.getSender()
if (sender) {
info += '<b>Reply</b>' + '\n'
info += (await whois(sender, client)).trim() + '\n\n'
}
if (reply.forward) {
const sender = await reply.forward.getSender()
if (sender) {
info += '<b>Forwarder</b>' + '\n'
info += (await whois(sender, client)).trim() + '\n\n'
}
}
}
if (info.length == 0) return
await event.message.edit({
text: event.message.text + '\n\n' + info,
parseMode: 'html'
})
})
]
}

Expand Down

0 comments on commit 6959489

Please sign in to comment.