From df7aeed6733503efc1468df570bda511d3c576c8 Mon Sep 17 00:00:00 2001 From: xleat Date: Tue, 17 Sep 2024 21:47:45 +0800 Subject: [PATCH] Add: #38 add command `/rcc` update contact --- src/client/TelegramBotClient.ts | 64 +++++++++++++++++++++++++++++++-- src/client/WechatClient.ts | 10 ++++++ src/i18n/locales/en.ts | 2 ++ src/i18n/locales/zh.ts | 2 ++ src/service/BindItemService.ts | 2 +- 5 files changed, 76 insertions(+), 4 deletions(-) diff --git a/src/client/TelegramBotClient.ts b/src/client/TelegramBotClient.ts index 49cd4e2..bf02209 100644 --- a/src/client/TelegramBotClient.ts +++ b/src/client/TelegramBotClient.ts @@ -300,19 +300,20 @@ export class TelegramBotClient extends BaseClient { {command: 'help', description: this.t('command.description.help')}, {command: 'start', description: this.t('command.description.start')}, {command: 'login', description: this.t('command.description.login')}, + {command: 'lang', description: this.t('command.description.lang')}, {command: 'user', description: this.t('command.description.user')}, {command: 'room', description: this.t('command.description.room')}, {command: 'recent', description: this.t('command.description.recent')}, {command: 'settings', description: this.t('command.description.settings')}, - {command: 'check', description: this.t('command.description.check')}, {command: 'bind', description: this.t('command.description.bind')}, {command: 'unbind', description: this.t('command.description.unbind')}, - {command: 'gs', description: this.t('command.description.gs')}, {command: 'order', description: this.t('command.description.order')}, {command: 'cgdata', description: this.t('command.description.cgdata')}, + {command: 'gs', description: this.t('command.description.gs')}, {command: 'reset', description: this.t('command.description.reset')}, + {command: 'rcc', description: this.t('command.description.rcc')}, {command: 'stop', description: this.t('command.description.stop')}, - {command: 'lang', description: this.t('command.description.lang')}, + {command: 'check', description: this.t('command.description.check')}, ] if (config.API_ID && config.API_HASH) { // 启动tg client @@ -630,6 +631,63 @@ export class TelegramBotClient extends BaseClient { bot.command('stop', this.onWeChatStop) + // 重新加载所有联系人 + bot.command('rcc', async ctx => { + await ctx.reply(this.t('wechat.loadingMembers')) + if (ctx.chat && ctx.chat.type.includes('group')) { + this.bindItemService.getBindItemByChatId(ctx.chat.id).then(bindItem => { + const wechatId = bindItem.wechat_id + this.weChatClient.client.Contact.find({id: wechatId}).then(async contact => { + await contact?.sync() + const copyBindItem = {...bindItem} + copyBindItem.name = contact?.name() + copyBindItem.alias = await contact?.alias() + copyBindItem.avatar = contact?.payload.avatar + await this.bindItemService.updateGroupData(bindItem, copyBindItem) + }) + }).catch(() => { + ctx.reply(this.t('common.notBind')) + }) + } else { // in the bot chat + const updateBindItem = async (contact: ContactInterface) => { + this.bindItemService.getBindItemByWechatId(contact.id).then(async bindItem => { + if (bindItem.chat_id) { + const copyBindItem = {...bindItem} + copyBindItem.name = contact?.name() + copyBindItem.alias = await contact?.alias() + copyBindItem.avatar = contact?.payload.avatar + await this.bindItemService.updateGroupData(bindItem, copyBindItem) + } else { + this.logWarn('update bind item failed, chat id is null', bindItem.name) + } + }) + } + if (ctx.args.length > 0) { + ctx.args.forEach(name => { + this.weChatClient.client.Contact.findAll({name: name}).then(contacts => { + contacts.filter(it => it.name() && it.friend()).forEach(async contact => { + await contact?.sync() + await updateBindItem(contact) + }) + }) + }) + } else { + this.weChatClient.client.Contact.findAll().then(contacts => { + contacts.filter(it => it.name() && it.friend()).forEach(async fc => { + await fc?.sync() + await updateBindItem(fc) + }) + }) + } + } + // update cache member info + this.weChatClient.reloadContactCache().then(() => { + ctx.reply(this.t('wechat.contactFinished')) + }).catch(() => { + ctx.reply(this.t('wechat.contactFailed')) + }) + }) + bot.command('check', ctx => { if (this.wechatStartFlag && this._weChatClient.client.isLoggedIn) { ctx.reply(this.t('command.check.onLine')) diff --git a/src/client/WechatClient.ts b/src/client/WechatClient.ts index b5b85c7..f6c1c68 100644 --- a/src/client/WechatClient.ts +++ b/src/client/WechatClient.ts @@ -1017,6 +1017,16 @@ export class WeChatClient extends BaseClient { this.tgClient.reset() } + public async reloadContactCache () { + this._contactMap = new Map>([ + [0, new Set()], + [1, new Set()], + [2, new Set()], + [3, new Set()] + ]) + return this.cacheMember() + } + private clearCache() { return new Promise(resolve => { const filePath = 'storage/wechat_bot.memory-card.json' diff --git a/src/i18n/locales/en.ts b/src/i18n/locales/en.ts index ea46159..85c1217 100644 --- a/src/i18n/locales/en.ts +++ b/src/i18n/locales/en.ts @@ -14,6 +14,7 @@ const en = { gs: 'Forward group messages', order: 'Set up public account shortcuts', cgdata: 'Set group avatar and name (requires admin permissions)', + rcc: 'Reload current contacts (update currently bind user in group chat, bot can add names separated by spaces; if not, update all users ⚠️ may be slow)', reset: 'Clear cache and re-login', stop: 'Stop WeChat client, requires re-login', autocg: 'Automatic group creation mode, requires configuring the API and logging into the Telegram User Client', @@ -187,6 +188,7 @@ const en = { setMsg: 'Forwarding combined messages', }, contactFinished: 'Contacts loaded', + contactFailed: 'Failed to load contacts', fileReceivingFailed: 'Failed to receive file', receivingFile: 'Receiving file...Please wait', }, diff --git a/src/i18n/locales/zh.ts b/src/i18n/locales/zh.ts index f98d1bd..aa571c7 100644 --- a/src/i18n/locales/zh.ts +++ b/src/i18n/locales/zh.ts @@ -14,6 +14,7 @@ const zh = { gs: '群组消息转发', order: '设置公众号快捷指令', cgdata: '设置群组的头像和名称(需要管理员权限)', + rcc: '重新加载当前联系人(群聊中更新当前绑定的用户,bot中可以加 名字 空格隔开;如没有就更新所有用户 ⚠️ 可能会很慢)', reset: '清空缓存重新登陆', stop: '停止微信客户端, 需要重新登陆', autocg: '自动创建群组模式, 需要配置Api并且登陆Telegram User Client', @@ -187,6 +188,7 @@ const zh = { setMsg: '合并转发消息', }, contactFinished: '联系人加载完成', + contactFailed: '联系人加载失败', fileReceivingFailed: '接收文件失败', receivingFile: '文件接收中...请稍候', }, diff --git a/src/service/BindItemService.ts b/src/service/BindItemService.ts index 257cce0..905946a 100644 --- a/src/service/BindItemService.ts +++ b/src/service/BindItemService.ts @@ -200,7 +200,7 @@ export class BindItemService extends AbstractSqlService { public updateBindItem(groupId: string, bindItem: BindItem) { this.db.serialize(() => { let query = 'UPDATE tb_bind_item SET ' - let params = [] + const params = [] let first = true if (bindItem.name) {