Skip to content

Commit

Permalink
feat: Add kickParticipant method to TelegramClient (#696)
Browse files Browse the repository at this point in the history
  • Loading branch information
darkdeathoriginal authored Jul 28, 2024
1 parent fbe3057 commit d18804b
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
25 changes: 25 additions & 0 deletions gramjs/client/TelegramClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -1550,6 +1574,7 @@ export class TelegramClient extends TelegramBaseClient {
static get events() {
return require("../events");
}


// endregion
}
55 changes: 53 additions & 2 deletions gramjs/client/chats.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)
}

0 comments on commit d18804b

Please sign in to comment.