· 5 years ago · May 14, 2020, 02:36 AM
1setInterval(async() => {
2let oauthToken = await fetch('https://id.twitch.tv/oauth2/token' +
3 `?client_id=${process.env.twitch_id}` +
4 `&client_secret=${process.env.twitch_secret}` +
5 '&grant_type=client_credentials', {
6 method: 'POST'
7 }
8).then(res => res.json()).then(res => res.access_token);
9fetch("https://api.twitch.tv/helix/streams?user_login=rcnxelenos", {
10 headers: {
11 'Authorization': `Bearer ${oauthToken}`,
12 'Client-ID': process.env.twitch_id
13 },
14}).then((res) => res.json())
15.then((body) => {
16 const url = 'https://static-cdn.jtvnw.net/previews-ttv/live_user_rcnxelenos-320x180.jpg?' + uid();
17 const stream = body.data[0];
18if (stream) {
19 const newstream = stream !== undefined;
20 if (Livestream !== newstream){
21 Livestream = newstream;
22 const gameId = stream.game_id;
23 fetch(`https://api.twitch.tv/helix/games?id=${gameId}`, {headers})
24 .then((res) => res.json())
25 .then((_body) => {
26 const game = _body.data[0];
27 const twitchEmbed = new Discord.RichEmbed()
28 .setColor(0x7F00AE)
29 .setAuthor(`Hello ${stream.user_name} is now live on Twitch`, 'https://static-cdn.jtvnw.net/jtv_user_pictures/4841e3c32b746f28-profile_image-70x70.png')
30 .setTitle(stream.title)
31 .setDescription('You can watch this stream by clicking [Here](https://www.twitch.tv/rcnxelenos)')
32 .addField('Playing', game.name, true)
33 .addField('Viewers', stream.viewer_count, true)
34 .setImage(url)
35 .setThumbnail('https://images-ext-1.discordapp.net/external/ezICxCMxeB8dy9RCFMqe98OivMELLL30CzrqDZtTv5s/https/static-cdn.jtvnw.net/jtv_user_pictures/4841e3c32b746f28-profile_image-300x300.png')
36 .setFooter("-Powered by theringcompagny", "https://res.cloudinary.com/teepublic/image/private/s--ZGXtNiB7--/t_Preview/b_rgb:191919,c_limit,f_auto,h_313,q_90,w_313/v1462743367/production/designs/506289_1");
37
38 client.guilds.forEach(guild => {
39 const channel = guild.channels.find('name', 'general');
40 if (channel) {
41 channel.send('Hello guys [RECON]Xelenos is going live, come check it out ;)', twitchEmbed)
42 } else {
43 guild.owner.send('We were unable to find the #general section on your discord server, if you want to fully use Eärendil to the best of is capacity, you should consider making this channel.')
44 }
45 })
46 })
47 }
48
49}
50})
51}, 30000);