-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
getUserInfoVars.js
71 lines (61 loc) · 1.52 KB
/
getUserInfoVars.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
const getConnectedUser = require("./getConnectedUser");
const appApi = require("../../../api/app");
const { _ } = require("../../i18next");
const twitch = require("../index");
const retry = require("../retry");
function getDefaultVars() {
return {
isBroadcaster: "0",
isMod: "0",
isVip: "0",
isSubscriber: "0",
// timestamp: Date.now(),
};
}
function getChatUserInfoVars(data) {
const { isBroadcaster, isMod, isSubscriber, isVip } = data.userInfo;
return {
...getDefaultVars(),
isBroadcaster,
isMod,
isVip,
isSubscriber,
};
}
async function isSub(from, to) {
return !!(await twitch.api.helix.subscriptions.getSubscriptionForUser(
from,
to
));
}
async function getPubSubUserInfoVars(user) {
const broadcaster = await getConnectedUser();
const isBroadcaster = broadcaster.login === user.login;
let isSubscriber = false;
try {
isSubscriber =
isBroadcaster || (await retry(() => isSub(broadcaster.id, user.id)));
} catch (error) {
appApi.stateNotify("error", _("errors.unable_to_fetch_sub_state"));
}
let isMod = false;
let isVip = false;
if (!isBroadcaster) {
const mods = await twitch.chat.getMods(broadcaster.login);
const vips = await twitch.chat.getVips(broadcaster.login);
isMod = mods.includes(user.login);
isVip = vips.includes(user.login);
}
return {
...getDefaultVars(),
isBroadcaster,
isMod,
isVip,
isSubscriber,
};
}
module.exports = {
getDefaultVars,
getChatUserInfoVars,
getPubSubUserInfoVars,
};