-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
83 lines (74 loc) · 2.23 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* @Author: isboyjc
* @Date: 2020-05-31 16:38:19
* @LastEditors: isboyjc
* @LastEditTime: 2020-05-31 19:10:06
* @Description: wechaty plugin 加入房间邀请
*/
const DEFAULT_CONFIG = {
keyword: ["加群"],
reply: "",
roomList: [],
}
module.exports = function WechatyRoomInvite(config = {}) {
config = Object.assign({}, DEFAULT_CONFIG, config)
if (typeof config.keyword === "string") config.keyword = [config.keyword]
return (bot) => {
// 消息监听
bot.on("message", async (msg) => {
if (msg.self()) return
for (let i = 0; i < config.roomList.length; i++) {
if (
config.roomList[i].name == msg.text() ||
config.roomList[i].alias == msg.text()
) {
if (config.roomList[i].close == true) {
msg.say("此群已关闭自动拉取功能")
return
}
await roomInvite(bot, msg, config.roomList[i].roomId)
return
}
}
if (msg.type() === bot.Message.Type.Text && !msg.room()) {
// 校验关键字
if (config.keyword.some((c) => c == msg.text())) {
if (config.roomList.length == 1) {
await roomInvite(bot, msg, config.roomList[0].roomId)
return
}
let info
if (config.reply) {
info = config.reply
} else {
info = `当前${bot.options.name}为小主管理的群聊有${config.roomList.length}个,回复群聊名或编号(【】中为编号)即可加入哦\n\n`
config.roomList.map((item) => {
info += `【${item.alias}】${item.name} (${item.label})\n`
})
}
msg.say(info)
}
}
})
}
}
/**
* @description 房间邀请
* @param {Object} bot 实例对象
* @param {Object} msg 消息实例
* @param {Object} roomId 房间id
* @return {}
*/
async function roomInvite(bot, msg, roomId) {
// 通过群聊id获取到该群聊实例
const room = await bot.Room.find({ id: roomId })
// 判断是否在房间中 在-提示并结束
if (await room.has(msg.from())) {
await msg.say("您已经在房间中了")
return
}
// 发送群邀请
await room.add(msg.from())
await msg.say("已发送群邀请")
return
}