perf: 方法添加对超级群的支持,新的关联默认创建超级群

(不然升级的时候以及撤回消息都会有问题
This commit is contained in:
Clansty 2022-03-05 20:08:55 +08:00
parent 729d5f7357
commit 5d498f1a59
No known key found for this signature in database
GPG Key ID: 05F8479BA63A8E92
3 changed files with 85 additions and 28 deletions

View File

@ -140,8 +140,11 @@ export default class Telegram {
return await this.client.invoke(new Api.messages.UpdateDialogFilter(params));
}
public async createChat(params: Partial<Partial<{ users: EntityLike[]; title: string; }>>) {
const updates = await this.client.invoke(new Api.messages.CreateChat(params)) as Api.Updates;
public async createChat(title: string, about?: string) {
const updates = await this.client.invoke(new Api.channels.CreateChannel({
title, about,
megagroup: true,
})) as Api.Updates;
const newChat = updates.chats[0];
return new TelegramChat(this, this.client, newChat, this.waitForMessageHelper);
}

View File

@ -61,29 +61,68 @@ export default class TelegramChat {
public async setProfilePhoto(photo: Buffer) {
if (!(this.entity instanceof Api.Chat || this.entity instanceof Api.Channel))
throw new Error('不是群组,无法设置头像');
return await this.client.invoke(
new Api.messages.EditChatPhoto({
chatId: this.id,
photo: new Api.InputChatUploadedPhoto({
file: await this.client.uploadFile({
file: new CustomFile('photo.jpg', photo.length, '', photo),
workers: 1,
if (this.entity instanceof Api.Chat) {
return await this.client.invoke(
new Api.messages.EditChatPhoto({
chatId: this.id,
photo: new Api.InputChatUploadedPhoto({
file: await this.client.uploadFile({
file: new CustomFile('photo.jpg', photo.length, '', photo),
workers: 1,
}),
}),
}),
}),
);
);
}
else {
return await this.client.invoke(
new Api.channels.EditPhoto({
channel: this.entity,
photo: new Api.InputChatUploadedPhoto({
file: await this.client.uploadFile({
file: new CustomFile('photo.jpg', photo.length, '', photo),
workers: 1,
}),
}),
}),
);
}
}
public async editAdmin(user: EntityLike, isAdmin: boolean) {
if (!(this.entity instanceof Api.Chat || this.entity instanceof Api.Channel))
throw new Error('不是群组,无法设置管理员');
return await this.client.invoke(
new Api.messages.EditChatAdmin({
chatId: this.id,
userId: user,
isAdmin,
}),
);
if (this.entity instanceof Api.Chat) {
return await this.client.invoke(
new Api.messages.EditChatAdmin({
chatId: this.id,
userId: user,
isAdmin,
}),
);
}
else {
return await this.client.invoke(
new Api.channels.EditAdmin({
channel: this.entity,
userId: user,
adminRights: new Api.ChatAdminRights({
changeInfo: true,
postMessages: true,
editMessages: true,
deleteMessages: true,
banUsers: true,
inviteUsers: true,
pinMessages: true,
addAdmins: true,
anonymous: true,
manageCall: true,
other: true,
}),
rank: '转发姬',
}),
);
}
}
public async editAbout(about: string) {
@ -129,6 +168,8 @@ export default class TelegramChat {
}
public async getMember(user: EntityLike) {
if (!(this.entity instanceof Api.Channel))
throw new Error('不是超级群,无法获取成员信息');
return await this.client.invoke(
new Api.channels.GetParticipant({
channel: this.entity,
@ -143,4 +184,16 @@ export default class TelegramChat {
}
return await this.client.deleteMessages(this.entity, messageId, { revoke: true });
}
public async inviteMember(member: EntityLike | EntityLike[]) {
if (!Array.isArray(member)) {
member = [member];
}
return await this.client.invoke(
new Api.channels.InviteToChannel({
channel: this.entity,
users: member,
}),
);
}
}

View File

@ -132,22 +132,26 @@ export default class ConfigService {
try {
// 状态信息
if (status === true) {
status = await (await this.owner).sendMessage('正在创建 Telegram 群…');
const avatar = await getAvatar(roomId);
status = await (await this.owner).sendMessage({
message: '正在创建 Telegram 群…',
file: new CustomFile('avatar.png', avatar.length, '', avatar),
});
}
else if (status instanceof Api.Message) {
await status.edit({ text: '正在创建 Telegram 群…', buttons: Button.clear() });
}
// 创建群聊,拿到的是 user 的 chat
const chat = await this.tgUser.createChat({
title,
users: [this.tgBot.me.username],
});
const chat = await this.tgUser.createChat(title, await this.getAboutText(qEntity));
// 添加机器人
status && await status.edit({ text: '正在添加机器人…' });
await chat.inviteMember(this.tgBot.me.id);
// 设置管理员
status && await status.edit({ text: '正在设置管理员…' });
await chat.editAdmin(this.tgBot.me.username, true);
const chatForBot = await this.tgBot.getChat(chat.id);
// 添加到 Filter
status && await status.edit({ text: '正在将群添加到文件夹…' });
@ -172,6 +176,7 @@ export default class ConfigService {
}
// 关联写入数据库
const chatForBot = await this.tgBot.getChat(chat.id);
status && await status.edit({ text: '正在写数据库…' });
const dbPair = await forwardPairs.add(qEntity, chatForBot);
isFinish = true;
@ -185,10 +190,6 @@ export default class ConfigService {
data: { forwardPairId: dbPair.id, hash: avatarHash },
});
// 更新关于文本
status && await status.edit({ text: '正在更新关于文本…' });
await chatForBot.editAbout(await this.getAboutText(qEntity));
// 完成
if (status) {
await status.edit({ text: '正在获取链接…' });