/
index.js
92 lines (83 loc) · 2.58 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
84
85
86
87
88
89
90
91
92
'use strict'
const fs = require('fs')
const Wechat = require('wechat4u')
const qrcode = require('qrcode-terminal')
const emoticons = require('./emoticons.json')
let bot = new Wechat()
let users = new Set()
let emoticonSet = new Set()
// 初始化emoticonSet,避免重复记录表情
try {
emoticons.forEach(emoticon => emoticonSet.add(emoticon.md5))
fs.readFileSync('./new-emoticons.dat', 'utf8').split('\n').forEach(emoticon => emoticonSet.add(
emoticon.split(' ')[0]))
} catch (err) {
console.log(err)
}
bot.on('error', err => {
console.log(err)
})
bot.on('uuid', uuid => {
// uuid事件,获取二维码
console.log('扫码登录')
qrcode.generate('https://login.weixin.qq.com/l/' + uuid, {
small: true
})
console.log('如果二维码不能识别,请打开链接')
console.log('https://login.weixin.qq.com/qrcode/' + uuid)
})
bot.on('login', () => {
console.log('登录成功')
console.log('选择你要轰炸的对象,直接在手机上向TA发送"科科",取消发送"拜拜"')
})
bot.on('logout', () => {
console.log('登出成功')
})
bot.on('message', msg => {
switch (msg.MsgType) {
case bot.CONF.MSGTYPE_TEXT:
// 文本消息
// 自定义命令
if (msg.Content == '科科' && msg.isSendBy(bot.user)) {
users.add(msg.ToUserName)
sendEmoticon(msg.ToUserName)
console.log('开始轰炸', bot.contacts[msg.ToUserName].getDisplayName())
} else if (msg.Content == '拜拜' && msg.isSendBy(bot.user)) {
users.delete(msg.ToUserName)
console.log('取消轰炸', bot.contacts[msg.ToUserName].getDisplayName())
}
break
case bot.CONF.MSGTYPE_EMOTICON:
// 新表情数据写入new-emoticons.dat,去重
let pm = msg.Content.match(/md5\s?=\s?"(.*?)".*?cdnurl\s?=\s?"(.*?)"/)
if (pm && !emoticonSet.has(pm[1])) {
console.log('新表情GET')
try {
fs.appendFileSync('./new-emoticons.dat', `${pm[1]} ${pm[2]}\n`)
emoticonSet.add(pm[1])
} catch (err) {
console.log(err)
}
}
break
default:
break
}
})
bot.start()
const sendEmoticon = (ToUserName, index) => {
index = index || 0 // 自定义
if (users.has(ToUserName) && index < emoticons.length) {
bot.sendEmoticon(emoticons[index].md5, ToUserName)
.catch(err => {
console.log(err)
return
}).then(() => {
setTimeout(() => {
sendEmoticon(ToUserName, ++index)
}, 1000) // 自定义间隔
})
} else {
console.log(`已轰炸 ${bot.contacts[ToUserName].getDisplayName()} ${index}个表情`)
}
}