telegram-mcp/cli/src/commands/chat/leave.ts
2026-11-05 14:26:28 +03:00

50 lines
1.5 KiB
TypeScript

import type { Command } from 'commander';
import { Api } from 'teleproto';
import { withAuth } from '../../lib/with-auth.js';
import { outputSuccess, outputError } from '../../lib/output.js';
import { resolveEntity } from '../../lib/peer.js';
import { markedPeerId } from '../../lib/serialize.js';
import type { GlobalOptions } from '../../lib/types.js';
import { ErrorCode } from '../../lib/error-codes.js';
/**
* Action handler for `tg chat leave <chat>`.
*
* Leaves a group or channel.
* - Channel/Supergroup: uses LeaveChannel
* - Basic group: uses DeleteChatUser with self as user
*/
export async function chatLeaveAction(this: Command, chatInput: string): Promise<void> {
const opts = this.optsWithGlobals() as GlobalOptions;
await withAuth(opts, async (client) => {
const entity = await resolveEntity(client, chatInput);
if (entity instanceof Api.Channel) {
// Channel or supergroup
await client.invoke(
new Api.channels.LeaveChannel({ channel: entity }),
);
} else if (entity instanceof Api.Chat) {
// Basic group: remove self
const me = await client.getMe();
await client.invoke(
new Api.messages.DeleteChatUser({
chatId: entity.id,
userId: (me as any).id,
}),
);
} else {
outputError('Cannot leave a user chat', ErrorCode.INVALID_CHAT_TYPE);
return;
}
outputSuccess({
left: true,
chat: {
id: markedPeerId(entity),
title: (entity as any).title ?? '',
},
});
});
}