This repository has been archived by the owner on Apr 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmaplb.js
153 lines (139 loc) · 6.26 KB
/
maplb.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
var cd = new Set();
var request = require('request')
function spaceFill (s, l) {
var a = s.length;
for (var i = 1; i < l-a; i++) {
s += ' ';
}
return s;
}
async function lbcall(hash, page) {
var droidapikey = process.env.DROID_API_KEY
var url = 'http://ops.dgsrz.com/api/scoresearchv2.php?apiKey=' + droidapikey + '&hash=' + hash + '&order=score&page=' + page
var entries = [];
//console.log("calling")
return new Promise((resolve, reject) => {
request(url, function (err, response, data) {
if (err) reject(error);
var line = data.split('<br>')
for (i in line) {
entries.push(line[i].split(' '));
}
entries.shift();
//console.log("called")
resolve(entries);
})
})
}
function modstring(mod) {
var res = " +";
if (mod.includes("e")) res += "EZ";
if (mod.includes("h")) res += "HD";
if (mod.includes("r")) res += "HR";
if (mod.includes("d")) res += "DT";
if (mod.includes("c")) res += "NC";
if (mod.includes("n")) res += "NF";
if (mod.includes("t")) res += "HT";
return res;
}
async function editlb(hash, cache, page) {
return new Promise(async (resolve, reject) => {
var output = ""
req_page = Math.floor(parseInt(page) / 10)
//console.log(page + "-" + req_page)
//if no longer in page range, load new req_page
var cache_index = -1
for (var i = 0; i < cache.length; i++) {
if (req_page == cache[i].req_page) {cache_index = i; break}
}
if (cache_index == -1) {
var content = await lbcall(hash, req_page)
cache.push({
req_page: req_page,
content: content
})
cache_index = cache.length - 1
}
//process the cache
//console.log(cache.content)
var lower_bound = page * 10 - ((req_page) * 100)
var upper_bound = page * 10 + 10 - ((req_page) * 100)
for (var i = lower_bound ; i < upper_bound ; i++) {
if (cache[cache_index].content[i]) {
var modstringres = modstring(cache[cache_index].content[i][6])
if (modstringres == " +") modstringres = ""
output += spaceFill((req_page * 100 + i + 1).toString(), 3) + " - " + spaceFill(cache[cache_index].content[i][2], 15) + " - " + spaceFill(cache[cache_index].content[i][5], 2) + " - " + spaceFill(cache[cache_index].content[i][3], 10) + "(" + cache[cache_index].content[i][4] +"x "+ (parseInt(cache[cache_index].content[i][7])/1000).toFixed(3) +"% "+ cache[cache_index].content[i][8] +"m" + modstringres + ")\n"
output += (new Date(parseInt(cache[cache_index].content[i][9]) * 1000)).toUTCString() + "\n"
}
else break;
}
output += "Current page: " + (page + 1);
resolve(output)
})
}
function mapcall(bid, cb) {
var apikey = process.env.OSU_API_KEY;
var url = "https://osu.ppy.sh/api/get_beatmaps?k=" + apikey + "&b=" + bid
request(url, (err, response, data) => {
if (err) throw err
var obj = JSON.parse(data);
if (!obj[0]) {console.log("Map not found"); cb(1)}
else {cb(obj[0].file_md5)}
})
}
module.exports.run = async (client, message, args) => {
if (!args[0]) {message.channel.send("Hey at least give me the map :/"); return;}
var a = args[0].split("/");
beatmapid = a[a.length-1]
var page = 0;
if (!isNaN(parseInt(args[1]))) {page = parseInt(args[1])}
mapcall(beatmapid, async (hash) => {
console.log(hash)
var cache = []
let output = await editlb(hash, cache, page);
message.channel.send('```' + output + '```').then (msg => {
msg.react("⏮️").then(() => {
msg.react("⬅️").then(() => {
msg.react("➡️").then(() => {
msg.react("⏭️").catch(e => console.log(e))
})
})
});
let backward = msg.createReactionCollector((reaction, user) => reaction.emoji.name === '⏮️' && user.id === message.author.id, {time: 120000});
let back = msg.createReactionCollector((reaction, user) => reaction.emoji.name === '⬅️' && user.id === message.author.id, {time: 120000});
let next = msg.createReactionCollector((reaction, user) => reaction.emoji.name === '➡️' && user.id === message.author.id, {time: 120000});
let forward = msg.createReactionCollector((reaction, user) => reaction.emoji.name === '⏭️' && user.id === message.author.id, {time: 120000});
backward.on('collect', async () => {
page = Math.max(0, page - 10);
output = await editlb(hash, cache, page);
msg.edit('```' + output + '```').catch(e => console.log(e));
msg.reactions.forEach(reaction => reaction.remove(message.author.id).catch(e => console.log(e)))
});
back.on('collect', async () => {
page--;
output = await editlb(hash, cache, page);
msg.edit('```' + output + '```').catch(e => console.log(e));
msg.reactions.forEach(reaction => reaction.remove(message.author.id).catch (e => console.log(e)))
});
next.on('collect', async () => {
page++;
output = await editlb(hash, cache, page);
msg.edit('```' + output + '```').catch(e => console.log(e));
msg.reactions.forEach(reaction => reaction.remove(message.author.id).catch(e => console.log(e)))
});
forward.on('collect', async () => {
page += 10;
output = await editlb(hash, cache, page);
msg.edit('```' + output + '```').catch(e => console.log(e));
msg.reactions.forEach(reaction => reaction.remove(message.author.id).catch (e => console.log(e)))
})
});
cd.add(message.author.id);
setTimeout(() => {
cd.delete(message.author.id)
}, 15000)
})
}
module.exports.help = {
name: "maplb"
}