Skip to content

Commit

Permalink
feat: napcat 登录
Browse files Browse the repository at this point in the history
  • Loading branch information
clansty committed Jul 12, 2024
1 parent fd8cae8 commit 6ce58f6
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 25 deletions.
6 changes: 3 additions & 3 deletions main/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ enum QqBotType {

model QqBot {
id Int @id @default(autoincrement())
uin BigInt @default(0)
password String @default("")
platform Int @default(0)
uin BigInt? @default(0)
password String? @default("")
platform Int? @default(0)
Instance Instance[]
signApi String?
signVer String?
Expand Down
4 changes: 2 additions & 2 deletions main/src/client/NapCatClient/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ export class NapCatClient extends QQClient {
const echo = `${new Date().getTime()}${random.int(100000, 999999)}`;
this.echoMap[echo] = { resolve, reject };
this.ws.send(JSON.stringify({ action, params, echo }));
this.logger.trace('send', JSON.stringify({ action, params, echo }));
this.logger.debug('send', JSON.stringify({ action, params, echo }));
});
}

private async handleWebSocketMessage(message: string) {
this.logger.trace('receive', message);
this.logger.debug('receive', message);
const data = JSON.parse(message) as WSReceiveHandler[keyof WSReceiveHandler] & { echo: string; status: 'ok' | 'error'; data: any; message: string };
if (data.echo) {
const promise = this.echoMap[data.echo];
Expand Down
2 changes: 1 addition & 1 deletion main/src/client/OicqClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { escapeXml, gzip, timestamp } from '@icqqjs/icqq/lib/common';
import { pb } from '@icqqjs/icqq/lib/core';
import env from '../models/env';
import {
CreateQQClientParamsBase, Friend, FriendIncreaseEvent,
CreateQQClientParamsBase, Friend, FriendIncreaseEvent, Group,
GroupMemberDecreaseEvent,
GroupMemberIncreaseEvent,
MessageEvent, MessageRecallEvent, PokeEvent,
Expand Down
3 changes: 2 additions & 1 deletion main/src/controllers/SetupController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export default class SetupController {
if (this.instance.qq) {
await this.setupService.informOwner('正在登录已设置好的 QQ');
this.oicq = await QQClient.create({
type: 'oicq',
type: this.instance.qq.type,
id: this.instance.qq.id,
uin: Number(this.instance.qq.uin),
password: this.instance.qq.password,
Expand All @@ -94,6 +94,7 @@ export default class SetupController {
'请使用<a href="https://github.com/mzdluo123/TxCaptchaHelper/releases">此软件</a>验证并输入 Ticket',
);
},
wsUrl: this.instance.qq.wsUrl,
});
}
else
Expand Down
37 changes: 19 additions & 18 deletions main/src/models/Instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,23 +91,23 @@ export default class Instance {
this._flags = dbEntry.flags;
}

private async init(botToken?: string) {
this.log.debug('正在登录 TG Bot');
if (this.botSessionId) {
this.tgBot = await Telegram.connect(this._botSessionId);
}
else {
const token = this.id === 0 ? env.TG_BOT_TOKEN : botToken;
if (!token) {
throw new Error('botToken 未指定');
}
this.tgBot = await Telegram.create({
botAuthToken: token,
});
this.botSessionId = this.tgBot.sessionId;
}
this.log.info('TG Bot 登录完成');
private init(botToken?: string) {
(async () => {
this.log.debug('正在登录 TG Bot');
if (this.botSessionId) {
this.tgBot = await Telegram.connect(this._botSessionId);
}
else {
const token = this.id === 0 ? env.TG_BOT_TOKEN : botToken;
if (!token) {
throw new Error('botToken 未指定');
}
this.tgBot = await Telegram.create({
botAuthToken: token,
});
this.botSessionId = this.tgBot.sessionId;
}
this.log.info('TG Bot 登录完成');
if (!this.isSetup || !this._owner) {
this.log.info('当前服务器未配置,请向 Bot 发送 /setup 来设置');
this.setupController = new SetupController(this, this.tgBot);
Expand All @@ -122,7 +122,7 @@ export default class Instance {
this._ownerChat = await this.tgBot.getChat(this.owner);
this.log.debug('正在登录 OICQ');
this.oicq = await QQClient.create({
type: 'oicq',
type: this.qq.type,
id: this.qq.id,
uin: Number(this.qq.uin),
password: this.qq.password,
Expand All @@ -138,6 +138,7 @@ export default class Instance {
'请使用<a href="https://github.com/mzdluo123/TxCaptchaHelper/releases">此软件</a>验证并输入 Ticket',
);
},
wsUrl: this.qq.wsUrl,
});
this.log.info('OICQ 登录完成');
}
Expand Down Expand Up @@ -170,7 +171,7 @@ export default class Instance {

public async login(botToken?: string) {
await this.load();
await this.init(botToken);
this.init(botToken);
}

public static async start(instanceId: number, botToken?: string) {
Expand Down

0 comments on commit 6ce58f6

Please sign in to comment.