-
Notifications
You must be signed in to change notification settings - Fork 1
/
jukebot.html
136 lines (121 loc) · 4.19 KB
/
jukebot.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>My bot</title>
<script src="./config.js"></script>
<script src="./deps/tmi.min.js"></script>
</head>
<body>
<audio id="player" />
</body>
<script>
let cooldown = {
'chatters': {
// '<pseudo>': <date last command execution>
},
'tracks': {
// '<command>': <date last command execution>
},
'command': 0,
}
let isPlaying = false;
const player = document.getElementById('player');
player.addEventListener('ended', () => {
isPlaying = false;
});
const chat = new window.tmi.Client({
options: { debug: true },
connection: {
reconnect: true,
secure: true
},
// identity: {
// username: 'bot-name',
// password: 'oauth:my-bot-token'
// },
channels: [ config['channel'] ]
});
chat.connect();
chat.on('message', (channel, tags, message, self) => {
console.log(tags);
let data = message.split(' ');
let now = Date.now() / 1000.;
// First thing first, stop if requested
if((tags['mod'] === true) || (tags['username'] === config['channel'])) {
if (data[0] === '!stop') {
player.pause();
isPlaying = false;
}
}
// <prefix> <command>
if (data[0] !== config['prefix'])
// Fail-fast si le prefix est faux
return;
// Vérifie les droits des utilisateurs
if(config['rights']['user'] === false) {
if(tags['username'] === config['channel']) {
console.log(`${config['channel']} est le boss, lets go`);
}
else if((config['rights']['mods'] === true) && (tags['mod'] === true)) {
console.log(`${tags['username']} est un modo, lets go`);
}
else if((config['rights']['subs'] === true) &&
('premium' in tags['badges'] || 'subscriber' in tags['badges'])) {
console.log(`${tags['username']} est un sub, lets go`);
}
else if((config['rights']['vips'] === true) && ('vip' in tags['badges'])) {
console.log(`${tags['username']} est un vip, lets go`);
} else {
return;
}
}
// Vérifie le cooldown du bot
if( cooldown['command'] > (now - config['cooldown']['command'])) {
console.error(`The use of the command is violating the ${config['cooldown']['command']}s cooldown`);
return;
}
// Vérifie qu'un fichier n'est pas déjà en lecture
if(isPlaying) {
console.error('Un morceau est déjà en lecture');
return;
}
let command = data[1];
// on parcours le tableau 'tracks'
for(let idx in tracks) {
// Si la command est trouvée
if(command === tracks[idx]['command']) {
console.log(`Lecture de ${tracks[idx]['file']} par ${tags['username']}`);
// Vérifions si l'utilisateur ne dépasse pas le cooldown
let user = tags['username'];
if (user in cooldown['chatters']) {
let ts = cooldown['chatters'][user];
if( ts > (now - config['cooldown']['user'])) {
console.error(`${user} is violating the ${config['cooldown']['user']}s cooldown`);
return;
}
}
// Vérifions si la track ne dépasse pas le cooldown
if (command in cooldown['tracks']) {
let ts = cooldown['tracks'][command];
if( ts > (now - config['cooldown']['track'])) {
console.error(`Track ${command} is violating the ${config['cooldown']['track']}s cooldown`);
return;
}
}
cooldown['chatters'][user] = now; // Met à jour le ts du user
cooldown['tracks'][command] = now;
cooldown['command'] = now;
player.src = `file:///${tracks[idx]['file']}`;
player.play()
.then(() => {
isPlaying = true;
})
.catch(() => {
isPlaying = false;
});
}
}
});
</script>
</html>