-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtpp-chatterdb.js
117 lines (97 loc) · 4.66 KB
/
tpp-chatterdb.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
// define configuration options
require('dotenv').config()
const { JsonDB, Config } = require('node-json-db')
const userDB = new JsonDB(new Config('db-users', false, true, '/'))
const fetch = require('node-fetch')
// our pretty printer
function printMessage(message) {
console.log(new Date().toLocaleTimeString(), message)
}
// ensure our token is valid
function validateToken(print) {
if (print) printMessage(`Validating OAuth token...`)
// https://dev.twitch.tv/docs/authentication/validate-tokens/
fetch(`https://id.twitch.tv/oauth2/validate`, { method: 'GET', headers: { 'Authorization': `OAuth ${process.env.TWITCH_OAUTH}` }})
.then(data => data.json())
.then(data => {
if (data.login && print) {
if (data.expires_in > 0) printMessage(`OAuth token is valid and will expire on ${new Date(Date.now() + (data.expires_in * 1000))}`)
else printMessage(`OAuth token is valid and but Twitch did not provide an expiry date.`)
} else if (data.status == 401) {
printMessage(`OAuth token is invalid or has expired. Please create a new one and update env file.`)
setTimeout(process.exit, 1000)
}
})
.catch(err => printMessage(`Error while checking token validity -- ${err}`))
}
// build our databases
async function addToDatabase(array, skip) {
const user_id = array.user_id
const user_login = array.user_login
const result = await userDB.getObjectDefault(`/${user_id}`, "user doesn't exist")
if (result == "user doesn't exist") {
// new user to the database
userDB.push(`/${user_id}`, { "user_login": user_login, "time_in_chat": 0, "spoken_in_chat": false })
} else {
// user exists in the database; update their time_in_chat and check if they've spoken_in_chat
if (!skip) userDB.push(`/${user_id}/time_in_chat`, result.time_in_chat + 5)
if (result.user_login != user_login) {
printMessage(`${result.user_login} has changed their name to ${user_login}`)
userDB.push(`/${user_id}/previous_login`, result.user_login)
userDB.push(`/${user_id}/user_login`, user_login)
}
// no need to spam GQL with requests if we know they've spoken at least once.
if (result.spoken_in_chat == false) {
const body = [{
operationName: 'ViewerCardModLogsMessagesBySender',
variables: {
senderID: user_id,
channelID: '56648155',
},
extensions: {
persistedQuery: {
version: 1,
sha256Hash: '53962d07438ec66900c0265d3e9ec99c4124067ac3a9c718bc29b0b047d1e89c',
},
},
}]
fetch(`https://gql.twitch.tv/gql`, {
method: 'POST', body: JSON.stringify(body), headers: {
'Authorization': `OAuth ${process.env.GRAPHQL_OAUTH}`, 'Client-Id': 'kimne78kx3ncx6brgo4mv6wki5h1ko',
'Client-Integrity': `${process.env.GRAPHQL_INTEGRITY}`, 'X-Device-Id': `${process.env.GRAPHQL_DEVICEID}`,
}
})
.then(data => data.json())
.then(data => {
// user has spoken, mark down as such.
if (data[0]?.data?.viewerCardModLogs?.messages?.edges?.length > 0) userDB.push(`/${user_id}/spoken_in_chat`, true)
})
.catch(err => printMessage(`ERROR fetching messages for ${user_login} -- ${err}`))
}
}
}
// gather the goods
function queryTwitch(cursor, skip) {
let pagination = ""
if (cursor) pagination = `&after=${cursor}`
fetch(`https://api.twitch.tv/helix/chat/chatters?moderator_id=44322184&broadcaster_id=56648155&first=1000${pagination}`, {
method: 'GET', headers: { 'Authorization': `Bearer ${process.env.TWITCH_OAUTH}`, 'Client-Id': process.env.TWITCH_CLIENTID },
})
.then(data => { if (data.ok) return data.json(); else printMessage(`Chatters endpoint returned Error ${data.status} ${data.statusText}`)})
.then(data => {
if (!data) return
for (let i = 0; i < data.data.length; i++) {
if (data.data[i].user_login != "") addToDatabase(data.data[i], skip)
if (i+1 == data.data.length) userDB.save()
//else console.log(data.data[i])
}
if (data.pagination.cursor) setTimeout(() => queryTwitch(data.pagination.cursor, skip), 100)
})
.catch(err => printMessage(`Error while downloading chatter list -- ${err}`))
}
// engage
userDB.load()
validateToken(true)
setInterval(validateToken, 3600000, false)
queryTwitch(null, true)
setInterval(queryTwitch, 300000, null, false)