Browse Source

feat: Add kickParticipant method to TelegramClient (#696)

Anwin Sharon 9 tháng trước cách đây
mục cha
commit
d18804bcc2
2 tập tin đã thay đổi với 78 bổ sung2 xóa
  1. 25 0
      gramjs/client/TelegramClient.ts
  2. 53 2
      gramjs/client/chats.ts

+ 25 - 0
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
 }

+ 53 - 2
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<Api.User>;
 }
+
+/** @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)
+}