-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBeatBot.js
155 lines (143 loc) · 3.47 KB
/
BeatBot.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
const tmi = require('tmi.js')
const request = require('request')
const pattern = require('./pattern.json')
const version = '0.1.2'
var options = {
options: {
debug: true
},
connection: {
reconnect: true
},
identity: {
username: 'BOT_USERNAME_HERE',
password: 'BOT_TOKEN_HERE'
},
channels: ['#YOUR_CHANNEL_HERE']
}
function getPatternizedMessage(beatmapInfo) {
var message = ''
for(let i = 0; i < pattern.length; i++) {
switch(pattern[i]) {
//TODO Escape HTML codes
case '$authorName':
message += beatmapInfo['authorName']
break
case '$beatname':
message += beatmapInfo['beatname']
break
case '$beatsPerMinute':
message += beatmapInfo['beatsPerMinute']
break
case '$beatText':
message += beatmapInfo['beattext']
break
case '$downloadLink':
message += 'https://beatsaver.com/dl.php?id=' + beatmapInfo['id']
break
case '$downloads':
message += beatmapInfo['downloads']
break
case '$id':
message += beatmapInfo['id']
break
case '$ownerID':
message += beatmapInfo['ownerid']
break
case '$plays':
message += beatmapInfo['plays']
break
case '$songName':
message += beatmapInfo['songName']
break
case '$songSubName':
message += beatmapInfo['songSubName']
break
case '$uploadTime':
//TODO Format epoch time to standard date format
message += beatmapInfo['uploadtime']
break
case '$upvotes':
message += beatmapInfo['upvotes']
break
case '$webLink':
message += 'https://beatsaver.com/details.php?id=' + beatmapInfo['id']
break
default:
message += pattern[i]
break
}
}
return message
}
function getSearchResults(q, callback) {
request({
url: 'https://beatsaver.com/search.php?q=' + q,
method: 'GET',
headers: {
'User-Agent': 'BeatBot/' + version,
'Content-Type': 'application/json'
}}, function(err, res, body) {
if(!err && res.statusCode == 200) {
callback(body)
} else {
console.log('Error ' + res.statusCode)
}
}
)
}
function getBeatmapDetails(id, callback) {
request({
url: 'https://beatsaver.com/api.php?mode=details&id=' + id,
method: 'GET',
headers: {
'User-Agent': 'BeatBot/' + version,
'Content-Type': 'application/json'
}},
function(err, res, body) {
if(!err && res.statusCode == 200) {
callback(body)
} else {
console.log('Error ' + res.statusCode)
}
}
)
}
var client = new tmi.client(options)
client.connect()
client.on('chat', function(channel, userstate, message, self) {
const args = message.split(' ')
if(self) return
let prefix = '!'
switch(args[0]) {
case prefix + 'beatrequest':
if(args.length > 1) {
if(args.length == 2) {
if(!isNaN(args[1]) && parseInt(Number(args[1])) == args[1] && !isNaN(parseInt(args[1], 10))) {
getBeatmapDetails(args[1], function(body) {
let beatmapInfo = JSON.parse(body)[0]
client.say(channel, getPatternizedMessage(beatmapInfo))
})
}
break
}
var q = ''
for(let i = 1; i < args.length; i++) {
q += args[i] + '%20'
}
getSearchResults(q, function(body) {
let searchResults = JSON.parse(body)
let id = searchResults['hits']['hits'][0]['_id']
getBeatmapDetails(id, function(body) {
let beatmapInfo = JSON.parse(body)[0]
client.say(channel, getPatternizedMessage(beatmapInfo))
})
})
} else {
client.say(channel, userstate['display-name'] + ': Please specify a search query!')
}
break
default:
break
}
})