-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.mjs
296 lines (262 loc) · 9.01 KB
/
commands.mjs
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import Spotify from 'spotify-web-api-node';
import * as Utilities from './utilities.mjs';
import * as Neo4j from './neo4j.mjs';
/**
* @typedef {Object} Context
* @property {import('discord.js').Message} message
* @property {[string]} content
* @property {import('pg').Pool} postgres
* @property {import('neo4j-driver').Session} neo4j
*/
/**
* @typedef {import('discord.js').User} DiscordUser
*/
/**
* Gets a Spotify Web API instance for the user and database from the context.
* @param {Context} context
* @param {?DiscordUser} user
* @return {Promise<?Spotify>}
*/
async function getSpotify(context, user) {
const accessToken = await Utilities.getSpotifyAccessToken(
user || context.message.author,
context.postgres,
);
return accessToken ? new Spotify({accessToken: accessToken}) : null;
}
/**
* Links a Spotify account to a Discord user.
* Adds the Spotify user ID and necessary auth material to the database.
* @param {Context} context
*/
export async function link(context) {
const reply = (content) => context.message.reply(content);
const spotify = await getSpotify(context);
if (spotify) {
const profileResponse = await spotify.getMe();
const username = profileResponse.body.id;
await reply(`you are linked to the Spotify account \`${username}\`.`);
} else {
await reply('your Spotify account isn\'t connected.');
}
}
/**
* Creates a moshpit and adds the user to it
* @param {Context} context The context from which this command was called
*/
export async function start(context) {
const reply = (content) => context.message.reply(content);
const owner = context.message.author;
const ownerSpotify = await getSpotify(context, owner);
if (!ownerSpotify) {
await reply('you need to be signed in to Spotify.');
return;
}
const oldMoshpitResults = await context.postgres.query(`
select moshpit_id, spotify_playlist_id
from "Moshpit"
where discord_guild_id = '${context.message.guild.id}'
and owner_discord_id = '${owner.id}';
`);
let moshpit = oldMoshpitResults.rows[0];
// If there isn't already a moshpit for this user in this guild, make one
if (!moshpit) {
// Create a Spotify playlist
const playlistResponse = await ownerSpotify.createPlaylist(
`${context.message.guild.name} moshpit`,
{
'public': true,
'description': `A moshpit auto-generated playlist`,
},
);
const playlist = playlistResponse.body;
// Create a moshpit in the database
const newMoshpitResults = await context.postgres.query(`
insert into "Moshpit" (
spotify_playlist_id,
discord_guild_id,
owner_discord_id,
join_secret
)
values (
'${playlist.id}',
'${context.message.guild.id}',
'${owner.id}',
''
)
returning moshpit_id, spotify_playlist_id;
`);
moshpit = newMoshpitResults.rows[0];
}
// Update the owner's current moshpit to this one
await context.postgres.query(`
update "MoshpitUser"
set moshpit_id = '${moshpit.moshpit_id}'
where discord_user_id = '${owner.id}';
`);
await reply(`great! Let's get everyone else on board.`);
const listeners = await Utilities.collectJoins(
context.message.channel,
owner,
`React to join <@${owner.id}>'s moshpit!`,
'🖐️',
);
// Check how long the playlist is
const playlistLengthResponse = await ownerSpotify.getPlaylistTracks(
moshpit.spotify_playlist_id,
{fields: 'total'},
);
let playlistLength = playlistLengthResponse.body.total;
// Populate the playlist with more tracks if it's too small
if (playlistLength < 5) {
const trackURIs = await Utilities.addTracks(
ownerSpotify,
[ownerSpotify, ...await Promise.all(
listeners.map((user) => getSpotify(context, user)),
)],
5 - playlistLength,
context.postgres,
moshpit.moshpit_id,
moshpit.spotify_playlist_id,
);
playlistLength += trackURIs.length;
}
// Determine the track to start on
const startTrackURIResponse = await ownerSpotify.getPlaylistTracks(
moshpit.spotify_playlist_id,
{
offset: playlistLength - 5,
limit: 1,
fields: 'items(track(uri))',
},
);
const startTrackURI = startTrackURIResponse.body.items[0].track.uri;
// Start playing the playlist
await Promise.all([owner, ...listeners].map(async (listener) => {
const listenerSpotify = await getSpotify(context, listener);
// TODO: Can we force the users to open sessions before trying to play?
try {
await listenerSpotify.setShuffle(false);
await listenerSpotify.play({
context_uri: `spotify:playlist:${moshpit.spotify_playlist_id}`,
offset: {uri: startTrackURI},
});
// Update the listener's most recent moshpit
await context.postgres.query(`
update "MoshpitUser"
set moshpit_id = '${moshpit.moshpit_id}'
where discord_user_id = '${listener.id}';
`);
} catch (error) {
if (listener.id === owner.id) {
throw error;
} else {
listener.send('Something went wrong while joining the moshpit. Make ' +
'sure you have an active Spotify session! You may need ' +
'to start playback first.');
console.info(error);
}
}
}));
// Send an initial "Now Playing" message
let playbackState = (await ownerSpotify.getMyCurrentPlaybackState()).body;
let nowPlayingMessage =
await reply(`now playing: "${playbackState.item.name}"`);
// Check the owner's player status on an interval
setInterval(async function() { // Using lambda because we need 'this'
const oldPlaybackState = playbackState;
playbackState = (await ownerSpotify.getMyCurrentPlaybackState()).body;
if (!playbackState.is_playing || playbackState.context.uri !==
`spotify:playlist:${moshpit.spotify_playlist_id}`) {
// If the owner has stopped or left the playlist, end playback
await nowPlayingMessage.delete();
await Promise.all(listeners.map(async (listener) => {
const listenerSpotify = await getSpotify(context, listener);
await listenerSpotify.pause();
}));
// Stop this interval
// eslint-disable-next-line no-invalid-this
clearInterval(this);
} else if (playbackState.item.id !== oldPlaybackState.item.id) {
// If the owner has moved on to the next song, update "Now Playing"
await nowPlayingMessage.delete();
nowPlayingMessage =
await reply(`now playing: "${playbackState.item.name}"`);
}
}, 5000);
await Neo4j.sqlToNeo4j(context);
}
/**
* Gets data for the user's moshpit in the current server if one exists.
* @param {Context} context
*/
export async function data(context) {
const reply = (content) => context.message.reply(content);
const result = await context.postgres.query(`
select moshpit_id, discord_guild_id, owner_discord_id
from "Moshpit"
where owner_discord_id = '${context.message.author.id}'
and discord_guild_id = '${context.message.guild.id}';
`);
if (result.rowCount > 0) {
await reply(`\`\`\`\n${JSON.stringify(result.rows)}\n\`\`\``);
} else {
await reply('no results found.');
}
}
/**
* Deletes the current moshpit fromt the database
* @param {Context} context
*/
export async function quit(context) {
const reply = (content) => context.message.reply(content);
const result = await context.postgres.query(`
DELETE FROM "Moshpit"
WHERE owner_discord_id = '${context.message.author.id}';
`);
if (result.rowCount > 0) {
await reply(`your moshpits were deleted!`);
} else {
await reply('no results found.');
}
}
/**
* Advanced Query 1: Count total users with expired Spotify tokens, grouped by
* the Discord Guild they are in.
* @param {Context} context
*/
export async function aq1(context) {
const reply = (content) => context.message.reply(content);
const result = await context.postgres.query(`
SELECT m.discord_guild_id, COUNT(mu.discord_user_id)
FROM "Moshpit" m NATURAL JOIN "MoshpitUser" mu
WHERE mu.spotify_token_expiration < CURRENT_TIMESTAMP
GROUP BY m.discord_guild_id;
`);
if (result.rowCount > 0) {
await reply(`\`${JSON.stringify(result.rows)}\``);
} else {
await reply('no results found.');
}
}
/**
* Advanced Query 2: Count the number of moshpits that each user owns for users
* that own at least one.
* @param {Context} context
*/
export async function aq2(context) {
const reply = (content) => context.message.reply(content);
const result = await context.postgres.query(`
SELECT mu.discord_user_id, COUNT(m.moshpit_id)
FROM "MoshpitUser" mu LEFT JOIN "Moshpit" m
ON mu.discord_user_id = m.owner_discord_id
GROUP BY mu.discord_user_id
HAVING COUNT(m.moshpit_id) >= 1
ORDER BY COUNT(m.moshpit_id) DESC;
`);
if (result.rowCount > 0) {
await reply(`\`${JSON.stringify(result.rows)}\``);
} else {
await reply('no results found.');
}
}