Skip to content
This repository has been archived by the owner on Mar 24, 2024. It is now read-only.

Commit

Permalink
fix: quit when sending failded
Browse files Browse the repository at this point in the history
  • Loading branch information
Cnily03 committed Sep 27, 2023
1 parent d1e60d4 commit 0f06f41
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "oicq-sdk",
"version": "0.2.5",
"version": "0.2.7",
"description": "Offer SDK to build a QQ bot more easily based on `oicq`",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
102 changes: 95 additions & 7 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ export class Bot {
// Generate CLIENT
this.CLIENT = Bot.createClient(config);
this.status.client.exist = true;
// Fix ICQQClient
Bot.fixICQQClient();
// Add Event to CLIENT
this.CLIENT.on("system.online", function () {
that.status.client.logged = true;
Expand Down Expand Up @@ -186,17 +188,32 @@ export class Bot {
}
// register(event_name, entry: EventEntry<Bot, typeof event_name>, response: EventResponse<Bot, typeof event_name>)
if (isEntryLegal(entry) && isResponseLegal(response)) {
var listner: (...args: any[]) => any;
let listner: (...args: any[]) => any;
once ? this.CLIENT.on(event_name, listner = (...args: any) => {
if (entry.call(that, ...args)) response.call(that, ...args), that.CLIENT.off(event_name); // TODO: no listener?
}) : this.CLIENT.on(event_name, (...args: any) => {
if (entry.call(that, ...args)) response.call(that, ...args);
if (entry.call(that, ...args)) {
try {
that.CLIENT.off(event_name) // TODO: no listener provided?
return response.call(that, ...args)
} catch (e) { console.error(e) }
}
})
: this.CLIENT.on(event_name, (...args: any) => {
if (entry.call(that, ...args)) {
try { return response.call(that, ...args) }
catch (e) { console.error(e) }
}
});
} else {
// register(event_name, response: EventResponse<Bot, typeof event_name>)
const response: EventResponse<Bot, T> = entry;
once ? this.CLIENT.once(event_name, (...args: any) => response.call(that, ...args))
: this.CLIENT.on(event_name, (...args: any) => response.call(that, ...args));
once ? this.CLIENT.once(event_name, (...args: any) => {
try { return response.call(that, ...args) }
catch (e) { console.error(e) }
})
: this.CLIENT.on(event_name, (...args: any) => {
try { return response.call(that, ...args) }
catch (e) { console.error(e) }
});
}
}

Expand All @@ -222,7 +239,9 @@ export class Bot {
if (typeof response != "function") { // response is Sendable
const _response: oicq.Sendable = response;
return function (event: Parameters<EventMap<Bot>[T]>[0]): void {
event.reply(_response);
try {
event.reply(_response);
} catch (e) { console.error(e) }
}
}
else return response; // response is Function
Expand All @@ -240,6 +259,75 @@ export class Bot {
this.registerMsg<T>(entry, response, true);
}

private static _icqq_client_fixed = false;
/**
* 修复 ICQQClient 发送消息失败时退出的错误
*/
private static fixICQQClient() {
if (Bot._icqq_client_fixed) return;
const ori_oicq_group_sendMsg = oicq.Group.prototype.sendMsg;
oicq.Group.prototype.sendMsg = async (...args: Parameters<typeof oicq.Group.prototype.sendMsg>): ReturnType<typeof oicq.Group.prototype.sendMsg> => {
return await ori_oicq_group_sendMsg(...args).catch(e => {
console.error(e)
return {
message_id: "",
seq: 0,
rand: 0,
time: 0
}
})
}
const ori_oicq_discuss_sendMsg = oicq.Discuss.prototype.sendMsg;
oicq.Discuss.prototype.sendMsg = async (...args: Parameters<typeof oicq.Discuss.prototype.sendMsg>): ReturnType<typeof oicq.Discuss.prototype.sendMsg> => {
return await ori_oicq_discuss_sendMsg(...args).catch(e => {
console.error(e)
return {
message_id: "",
seq: 0,
rand: 0,
time: 0
}
})
}
const ori_oicq_friend_sendMsg = oicq.Friend.prototype.sendMsg;
oicq.Friend.prototype.sendMsg = async (...args: Parameters<typeof oicq.Friend.prototype.sendMsg>): ReturnType<typeof oicq.Friend.prototype.sendMsg> => {
return await ori_oicq_friend_sendMsg(...args).catch(e => {
console.error(e)
return {
message_id: "",
seq: 0,
rand: 0,
time: 0
}
})
}
const ori_oicq_channel_sendMsg = oicq.Channel.prototype.sendMsg;
oicq.Channel.prototype.sendMsg = async (...args: Parameters<typeof oicq.Channel.prototype.sendMsg>): ReturnType<typeof oicq.Channel.prototype.sendMsg> => {
return await ori_oicq_channel_sendMsg(...args).catch(e => {
console.error(e)
return {
message_id: "",
seq: 0,
rand: 0,
time: 0
}
})
}
const ori_oicq_user_sendMsg = oicq.User.prototype.sendMsg;
oicq.User.prototype.sendMsg = async (...args: Parameters<typeof oicq.User.prototype.sendMsg>): ReturnType<typeof oicq.User.prototype.sendMsg> => {
return await ori_oicq_user_sendMsg(...args).catch(e => {
console.error(e)
return {
message_id: "",
seq: 0,
rand: 0,
time: 0
}
})
}
Bot._icqq_client_fixed = true;
}

/**
* 验证账号是否合法
* @param account QQ acount to login
Expand Down

0 comments on commit 0f06f41

Please sign in to comment.