-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.ts
60 lines (53 loc) · 1.66 KB
/
index.ts
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
import { API_KEY, SPACE_ID, CLIENT_ID, CLIENT_SECRET, ACCESS_TOKEN, REFRESH_TOKEN } from "./api-key";
import { Game } from "@gathertown/gather-game-client";
global.WebSocket = require("isomorphic-ws");
const Updater = require("spotify-oauth-refresher");
// token refresher setup
const api = new Updater({ clientId: CLIENT_ID, clientSecret: CLIENT_SECRET });
api.setAccessToken(ACCESS_TOKEN);
api.setRefreshToken(REFRESH_TOKEN);
// gather game client setup
const game = new Game(SPACE_ID, () => Promise.resolve({ apiKey: API_KEY }));
game.connect();
game.subscribeToConnection((connected) => console.log("connected?", connected));
// check every 5s
setInterval(async () => {
const res = await api.request({
url: "https://api.spotify.com/v1/me/player/currently-playing",
method: "get",
authType: "bearer",
});
let playing = "";
let emoji = "";
if (res.data.is_playing === true) {
if (res.data.currently_playing_type === "track") {
playing =
res.data.item.name + " ∙ " + res.data.item.artists[0].name + " (Spotify)";
emoji = "🎶";
} else if (res.data.currently_playing_type === "episode") {
playing = "listening to some podcast (Spotify)";
emoji = "🎧";
}
else console.log("unexpected value in 'currently_playing_type'")
}
else { // listening to nothing
playing = "";
emoji = "";
};
if (playing !== "") {
console.log(playing);
}
else console.log("stopped listening");
game.sendAction({
$case: "setEmojiStatus",
setEmojiStatus: {
emojiStatus: emoji,
},
});
game.sendAction({
$case: "setTextStatus",
setTextStatus: {
textStatus: playing,
},
});
}, 5000);