This repository has been archived by the owner on Jan 27, 2021. It is now read-only.
/
twitch.js
80 lines (71 loc) · 2.95 KB
/
twitch.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
/**
* @typedef {import("twitch").Channel} TwitchApi.Channel
* @typedef {import("twitch").HelixStream} TwitchApi.HelixStream
* @typedef {import("twitch").default} TwitchApi.TwitchClient
*/
const {default: TwitchApi, HelixStreamType} = require("twitch"),
settings = require("./settings");
/**
* @type {TwitchApi.TwitchClient}
*/
let twitchApi;
// ##### # # #
// # # #
// # # # ## #### ### # ##
// # # # # # # # ## #
// # # # # # # # # #
// # # # # # # # # # # #
// # # # ### ## ### # #
/**
* A class that encapsulates functions needed from the twitch-api library.
*/
class Twitch {
// # # #
// #
// ## ### ## ###
// # # # # #
// # # # # #
// ### # # ### ##
/**
* Initializes the twitch API client.
* @returns {Promise} A promise that resolves when the client has been initialized.
*/
static async init() {
if (!twitchApi) {
twitchApi = await TwitchApi.withClientCredentials(settings.twitch.clientId, settings.twitch.clientSecret);
}
}
// # ## #
// # # # #
// ### ## ### # ### ### ## ### # # ###
// # # # ## # # # # # # ## # # #### ##
// ## ## # # # # # ## # ## # # ##
// # ## ## ## ## # ## # # # # ###
// ###
/**
* Returns the streams that are live from the provided list of channels.
* @param {string[]} channels An array of channels to check.
* @returns {Promise<TwitchApi.HelixStream[]>} An array of streams that are live from the provided list of channels.
*/
static async getStreams(channels) {
await Twitch.init();
return twitchApi.helix.streams.getStreamsPaginated({userName: channels, type: HelixStreamType.Live}).getAll();
}
// # ## # ## ## #
// # # # # # # # #
// ### ## ### # ### ### ### ### ## # # ### ### ## ### # #
// # # # ## # # # # # # # # # # # ## # # # # # # ## # # ####
// ## ## # # # # # # ## # # # # ## # # # # # ## # ## # #
// # ## ## ## # # # # # # # # ## ### ## ## # ## # # # #
// ###
/**
* Returns the channel's stream information.
* @param {string} channel The channel to check.
* @returns {Promise<TwitchApi.Channel>} The channel's stream information.
*/
static async getChannelStream(channel) {
await Twitch.init();
return twitchApi.kraken.channels.getChannel(channel);
}
}
module.exports = Twitch;