Skip to content

Commit

Permalink
feat: 刷新所有头像
Browse files Browse the repository at this point in the history
  • Loading branch information
clansty committed Dec 29, 2024
1 parent 9e9c1a3 commit f12e33b
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 1 deletion.
4 changes: 4 additions & 0 deletions main/src/constants/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ const personalPrivateCommands = [
command: 'addgroup',
description: '添加新的群转发',
}),
new Api.BotCommand({
command: 'refresh_all',
description: '刷新所有头像和简介',
}),
];

// 服务器零号实例的管理员
Expand Down
3 changes: 3 additions & 0 deletions main/src/controllers/ConfigController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ export default class ConfigController {
await this.oicq.oicq.login();
}
return true;
case '/refresh_all':
await this.configService.refreshAll();
return true;
}
}
else {
Expand Down
4 changes: 4 additions & 0 deletions main/src/models/ForwardPairs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,8 @@ export default class ForwardPairs {
}
}
}

public getAll() {
return this.pairs;
}
}
19 changes: 18 additions & 1 deletion main/src/models/Pair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import getAboutText from '../utils/getAboutText';
import { md5 } from '../utils/hashing';
import { getAvatar } from '../utils/urls';
import db from './db';
import flags from '../constants/flags';
import { NapCatGroup } from '../client/NapCatClient';

const log = getLogger('ForwardPair');

Expand Down Expand Up @@ -48,6 +50,22 @@ export class Pair {
const lastHash = avatarCache ? avatarCache.hash : null;
const avatar = await getAvatar(this.qqRoomId);
const newHash = md5(avatar);
try {
await this._tg.editAbout(await getAboutText(this.qq, false));
}
catch (e) {
log.error(`修改群简介失败: ${e.message}`);
}
if (!(this.flags & flags.NAME_LOCKED) && this.qq instanceof NapCatGroup) {
const info = await this.qq.renew();
try {
await this._tg.editTitle(info.group_name);
}
catch (e) {
log.error(`修改群名失败: ${e.message}`);
}
}

if (!lastHash || Buffer.from(lastHash).compare(newHash) !== 0) {
log.debug(`更新群头像: ${this.qqRoomId}`);
await this._tg.setProfilePhoto(avatar);
Expand All @@ -57,7 +75,6 @@ export class Pair {
create: { forwardPairId: this.dbId, hash: newHash },
});
}
await this._tg.editAbout(await getAboutText(this.qq, false));
}

get qqRoomId() {
Expand Down
28 changes: 28 additions & 0 deletions main/src/services/ConfigService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,4 +324,32 @@ export default class ConfigService {
}
}
}

public async refreshAll() {
const statusMessage = await (await this.owner).sendMessage('正在刷新所有头像和简介…');
const pairs = this.instance.forwardPairs.getAll();
let succ = 0, fail = 0;
for (let i = 0; i < pairs.length; i++) {
const pair = pairs[i];
try {
await pair.updateInfo();
succ++;
}
catch (e) {
this.log.error(`刷新 ${pair.dbId} 头像和简介失败`, e);
posthog.capture('刷新头像和简介失败', { error: e });
fail++;
}
try{
await statusMessage.edit({
text: `正在刷新所有头像和简介…\n成功:${succ},失败:${fail},总数:${pairs.length}`,
});
}
catch {
}
}
await statusMessage.edit({
text: `刷新完成\n成功:${succ},失败:${fail},总数:${pairs.length}`,
});
}
}

0 comments on commit f12e33b

Please sign in to comment.