Skip to content

Commit

Permalink
Add: #38 add command /rcc update contact
Browse files Browse the repository at this point in the history
  • Loading branch information
xleat committed Sep 17, 2024
1 parent c8e05d5 commit df7aeed
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 4 deletions.
64 changes: 61 additions & 3 deletions src/client/TelegramBotClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'))
Expand Down
10 changes: 10 additions & 0 deletions src/client/WechatClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,16 @@ export class WeChatClient extends BaseClient {
this.tgClient.reset()
}

public async reloadContactCache () {
this._contactMap = new Map<number, Set<ContactItem>>([
[0, new Set<ContactItem>()],
[1, new Set<ContactItem>()],
[2, new Set<ContactItem>()],
[3, new Set<ContactItem>()]
])
return this.cacheMember()
}

private clearCache() {
return new Promise(resolve => {
const filePath = 'storage/wechat_bot.memory-card.json'
Expand Down
2 changes: 2 additions & 0 deletions src/i18n/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
},
Expand Down
2 changes: 2 additions & 0 deletions src/i18n/locales/zh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const zh = {
gs: '群组消息转发',
order: '设置公众号快捷指令',
cgdata: '设置群组的头像和名称(需要管理员权限)',
rcc: '重新加载当前联系人(群聊中更新当前绑定的用户,bot中可以加 名字 空格隔开;如没有就更新所有用户 ⚠️ 可能会很慢)',
reset: '清空缓存重新登陆',
stop: '停止微信客户端, 需要重新登陆',
autocg: '自动创建群组模式, 需要配置Api并且登陆Telegram User Client',
Expand Down Expand Up @@ -187,6 +188,7 @@ const zh = {
setMsg: '合并转发消息',
},
contactFinished: '联系人加载完成',
contactFailed: '联系人加载失败',
fileReceivingFailed: '接收文件失败',
receivingFile: '文件接收中...请稍候',
},
Expand Down
2 changes: 1 addition & 1 deletion src/service/BindItemService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit df7aeed

Please sign in to comment.