socket_server.js
5.13 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
164
165
166
167
168
169
170
171
172
const config = require('./config/config')
const app = require('express')();
const cors = require('cors')
app.use(cors()) //express 기본 응답에 대한 cors
const http = require('http').Server(app);
const io = require('socket.io')(http, {
cors:{
origin: "*",
}
});
const papago = require('./openAPIs/papago_api');
const tmi = require('tmi.js');
// Define configuration options
var opts = {
identity: {
username: process.env.BOT_USERNAME,
password: process.env.OAUTH_TOKEN
},
channels: []
};
// Create a client with our options
var client = new tmi.client(opts); //twitch chatbot client
var bodyParser = require('body-parser');
const { default: Axios } = require('axios');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.set('view engine', 'ejs');
app.set('views', './testviews');
app.get('/', (req, res) => {
res.render('chat');
});
app.get('/channels',(req,res) => {
console.log(channels)
res.send(channels)
});
app.post('/jointest',async (req,res)=>{
// requests.post(`https://id.twitch.tv/oauth2/token?client_id=<클라이언트 ID>&client_secret=${process.env.TOKEN}&grant_type=client_credentials`).json()
JoinChannel(req.body.streamer);
res.send(req.body.streamer)
});
////////////////////////oauth////////////////////////
var channels = []
const axios = require('axios')
app.get('/oauth',(req,res)=>{
let codeAddr = `https://id.twitch.tv/oauth2/authorize?response_type=code&approval_prompt=auto&redirect_uri=${process.env.HOST_URI+':'+process.env.SOCKET_PORT}/join&client_id=${process.env.TWITCH_CLIENT}`
res.redirect(codeAddr)
});
app.get('/join', async (req,res)=>{
let code = req.query.code
let reqAddr = `https://id.twitch.tv/oauth2/token?client_id=${process.env.TWITCH_CLIENT}&client_secret=${process.env.TWITCH_SECRET}&code=${code}&grant_type=authorization_code&redirect_uri=${process.env.HOST_URI+':'+process.env.SOCKET_PORT}/test`
axios.post(reqAddr).then(resp1=>{
axios.get('https://id.twitch.tv/oauth2/validate',
{
headers:{
Authorization : "Bearer "+ resp1.data.access_token
}
}
).then(resp2=>{
JoinChannel(resp2.data.login)
axios.get(`https://api.twitch.tv/kraken/channels/${resp2.data.user_id}`,
{
headers:{
'Accept' : 'application/vnd.twitchtv.v5+json',
'Client-ID' : process.env.TWITCH_CLIENT
}
}
).then(resp3=>{
channels.push(resp3.data);
})
res.redirect(`${process.env.FRONT_URI}:${process.env.FRONT_PORT}/?authenticated=true`) // 이거 프론트 유알엘임
})
})
})
app.get('/test',(req,res)=>{
res.send("")
})
////////////////////////oauth////////////////////////
async function JoinChannel(streamer){
opts.channels = [('#'+streamer)]
client = await new tmi.client(opts);
client.channels.push('#'+streamer);
client.on('message', onMessageHandler);
client.on('connected', onConnectedHandler);
client.connect();
}
io.on('connection', (socket) => {
socket.on('disconnect', () => {
console.log('user disconnected');
});
socket.on('leaveRoom', (streamer,name) => {
socket.leave(streamer, () => {
console.log('Someone leave a ' + streamer);
io.to(streamer).emit('leaveRoom', streamer ,name);
});
});
socket.on('joinRoom', (streamer,name) => {
socket.join(streamer, () => {
console.log('Someone join a ' + streamer);
io.to(streamer).emit('joinRoom', streamer ,name);
console.log(streamer,io)
});
});
socket.on('chat message', (streamer, name, msg) => {
io.to(streamer).emit('chat message', name, msg);
});
});
http.listen(process.env.SOCKET_PORT, () => {
console.log(`Connect at ${process.env.SOCKET_PORT}`);
});
/////////////////////Twitch Bot//////////////////////////
// Register our event handlers (defined below)
client.on('message', onMessageHandler);
client.on('connected', onConnectedHandler);
// Connect to Twitch:
client.connect();
// Called every time a message comes in
function onMessageHandler (target, context, msg, self) {
if (self) { return; } // Ignore messages from the bot
if (msg.startsWith('!')){
if(msg.startsWith('!번역')){
io.to(target.replace('#','')).emit('chat message',context["display-name"],msg.slice(4))
papago.trans(msg.slice(4), client, io, target)
}
}
else if(context["display-name"] == "빵_떡"
|| context["display-name"]=="Nightbot"
|| context["display-name"]=="싹뚝"
|| context["display-name"]=="KhuwitchBot"){
return;
}
else{
io.to(target.replace('#','')).emit('chat message',context["display-name"],msg)
console.log("메시지 발생", context["display-name"],msg)
papago.detectchat(msg, client, io, target);
}
}
// Called every time the bot connects to Twitch chat
function onConnectedHandler (addr, port) {
console.log(`*KwitchBot Connected to ${addr}:${port}`);
}