/
index.js
119 lines (93 loc) · 2.19 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import Ultron from 'ultron'
import miniplug from 'miniplug'
import { Adapter, User } from 'munar-core'
import Message from './PlugdjMessage'
import Waitlist from './Waitlist'
import DJBooth from './DJBooth'
import DJHistory from './DJHistory'
const debug = require('debug')('munar:adapter:plugdj')
export default class PlugdjAdapter extends Adapter {
static adapterName = 'plugdj'
mp = miniplug()
constructor (bot, options) {
super(bot)
this.options = options
this.events = new Ultron(this.mp)
this.waitlist = new Waitlist(this)
this.djBooth = new DJBooth(this)
this.djHistory = new DJHistory(this)
}
getWaitlist () {
return this.waitlist
}
getDJBooth () {
return this.djBooth
}
getDJHistory () {
return this.djHistory
}
toBotUser (user) {
debug('toBotUser', user.id, user.username)
return new User(this, user.id, user.username, user)
}
// Base Adapter
async connect () {
this.events.on('userJoin', this.onJoin)
this.events.on('userLeave', this.onLeave)
this.events.on('chat', this.onChat)
await this.mp.connect({
email: this.options.email,
password: this.options.password
})
await this.mp.join(this.options.room)
}
async disconnect () {
this.mp.ws.close()
}
reply (message, text) {
this.send(`@${message.username} ${text}`)
}
send (text) {
this.mp.chat(text)
}
deleteMessage = this.mp.deleteChat
getSelf () {
return this.toBotUser(this.mp.me())
}
getUsers () {
return this.mp.users()
.map(this.toBotUser, this)
}
getChannels () {
return [this]
}
getChannel (str) {
if (str !== 'main') {
throw new Error(`Channel ${str} does not exist`)
}
return this
}
canExecute () {
return true
}
onChat = (message) => {
const { user } = message
this.receive('message',
new Message(this, message, user && this.toBotUser(user))
)
}
onJoin = (user) => {
if (user) {
this.receive('user:join', this.toBotUser(user))
}
}
onLeave = (id) => {
const user = this.mp.user(id)
if (user) {
this.receive('user:leave', this.toBotUser(user))
}
}
toString () {
return 'plug.dj'
}
}