-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathindex.js
125 lines (106 loc) · 4.28 KB
/
index.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
const Discord = require('discord.js');
const client = new Discord.Client();
var CronJob = require('cron').CronJob;
const fs = require('fs')
const Stream = require("./modules/getStreams.js")
const Auth = require("./modules/auth.js")
const Channel = require("./modules/channelData.js")
const config = require('./config.json')
//ready
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
//update the authorization key on startup
UpdateAuthConfig()
});
//function that will run the checks
var Check = new CronJob(config.cron,async function () {
const tempData = JSON.parse(fs.readFileSync('./config.json'))
tempData.channels.map(async function (chan, i) {
if (!chan.ChannelName) return;
let StreamData = await Stream.getData(chan.ChannelName, tempData.twitch_clientID, tempData.authToken);
if (StreamData.data.length == 0) return
StreamData = StreamData.data[0]
//get the channel data for the thumbnail image
const ChannelData = await Channel.getData(chan.ChannelName, tempData.twitch_clientID, tempData.authToken)
if (!ChannelData) return;
//structure for the embed
var SendEmbed = {
"title": `🔴 ${StreamData.user_name} is now live`,
"description": StreamData.title,
"url": `https://www.twitch.tv/${StreamData.user_login}`,
"color": 6570404,
"fields": [
{
"name": "Playing:",
"value": StreamData.game_name,
"inline": true
},
{
"name": "Viewers:",
"value": StreamData.viewer_count,
"inline": true
},
{
"name": "Twitch:",
"value": `[Watch stream](https://www.twitch.tv/${StreamData.user_login})`
},
(chan.DiscordServer ? {
"name": "Discord Server:",
"value": `[Join here](${chan.DiscordServer})`
} : {
"name": "** **",
"value": "** **"
})
],
"footer": {
"text": StreamData.started_at
},
"image": {
"url": `https://static-cdn.jtvnw.net/previews-ttv/live_user_${StreamData.user_login}-640x360.jpg?cacheBypass=${(Math.random()).toString()}`
},
"thumbnail": {
"url": `${ChannelData.thumbnail_url}`
}
}
//get the assigned channel
const sendChannel = client.guilds.cache.get(config.DiscordServerId).channels.cache.get(config.channelID)
if (chan.twitch_stream_id == StreamData.id) {
sendChannel.messages.fetch(chan.discord_message_id).then(msg => {
//update the title, game, viewer_count and the thumbnail
msg.edit({ embed: SendEmbed })
});
} else {
//this is the message when a streamer goes live. It will tag the assigned role
await sendChannel.send({ embed: SendEmbed }).then(msg => {
const channelObj = tempData.channels[i]
channelObj.discord_message_id = msg.id
channelObj.twitch_stream_id = StreamData.id
if(config.roleID){
sendChannel.send(`<@&${config.roleID}>`)
}
})
}
//save config with new data
fs.writeFileSync('./config.json', JSON.stringify(tempData))
})
});
//update the authorization key every hour
var updateAuth = new CronJob('0 * * * *', async function () {
UpdateAuthConfig()
});
//get a new authorization key and update the config
async function UpdateAuthConfig(){
let tempData = JSON.parse(fs.readFileSync('./config.json'));
//get the auth key
const authKey = await Auth.getKey(tempData.twitch_clientID, tempData.twitch_secret);
if (!authKey) return;
//write the new auth key
var tempConfig = JSON.parse(fs.readFileSync('./config.json'));
tempConfig.authToken = authKey;
fs.writeFileSync('./config.json', JSON.stringify(tempConfig));
}
//start the timers
updateAuth.start()
Check.start();
//login
client.login(config.token);