-
Notifications
You must be signed in to change notification settings - Fork 825
/
Copy pathanonymous_chat.js
58 lines (55 loc) · 2.45 KB
/
anonymous_chat.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
const { MessageType } = require("@adiwajshing/baileys")
async function handler(m, { command, usedPrefix }) {
if (!global.db.data.settings.anon) throw `This feature is not active`
command = command.toLowerCase()
this.anonymous = this.anonymous ? this.anonymous : {}
switch (command) {
case 'next':
case 'leave': {
let room = Object.values(this.anonymous).find(room => room.check(m.sender))
if (!room) {
await this.sendButton(m.chat, '_You are not in anonymous chat_', watermark, 'Find Partner', `${usedPrefix}start`)
throw false
}
m.reply('_Ok_')
let other = room.other(m.sender)
if (other) await this.sendButton(other, '_Partner left chat_', watermark, 'Find Partner', `${usedPrefix}start`)
delete this.anonymous[room.id]
if (command === 'leave') break
}
case 'start': {
if (Object.values(this.anonymous).find(room => room.check(m.sender))) {
await this.sendButton(m.chat, '_You are still in anonymous chat, waiting for a partner_', watermark, 'Go out', `${usedPrefix}leave`)
throw false
}
let room = Object.values(this.anonymous).find(room => room.state === 'WAITING' && !room.check(m.sender))
if (room) {
await this.sendButton(room.a, '_Partner found!_', watermark, 'Next', `${usedPrefix}next`)
room.b = m.sender
room.state = 'CHATTING'
await this.sendButton(room.b, '_Partner Found_', watermark, 'Next', `${usedPrefix}next`)
} else {
let id = + new Date
this.anonymous[id] = {
id,
a: m.sender,
b: '',
state: 'WAITING',
check: function (who = '') {
return [this.a, this.b].includes(who)
},
other: function (who = '') {
return who === this.a ? this.b : who === this.b ? this.a : ''
},
}
await this.sendButton(m.chat, '_Waiting partner..._', watermark, 'Go out', `${usedPrefix}leave`)
}
break
}
}
}
handler.help = ['start', 'leave', 'next']
handler.tags = 'anonymous'
handler.command = ['start', 'leave', 'next']
handler.private = true
module.exports = handler