-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitcasting.js
187 lines (180 loc) · 7.99 KB
/
twitcasting.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
178
179
180
181
182
183
184
185
186
187
const {Client, RichEmbed} = require('discord.js');
const client = new Client();
const fs = require("fs");
const request = require('request');
const crypto = require("crypto");
client.on('message', async msg => {
const memberId = msg.author.id;
if(msg.channel.type === 'dm'){
if(msg.content.startsWith('!token')){
var args = msg.content.split(' ');
var twitcas = JSON.parse(fs.readFileSync('./api/twitcas/token.json'));
var token = args[1];
var userId = msg.author.id;
var options = {
url: 'https://apiv2.twitcasting.tv/verify_credentials',
headers: {
'Accept':'application/json',
'X-Api-Version':'2.0',
'Authorization':'Bearer ' + token,
},
json: true
};
request.get(options, function(error, response, body){
if(body.error == undefined){
if(typeof twitcas[userId] === 'undefined'){
twitcas[userId] = {name : '', token : ''};
}
var cipher = crypto.createCipher(ALGORITHM, KEY );
var ciphered = cipher.update(token, INPUT_ENCODING, OUTPUT_ENCODING);
ciphered += cipher.final(OUTPUT_ENCODING);
twitcas[userId].token = ciphered;
twitcas[userId].name = body.user.name;
var charset = 'utf-8';
var place = './api/twitcas/token.json';
fs.writeFileSync(place, JSON.stringify(twitcas), charset);
var un = body.user.name;
msg.reply(un + 'でログインしました!');
return;
}else{
msg.reply('認証できませんでした。トークンを確認してください。');
return;
}
});
}
return;
}
if(msg.author.bot){
return;
}
if(msg.channel.id === '000000000000000000'){
if(msg.content === '!login'){
msg.author.send("<https://example.com/login>\nにアクセスして認証し、表示されたコードをここにコピペしてね。");
}
if(msg.content === '!signIn'){
var twitcas = JSON.parse(fs.readFileSync('./api/twitcas/token.json'));
if(typeof twitcas[msg.author.id] === 'undefined'){
msg.reply('先に`!login`でユーザー認証を済ませてください!');
return;
}else{
var token = twitcas[msg.author.id].token;
var decipher = crypto.createDecipher(ALGORITHM, KEY);
var deciphered = decipher.update(token, OUTPUT_ENCODING, INPUT_ENCODING );
deciphered += decipher.final(INPUT_ENCODING);
}
var options = {
url: 'https://apiv2.twitcasting.tv/verify_credentials',
headers: {
'Accept':'application/json',
'X-Api-Version':'2.0',
'Authorization':'Bearer ' + deciphered,
},
json: true
};
request.get(options, function(error, response, body){
if(body.error == undefined){
var memId = msg.author.id;
var userName = twitcas[memId].name;
var member = msg.guild.members.get(memId);
var role = msg.guild.roles.find(role => role.name === "twitcas");
member.addRole(role);
msg.reply('ようこそ ' + userName + '。ツイキャスコメントを楽しもう!');
return;
}else{
msg.reply('アクセストークンの期限が切れています。認証のやり直しをお願いいたします。\n```' + body.error.code + ', ' + body.error.message + '```');
return;
}
});
}
if(msg.content === '!getID'){
var twitcas = JSON.parse(fs.readFileSync('./api/twitcas/token.json'));
var userId = msg.author.id;
if(typeof twitcas[userId] === 'undefined'){
msg.reply('先に`!login`でユーザー認証を済ませてください!');
return;
}else{
var token = twitcas[userId].token;
var decipher = crypto.createDecipher(ALGORITHM, KEY);
var deciphered = decipher.update(token, OUTPUT_ENCODING, INPUT_ENCODING );
deciphered += decipher.final(INPUT_ENCODING);
}
var options = {
uri: "https://apiv2.twitcasting.tv/users/yasalabo/current_live",
headers: {
'Accept':'application/json',
'X-Api-Version':'2.0',
'Authorization':'Bearer ' + deciphered,
},
json: true
};
request.get(options, function(error, response, body){
if(body.error == undefined){
var now = new Date();
var year = now.getFullYear();
var mon = now.getMonth() + 1;
var day = now.getDate();
var todays = String(year) + String(mon) + String(day);
var movieid = JSON.parse(fs.readFileSync('./api/twitcas/movieid.json'));
if(typeof movieid[todays] === 'undefined'){
movieid[todays] = {id : ''};
}
var movieId = body.movie.id;
msg.reply('動画IDを取得しました!');
movieid[todays].id = movieId;
var charset = 'utf-8';
var place = './api/twitcas/movieid.json';
fs.writeFileSync(place, JSON.stringify(movieid), charset);
}else if(body.error.code == 404){
msg.reply('現在配信していません。');
}
});
}
}
if(msg.channel.id === '000000000000000000'){
var userId = msg.author.id;
var now = new Date();
var year = now.getFullYear();
var mon = now.getMonth() + 1;
var day = now.getDate();
var todays = String(year) + String(mon) + String(day);
var movieid = JSON.parse(fs.readFileSync('./api/twitcas/movieid.json'));
var twitcas = JSON.parse(fs.readFileSync('./api/twitcas/token.json'));
msg.delete();
if(typeof twitcas[userId] === 'undefined'){
if(msg.author.bot){
return;
}
msg.delete();
msg.reply('先に<#000000000000000000>でユーザー認証を済ませてください!');
return;
}else if(typeof movieid[todays] === 'undefined'){
if(msg.author.bot){
return;
}
msg.delete();
msg.reply('先に<#000000000000000000>で`!getID`を入力してください!');
return;
}
var token = twitcas[userId].token;
var decipher = crypto.createDecipher(ALGORITHM, KEY);
var deciphered = decipher.update(token, OUTPUT_ENCODING, INPUT_ENCODING );
deciphered += decipher.final(INPUT_ENCODING);
var movieID = movieid[todays].id;
var options = {
url: 'https://apiv2.twitcasting.tv/movies/' + movieID + '/comments',
headers: {
'Accept':'application/json',
'X-Api-Version':'2.0',
'Content-Type':'application/json',
'Authorization':'Bearer ' + deciphered,
},
body: {
"comment": msg.content,
"sns": "none"
},
json: true
};
request.post(options, function(error, response, body){
});
}
client.login('XXXXXXXXXXXXXXXX');