From 3fe96d4b93c8587c8d33b18db9e658a952ad7b86 Mon Sep 17 00:00:00 2001 From: Anwin Sharon <87226858+darkdeathoriginal@users.noreply.github.com> Date: Mon, 8 Jul 2024 19:14:51 +0530 Subject: [PATCH] feat: Add kickParticipant method to TelegramClient --- gramjs/client/TelegramClient.ts | 25 +++++++++++++++ gramjs/client/chats.ts | 55 +++++++++++++++++++++++++++++++-- 2 files changed, 78 insertions(+), 2 deletions(-) diff --git a/gramjs/client/TelegramClient.ts b/gramjs/client/TelegramClient.ts index c1f09d94..62cbd688 100644 --- a/gramjs/client/TelegramClient.ts +++ b/gramjs/client/TelegramClient.ts @@ -1031,6 +1031,30 @@ export class TelegramClient extends TelegramBaseClient { ) { return chatMethods.getParticipants(this, entity, params); } + /** + * Kicks a user from a chat. + * + * Kicking yourself (`'me'`) will result in leaving the chat. + * + * @note + * Attempting to kick someone who was banned will remove their + * restrictions (and thus unbanning them), since kicking is just + * ban + unban. + * + * @example + * // Kick some user from some chat, and deleting the service message + * const msg = await client.kickParticipant(chat, user); + * await msg.delete(); + * + * // Leaving chat + * await client.kickParticipant(chat, 'me'); + */ + kickParticipant( + entity:EntityLike, + participant:EntityLike + ) { + return chatMethods.kickParticipant(this,entity,participant) + } //endregion @@ -1550,6 +1574,7 @@ export class TelegramClient extends TelegramBaseClient { static get events() { return require("../events"); } + // endregion } diff --git a/gramjs/client/chats.ts b/gramjs/client/chats.ts index e20ff334..b0cb42d4 100644 --- a/gramjs/client/chats.ts +++ b/gramjs/client/chats.ts @@ -1,11 +1,12 @@ import type { TelegramClient } from "./TelegramClient"; import type { EntitiesLike, Entity, EntityLike, ValueOf } from "../define"; -import { sleep, getMinBigInt, TotalList, betterConsoleLog } from "../Helpers"; +import { sleep, getMinBigInt, TotalList, betterConsoleLog, returnBigInt } from "../Helpers"; import { RequestIter } from "../requestIter"; import { helpers, utils } from "../"; import { Api } from "../tl"; -import bigInt, { BigInteger } from "big-integer"; +import bigInt, { BigInteger, isInstance } from "big-integer"; import { inspect } from "../inspect"; +import { getPeerId } from "../Utils"; const _MAX_PARTICIPANTS_CHUNK_SIZE = 200; const _MAX_ADMIN_LOG_CHUNK_SIZE = 100; @@ -441,3 +442,53 @@ export async function getParticipants( const it = client.iterParticipants(entity, params); return (await it.collect()) as TotalList; } + +/** @hidden */ +export async function kickParticipant( + client: TelegramClient, + entity: EntityLike, + participant: EntityLike +) { + const peer = await client.getInputEntity(entity); + const user = await client.getInputEntity(participant); + let resp + let request + + const type = helpers._entityType(peer); + if(type === helpers._EntityType.CHAT){ + request = new Api.messages.DeleteChatUser({ + chatId: returnBigInt(getPeerId(entity)), + userId:returnBigInt(getPeerId(participant)) + }) + resp = await client.invoke(request); + } + else if(type === helpers._EntityType.CHANNEL){ + if(user instanceof Api.InputPeerSelf){ + request = new Api.channels.LeaveChannel({ + channel:peer + }) + resp = await client.invoke(request) + } + else{ + request = new Api.channels.EditBanned({ + channel:peer, + participant:user, + bannedRights: new Api.ChatBannedRights({ + untilDate:0, + viewMessages:true + }) + }) + resp = await client.invoke(request) + await sleep(500) + await client.invoke(new Api.channels.EditBanned({ + channel:peer, + participant:user, + bannedRights:new Api.ChatBannedRights({untilDate:0}) + })) + } + } + else{ + throw new Error("You must pass either a channel or a chat") + } + return client._getResponseMessage(request,resp,entity) +}