Files
mol-robot/bot/index.js
2023-12-25 10:58:47 +08:00

63 lines
1.6 KiB
JavaScript

import pkg from 'mirai-js';
import Queue from '#root/utils/queue.js';
import logger from '#root/utils/logger.js';
const { Bot } = pkg;
export default class CreateBot {
constructor() {
this.bot = new Bot();
this.queue = new Queue();
this.running = false;
}
async open(config) {
logger(`开始连接 mirai: host: ${config.baseUrl}, qq: ${config.qq}`);
await this.bot.open({
baseUrl: config.baseUrl,
verifyKey: config.verifyKey,
qq: config.qq,
});
}
sendMessageToFriend(qq, message) {
logger(`发送好友[${qq}]消息进入消息队列`);
return this.queue
.addMethod(this.bot.sendMessage.bind(this.bot, { friend: qq, message }))
.then(
(res) => {
logger(`发送好友[${qq}]消息成功 ${res}`);
return ['success', res];
},
(e) => {
logger.warning(`发送好友[${qq}]消息失败,错误信息${e}`);
return ['failed', e];
},
);
}
sendMessageToGroup(groupId, message) {
logger(`发送群[${groupId}]消息进入消息队列`);
return this.queue
.addMethod(
this.bot.sendMessage.bind(this.bot, { group: groupId, message }),
)
.then(
(res) => {
logger(`发送群[${groupId}]消息成功 ${res}`);
return ['success', res];
},
(e) => {
logger.warning(`发送群[${groupId}]消息失败,错误信息${e}`);
return ['failed', e];
},
);
}
mountPlugin(plugins) {
for (let plugin of plugins) {
plugin(this);
}
}
}