-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathminispin.js
114 lines (106 loc) · 3.93 KB
/
minispin.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
const SSAPI = require('ssapi-node');
const api = new SSAPI();
async function lookup(query, by) {
// If arg is a number string
if (query.match(/^\d+$/)) {
const song = await api.getOpenData(`song/${query}`);
if (song && song.id) {
return song;
}
throw `${query} not found`;
}
const matches = query.match(/^https:\/\/spinsha.re\/song\/(\d+)/);
if (matches) {
const song = await api.getOpenData(`song/${matches[1]}`);
if (song && song.id) {
return song;
}
throw `${query} not found`;
}
const data = await api.postOpenData("searchCharts", {
searchQuery: query,
showExplicit: true
});
if (data.length === 0) {
throw `${query} not found`;
} else if (data.length > 1) {
if (by) {
const filtered = data.filter(song =>
song.artist.toLowerCase() == by.toLowerCase() ||
song.charter.toLowerCase() == by.toLowerCase());
if (filtered.length == 0) {
throw `Could not find such a chart by ${by}`;
}
if (filtered.length == 1) {
return filtered[0];
}
}
throw `Too many results, be more specific`;
} else {
return data[0];
}
}
async function open(chatClient, channel, db) {
const res = await db.query('UPDATE mini_vanilla SET open = true WHERE channel = $1', [channel]);
chatClient.say(channel, `Requests are now open`);
}
async function close(chatClient, channel, db) {
const res = await db.query('UPDATE mini_vanilla SET open = false WHERE channel = $1', [channel]);
chatClient.say(channel, `Requests are now closed`);
}
async function request(chatClient, channel, db, user, args) {
const { rows } = await db.query('SELECT open from mini_vanilla WHERE channel = $1', [channel]);
if (rows[0].open === false) {
chatClient.say(channel, `Sorry, ${user}, requests are closed`);
return;
}
const query = args.filter(w => !w.startsWith("by:")).join(' ');
const byArg = args.filter(w => w.startsWith("by:"))[0];
let by = null;
if (byArg) {
by = byArg.slice(3);
}
const song = await lookup(query, by).catch(err => {
chatClient.say(channel, err);
});
// Insert song into queue
if (song) {
const text = `#${song.id}: ${song.title} - ${song.charter} (${song.XDDifficulty})`;
const { rows } = await db.query('SELECT count(1) from minirequests where channel = $1 and added_by = $2', [channel, user]);
const priority = rows[0].count;
const res = await db.query('INSERT INTO minirequests (spin_id, channel, title, added_by, priority, done) VALUES ($1, $2, $3, $4, $5, $6)',
[song.id, channel, text, user, priority, false]);
console.log(`Inserting ${res.rowCount} song: ${song.id}`);
chatClient.say(channel, `Adding #${song.id}: ${song.title} - ${song.charter} (${song.XDDifficulty})`);
}
}
async function done(channel, db, id) {
if (id) {
const res = await db.query('UPDATE minirequests SET done = true WHERE spin_id = $1 AND channel = $2', [id, channel]);
console.log(`Done ${res.rowCount} song: ${id}`);
} else {
const res = await db.query('UPDATE minirequests SET done = true WHERE id = (' +
'SELECT id FROM minirequests ' +
'WHERE channel=$1 AND DONE=false ' +
'ORDER BY priority, id LIMIT 1)', [channel]);
console.log(`Done ${res.rowCount} song`);
}
}
async function clear(channel, db) {
await db.query('DELETE FROM minirequests WHERE channel = $1', [channel]);
}
async function queue(channel, db) {
const { rows } = await db.query(
'SELECT title, added_by FROM minirequests ' +
'WHERE channel=$1 AND done=false ' +
'ORDER BY priority, id', [channel]);
return rows;
}
module.exports = {
clear,
done,
queue,
request,
open,
close,
};