Skip to content

Commit

Permalink
feat: napcat 私聊文件发送
Browse files Browse the repository at this point in the history
  • Loading branch information
clansty committed Dec 29, 2024
1 parent 0cd317a commit 1ce3b40
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
9 changes: 9 additions & 0 deletions main/src/client/NapCatClient/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,15 @@ export class NapCatFriend extends NapCatUser implements Friend {
this.remark = data.remark;
return data;
}

async sendFile(file: string, filename: string): Promise<string> {
await this.client.callApi('upload_private_file', {
user_id: this.uin,
file,
name: filename,
});
return filename;
}
}

class NapCatGFS implements GroupFs {
Expand Down
2 changes: 2 additions & 0 deletions main/src/client/QQClient/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export interface QQUser extends QQEntity {
export interface Friend extends QQUser {
readonly nickname: string;
readonly remark: string;

sendFile(file: string, filename: string): Promise<string>;
}

export interface Group extends QQEntity {
Expand Down
20 changes: 7 additions & 13 deletions main/src/services/ForwardService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -837,11 +837,11 @@ export default class ForwardService {
`类型:${file.mimeType}\n` +
`大小:${file.size}`);
if (file.size.leq(50 * 1024 * 1024)) {
chain.push('\n');
useText('\n');
const file = await createTempFile();
tempFiles.push(file);
useText('文件正在上传中…');
if ('gid' in pair.qq) {
useText('文件正在上传中…');
const file = await createTempFile();
tempFiles.push(file);
await message.downloadMedia({ outputFile: file.path });
pair.qq.fs.upload(file.path, '/',
fileNameAttribute ? fileNameAttribute.fileName : 'file')
Expand All @@ -851,20 +851,14 @@ export default class ForwardService {
})
.finally(() => file.cleanup());
}
else if (pair.qq instanceof OicqFriend) {
useText('文件正在上传中…');
pair.qq.sendFile(await message.downloadMedia({}),
fileNameAttribute ? fileNameAttribute.fileName : 'file')
else {
await message.downloadMedia({ outputFile: file.path });
pair.qq.sendFile(file.path, fileNameAttribute ? fileNameAttribute.fileName : 'file')
.catch(err => {
message.reply({ message: `上传失败:\n${err.message}` });
posthog.capture('上传好友文件失败', { error: err });
});
}
else {
await message.reply({
message: '当前配置不支持好友文件',
});
}
}
brief += '[文件]';
if (env.DISABLE_FILE_UPLOAD_TIP) {
Expand Down

0 comments on commit 1ce3b40

Please sign in to comment.