-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.js
247 lines (204 loc) · 6.92 KB
/
bot.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//Client criation
const tmi = require('tmi.js');
const fs = require('fs');
function botStart()
{
let myinfo = fs.readFileSync(__dirname + '/info/info.json');
let info = JSON.parse(myinfo);
let channel = info.Channel;
var myCredentials = fs.readFileSync(__dirname + '/electron-info/credentials.json');
var credentials = JSON.parse(myCredentials);
const client = new tmi.Client({
options: { debug: true },
connection: {
secure: true,
reconnect: true
},
identity: {
username: credentials.login,
password: "oauth:" + credentials.token
},
channels: [ channel ]
});
//Consts
const active = new Map();
const mediaHandler = require(__dirname + '/server/mediaHandler.js');
//Vars / Lets
let prefix = info.Prefix;
let mypointconfig = fs.readFileSync(__dirname + '/info/points.json');
let data = fs.readFileSync(__dirname + '/data/data.json');
let myrewards = fs.readFileSync(__dirname + '/info/rewards.json');
let pointconfig = JSON.parse(mypointconfig);
let viewer = JSON.parse(data);
let keys = Object.keys(viewer);
let rewards = JSON.parse(myrewards);
var minutes = pointconfig.Minutes * 60000;
var i = 0;
// ON START
console.log(info);
console.log(pointconfig);
require(__dirname + '/server/server.js');
client.connect();
//server.HoldServer();
client.on('connected', function(address, port) {
for(i; i < keys.length; i++)
{
var username = keys[i];
viewer[username].online = 0;
};
client.say(channel, "/me Acabei de me conectar a este canal!");
});
// COMMANDS
client.on('message', async function(channel, user, message, self) {
if(self) return;
if(user['message-type'] === "whisper") return;
if(!message.startsWith(prefix)) return;
const args = message.substr(prefix.length).trim().split(/ +/g);
var command = args.shift().toLowerCase();
try{
let ops = {
active: active
}
if(command === pointconfig.Name){
command = "points";
};
data = fs.readFileSync(__dirname + '/data/data.json');
viewer = JSON.parse(data);
runCustomCom(command);
runMediaRequest(command);
let commands = require(__dirname + '/commands/' + command + '.js');
commands.run(client, channel, user, message, self, args, ops, viewer);
} catch(err) {
//console.log(err);
} finally {}
});
// Run Custom commands
function runCustomCom(command)
{
var mycommands = fs.readFileSync(__dirname + '/data/customcommands.json');
var customcommands = JSON.parse(mycommands);
var comkeys = Object.keys(customcommands);
for(i = 0; i < comkeys.length; i++){
if(command === comkeys[i]){
if(customcommands[command].hasOwnProperty("contador")){
customcommands[command].contador++;
fs.writeFileSync(__dirname + '/data/customcommands.json', JSON.stringify(customcommands, null, 2));
var camp = customcommands[command].resposta.split(" ");
for(i = 0; i < camp.length; i++){
if(camp[i] === "${cont}"){
camp[i] = customcommands[command].contador;
};
};
customcommands[command].resposta = camp.join(" ");
};
client.say(channel, "/me " + customcommands[command].resposta);
return;
};
};
};
// Run Media Request
function runMediaRequest(media)
{
fs.readdir(__dirname + '/media', (err, files) => {
for(i = 0; i < files.length; i++)
{
if(media === files[i].split('.').shift())
{
if(/\.(mp4|webm)$/i.test("." + files[i].split('.').pop()))
{
mediaHandler.send(files[i], "video");
}
else if(/\.(mp3|aac|ogg|flac|wav)$/i.test("." + files[i].split('.').pop()))
{
mediaHandler.send(files[i], "sound");
}
else if(/\.(png|jpeg|jpg|gif|apng)$/i.test("." + files[i].split('.').pop()))
{
mediaHandler.send(files[i], "image");
}
}
};
});
}
// DATA
function createUserData(user){
if(!viewer[user]){
viewer[user] = {
online: 1,
points: 0
};
} else { return };
}
function givePoints(user){
function intervalFunc() {
if(viewer[user].online === 1){
viewer[user].points += pointconfig.Quantity;
if(user.subscriber === true){
//console.log("SUB POINTS");
viewer[user].points += pointconfig.Quantity;
};
fs.writeFileSync(__dirname + '/data/data.json', JSON.stringify(viewer, null, 2));
} else {clearInterval(pointsInterval); };
};
var pointsInterval = setInterval(intervalFunc, minutes);
};
client.on('join', function(channel, user){
createUserData(user);
//console.log("JOINED: " + user);
viewer[user].online = 1;
fs.writeFileSync(__dirname + '/data/data.json', JSON.stringify(viewer, null, 2));
givePoints(user, pointconfig);
});
client.on('part', function(channel, user){
//console.log("PARTED: " + user);
if(viewer[user])
{
viewer[user].online = 0;
fs.writeFileSync(__dirname + '/data/data.json', JSON.stringify(viewer, null, 2));
}
});
// ALERTS
client.on("subscription", (channel, user, method, message, userstate) => {
client.say(channel, "/me <3 PogChamp PogChamp PogChamp Obrigado pelo subscribe @" + user);
createUserData(user);
viewer[user].points += rewards.Sub;
fs.writeFileSync(__dirname + '/data/data.json', JSON.stringify(viewer, null, 2));
});
client.on("resub", (channel, user, streakMonths, message, userstate, methods) => {
let cumulativeMonths = ~~userstate["msg-param-cumulative-months"];
let share = ~~userstate["msg-param-should-share-streak"];
if(share === 1 && message)
{
client.say(channel, "/me <3 PogChamp PogChamp PogChamp Obrigado pelo resub @" + user + " . Subscreveu um total de " + cumulativeMonths + " meses e vai numa sequência de " + streakMonths + " meses.");
}
else
{
client.say(channel, "/me <3 PogChamp PogChamp PogChamp Obrigado pelo resub @" + user + " . Subscreveu um total de " + cumulativeMonths + " meses.");
}
createUserData(user);
viewer[user].points += rewards.Sub;
fs.writeFileSync(__dirname + '/data/data.json', JSON.stringify(viewer, null, 2));
});
client.on('cheer', function(channel, user){
client.say(channel, "/me <3 PogChamp TwitchUnity PogChamp Obrigado pelos " + user.bits + " bits @" + user["display-name"]);
createUserData(user);
viewer[user].points += (user.bits * rewards.Bit);
fs.writeFileSync(__dirname + '/data/data.json', JSON.stringify(viewer, null, 2));
});
client.on("raided", (channel, username, viewers) => {
client.say(channel, "/me twitchRaid twitchRaid @" + user + " acabou de dar raid com " + viewers + " viewers.");
if(viewers >= 2)
{
createUserData(user);
viewer[user].points += (user.bits * rewards.Raid);
fs.writeFileSync(__dirname + '/data/data.json', JSON.stringify(viewer, null, 2));
}
});
// EXIT
function botDisconnect()
{
client.disconnect();
}
exports.botDisconnect = botDisconnect;
};
exports.botStart = botStart;