-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
163 lines (140 loc) · 4.71 KB
/
index.js
File metadata and controls
163 lines (140 loc) · 4.71 KB
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
const tmi = require('tmi.js')
const WebSocket = require('ws')
// auth, OBS, and utility methods
const {
returnTwitchRefreshTokenConfig,
} = require('./auth/twitch/twitchAccessTokenConfig')
const { obs, connectToOBS } = require('./obs/obsConnection')
const logToFile = require('./scripts/logger')
// command config and command methods
const autoCommandsConfig = require('./bot-assets/auto-commands/config/autoCommandsConfig')
const {
commandList,
urlCommandList,
} = require('./bot-assets/command-list/commandList')
// auto-id and spotify methods
const {
trackCurrentSongPlaying,
endTrackCurrentSongPlaying,
} = require('./bot-assets/auto-id/trackCurrentSongPlaying')
const wss = new WebSocket.Server({ port: 8081 })
const initializeBot = async (config) => {
let userCommandHistory = {}
let urlCommandCooldown = false
const COOLDOWN_DURATION = 5000
const COMMAND_REPEAT_LIMIT = 10
const displayOBSMessage = config.isObsResponseEnabled
const seratoDisplayName = config.seratoDisplayName.replaceAll(' ', '_')
const url = `https://serato.com/playlists/${seratoDisplayName}/live`
// Prefer keystore-stored Twitch access token (if migrated), otherwise fall back to DB field
const { getToken: getKeystoreToken } = require('./database/helpers/tokens')
let twitchAuthToken = config.twitchAccessToken
try {
const blob = await getKeystoreToken('twitch', config._id)
if (blob && blob.access_token) twitchAuthToken = blob.access_token
} catch (e) {
console.error('Error reading twitch token from keystore:', e)
}
const refreshTokenConfig = returnTwitchRefreshTokenConfig(
config,
twitchAuthToken
)
logToFile(`REFRESH TOKEN CONFIG: ${JSON.stringify(refreshTokenConfig)}`)
logToFile('*******************************')
const twitchClient = new tmi.Client(refreshTokenConfig)
try {
twitchClient.connect()
} catch (error) {
logToFile(`TWITCH CONNECTION ERROR: ${error}`)
logToFile('*******************************')
console.error('TWITCH CONNECTION ERROR: ', error)
return error
}
if (config.isObsResponseEnabled === true) {
await connectToOBS(config)
}
autoCommandsConfig(twitchClient, obs, config)
twitchClient.on('connected', (channel, tags, message, self) => {
console.log('Twitch connection successful')
console.log('---------------------------------')
if (config.isSpotifyEnabled || config.isAutoIDEnabled === true) {
console.log('Config: ')
console.log('Spotify Enabled: ', config.isSpotifyEnabled)
console.log('Auto Id Enabled: ', config.isAutoIDEnabled)
console.log('Cleanup Enabled: ', config.isAutoIDCleanupEnabled)
trackCurrentSongPlaying(config, url, twitchClient, wss)
}
})
twitchClient.on('disconnected', () => {
// console.log(npSongsQueried)
// console.log(dypSearchTerms)
console.log('---------------------------------')
console.log('Twitch client has been disconnected')
console.log('---------------------------------')
endTrackCurrentSongPlaying()
})
twitchClient.on('message', (channel, tags, message, self) => {
if (self || !message.startsWith('!')) {
return
}
const args = message.slice(1).split(' ')
const command = args.shift().toLowerCase()
if (!userCommandHistory[tags.username]) {
userCommandHistory[tags.username] = []
}
let history = userCommandHistory[tags.username]
history.push(command)
if (history.length > COMMAND_REPEAT_LIMIT) {
history.shift()
}
if (command in commandList) {
if (
history.length >= COMMAND_REPEAT_LIMIT &&
history.every((hist) => hist === command)
) {
twitchClient.say(
channel,
`@${tags.username}, try a different command before using that one again.`
)
} else {
if (command in urlCommandList && displayOBSMessage) {
if (urlCommandCooldown) {
twitchClient.say(
channel,
`@${tags.username}, please wait for the current command on screen to clear before using that one.`
)
return
}
urlCommandCooldown = true
commandList[command](
channel,
tags,
args,
twitchClient,
obs,
url,
config
)
setTimeout(() => {
urlCommandCooldown = false
}, COOLDOWN_DURATION)
} else {
commandList[command](
channel,
tags,
args,
twitchClient,
obs,
url,
config
)
}
}
}
})
return twitchClient
}
module.exports = initializeBot
// Start a background migration (non-blocking) so legacy DB tokens move to keystore on startup
// NOTE: migration moved to the main process startup (electron-main.js) so it runs when the
// application is launched rather than when the bot process is started.