Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add kickParticipant method to TelegramClient #696

Merged
merged 1 commit into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Loading