-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
177 lines (150 loc) · 4.09 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
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
// dependencies
const tmi = require('tmi.js')
const dotenv = require('dotenv')
const express = require('express')
const cors = require('cors')
const { Server } = require('socket.io')
const http = require('http')
const {
commandList,
sceneChangeCommandList,
} = require('./command-list/commandList')
const autoCommandsConfig = require('./auto-commands/config/autoCommandsConfig')
const obs = require('./obs/obsConnection')
dotenv.config()
const app = express()
const PORT = process.env.PORT || 5000
// configure CORS for the emote wall overlay
app.use(
cors({
origin: 'https://marcusmcb.github.io',
methods: ['GET', 'POST'],
allowedHeaders: ['my-custom-header'],
credentials: true,
})
)
app.use(express.json())
// endpoint to capture the authorization code
// when authorizing the script with Twitch
app.get('/auth/callback', (req, res) => {
const authCode = req.query.code
if (authCode) {
console.log('Authorization Code:', authCode)
res.send('Authorization Code received. Check your console for the code.')
} else {
res.send('Authorization Code not found.')
}
})
// set up the HTTPS server with SSL options
const server = http.createServer(app)
const io = new Server(server, {
cors: {
origin: 'https://marcusmcb.github.io',
methods: ['GET', 'POST'],
allowedHeaders: ['my-custom-header'],
credentials: true,
},
transports: ['polling', 'websocket'], // ensure fallback support
})
// remove the old HTTP server setup and start the HTTPS server
server.listen(PORT, '0.0.0.0', () => {
console.log(`--- HTTPS server is listening on port ${PORT} ---`)
})
// bot logic and TMI client config and connection
let userCommandHistory = {}
const COMMAND_REPEAT_LIMIT = 10
const client = new tmi.Client({
options: { debug: true },
connection: {
secure: true,
reconnect: true,
},
identity: {
username: process.env.TWITCH_BOT_USERNAME,
password: process.env.TWITCH_OAUTH_TOKEN,
},
channels: [process.env.TWITCH_CHANNEL_NAME],
})
try {
client.connect()
} catch (error) {
console.log(error)
}
// OBS connection initialization
;(async () => {
try {
await obs.connect()
console.log('OBS connection ready for commands')
} catch (error) {
console.error('Failed to connect to OBS via ngrok:', error.message)
}
})()
// load in the auto commands config
autoCommandsConfig(client, obs)
// create a socket connection to the static emotes overlay page
io.on('connection', (socket) => {
console.log('A user connected:', socket.id)
socket.on('disconnect', (reason) => {
console.log('A user disconnected:', socket.id, reason)
})
socket.on('ping', () => {
console.log('Ping received from client')
socket.emit('pong')
})
})
// global scene change lock value
const sceneChangeLock = { active: false }
client.on('message', (channel, tags, message, self) => {
if (tags.emotes) {
// console.log('has emotes')
console.log("EMOTES: ", tags.emotes)
io.emit('chat-emote', tags.emotes)
}
if (self || !message.startsWith('!')) {
return
}
const args = message.slice(1).split(' ')
const command = args.shift().toLowerCase()
if (command in commandList || command in sceneChangeCommandList) {
if (!userCommandHistory[tags.username]) {
userCommandHistory[tags.username] = []
}
let history = userCommandHistory[tags.username]
if (
history.length >= COMMAND_REPEAT_LIMIT &&
history.every((hist) => hist === command)
) {
client.say(
channel,
`@${tags.username}, try a different command before using that one again.`
)
} else if (command in sceneChangeCommandList) {
sceneChangeCommandList[command](
channel,
tags,
args,
client,
obs,
command,
sceneChangeLock
)
history.push(command)
if (history.length > COMMAND_REPEAT_LIMIT) {
history.shift()
}
} else {
commandList[command](
channel,
tags,
args,
client,
obs,
sceneChangeLock
)
history.push(command)
if (history.length > COMMAND_REPEAT_LIMIT) {
history.shift()
}
}
}
})