-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtwitch.js
217 lines (190 loc) · 6.84 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
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
const Discord = require('discord.js')
const axios = require('axios')
const { GoogleSpreadsheet } = require('google-spreadsheet');
// get Twitch Bearer token
function getTwitchBearerToken() {
return axios.post("https://id.twitch.tv/oauth2/token", null, {
params: {
client_id: process.env.TWITCH_CLIENT_ID,
client_secret: process.env.TWITCH_CLIENT_SECRET,
grant_type: "client_credentials"
}
}
)
.then(data => data.data.access_token)
.catch(err => console.log(`Error fetching token: ${err}`))
}
async function getWhitelistedUsers() {
let whitelistedUsers = []
try {
const doc = new GoogleSpreadsheet(process.env.WHITELIST_SPREADSHEET_ID);
await doc.useServiceAccountAuth({
client_email: process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL,
private_key: process.env.GOOGLE_PRIVATE_KEY.replace(/\\n/g, '\n'),
});
await doc.loadInfo();
const sheet = doc.sheetsByIndex[0]
const rows = await sheet.getRows()
rows.map(data =>
whitelistedUsers.push(data.usernames)
)
} catch (err) {
console.log(err)
}
return whitelistedUsers
}
// calling Twitch api checking what streamers are active based on env variable list of whitelisted users
function checkWhitelistedChannels(token,client){
return getWhitelistedUsers()
.then(users => {
const headers = {
Authorization: `Bearer ${token}`,
'client-id': process.env.TWITCH_CLIENT_ID
};
// need to update this from process.env to results from whitelist...
const params = {
user_login: users
};
return axios.get('https://api.twitch.tv/helix/streams', {
headers,
params
})
})
}
function checkingNowLive(data,client){
console.log('Checking Now Live Videos...')
if(data.length==0){
return null
} else {
let filteredStreamerInfo = data.data.map(
({user_name, title, started_at, viewer_count, thumbnail_url, game_id, user_id}) =>
({user_name, title, started_at, viewer_count, thumbnail_url, game_id, user_id})
)
return sendingChannelUpdates(filteredStreamerInfo,client)
}
}
function sendingChannelUpdates(data, client){
// hack with hardcoded channel id - need an automated way to get it...
const channel = client.channels.cache.get(process.env.NOWLIVE_ANNOUNCEMENTS_ID);
const selfchannel = client.channels.cache.get(process.env.SELF_NOWLIVE_ANNOUNCEMENTS_ID);
const tw = client.emojis.cache.find(emoji => emoji.name === "tw");
if (!channel) return;
return data.map(
streamer =>
{
// note for later -> abstract token call outside this promise then pass into the two functions, prevent duplicating calls
Promise.all(
[
gameIDConversion(streamer.game_id),
streamersProfilePicConversion(streamer.user_id)
]
).then(
res =>
{
// Twitch uses rfc3339 time formatting so getting the current timestamp in that format
let currentDateTime = (new Date()).toISOString()
// some custom logic in case the profilepics arent found, can be better error handled
// similar logic shoudl be in place for gameIDConversion for safety
let avatarImage = ""
if (res[1].data.length > 0){
avatarImage = res[1].data[0].profile_image_url
}
// hacky way to limit our posting to streams that only started within the last x minutes (based on polling frequency)
let minutesAgoStarted = Math.floor(new Date(currentDateTime) - new Date(streamer.started_at)) / 60000
console.log(`[DEBUG] ${streamer.user_name} started ${minutesAgoStarted} minutes ago. [started @ ${streamer.started_at}][currenttime @ ${currentDateTime}]`)
// case for the self promotion channel being different to the main promotions channel
if(streamer.user_name.toLowerCase() === process.env.TWITCH_SELF_IDENTIFIER && minutesAgoStarted < 3){
return selfchannel.send(`${tw} **${streamer.user_name}** - https://www.twitch.com/${streamer.user_name} - is now live!`, formatLiveCardEmbed(streamer, res[0], avatarImage))
}
// case for promoting other twich accounts in a separate channel -> using 2 minutes as our hardcoded threshold
if(streamer.user_name.toLowerCase() != process.env.TWITCH_SELF_IDENTIFIER && minutesAgoStarted < 3){
return channel.send(`${tw} **${streamer.user_name}** - https://www.twitch.com/${streamer.user_name} - is now live!`, formatLiveCardEmbed(streamer, res[0], avatarImage))
}
}
).catch(
err =>
console.log(err)
)
}
)
// return channel.send(`Somebody went live! ${JSON.stringify(data)}`);
}
function formatLiveCardEmbed(streamer, game_name, streamer_avatar){
return new Discord.MessageEmbed()
.setColor(process.env.EMBED_COLORS)
.setTitle(`🟣 **${streamer.user_name}** is Now Live!`)
.setURL(`https://www.twitch.com/${streamer.user_name}`)
// .setAuthor('Some name', 'https://i.imgur.com/wSTFkRM.png', 'https://discord.js.org')
.setDescription(streamer.title)
.setThumbnail(streamer_avatar)
.addFields(
// need to hit giantbomb api to translate game_id to a name
{ name: 'Currently Playing', value: game_name, inline: true },
{ name: 'Viewers', value: streamer.viewer_count, inline: true }
)
.setImage(streamer.thumbnail_url.replace("{width}", "480").replace("{height}", "272"))
.setTimestamp()
}
function gameIDConversion(id){
return getTwitchBearerToken()
.then(token =>
getGameName(id,token)
)
.catch(error =>
console.log(error)
)
}
function getGameName(id, token){
const headers = {
Authorization: `Bearer ${token}`,
'client-id': process.env.TWITCH_CLIENT_ID
};
const params = { id };
return axios.get('https://api.twitch.tv/helix/games', {
headers,
params
})
.then(gamedata =>
gamedata.data.data[0].name
)
.catch(err =>
console.log(err)
)
}
function streamersProfilePicConversion(user_id){
return getTwitchBearerToken()
.then(token =>
getStreamersProfilePic(user_id,token)
)
.catch(error =>
console.log(error)
)
}
function getStreamersProfilePic(user_id, token){
const headers = {
Authorization: `Bearer ${token}`,
'client-id': process.env.TWITCH_CLIENT_ID
};
const params = { id: user_id };
return axios.get(
`https://api.twitch.tv/helix/users`,
{
headers,
params
}
).then(streamerData =>
// appears temperatmental - keep getting data[] results...
streamerData.data
)
.catch(err =>
console.log(err)
)
}
function pollingCurrentlyLive(client){
console.log('Polling Twitch - Currently Live...')
return getTwitchBearerToken()
.then(token => checkWhitelistedChannels(token, client))
.then(channels => checkingNowLive(channels.data, client))
.catch(err => console.log(err))
}
module.exports = pollingCurrentlyLive