-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws_remake.js
84 lines (71 loc) · 2.41 KB
/
ws_remake.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
$(function(){
ws.init();
})
var ws = {
bot_token: '',
gateway_url: 'wss://gateway.discord.gg/?v=6&encoding=json',
send_ready: false,
contact_owner: false,
init: function() {
ws.getToken();
},
getToken: function() {
var token = prompt("Pega el token de tu bot aquí");
if(token.trim().length > 0) {
ws.bot_token = token;
ws.connect();
} else {
ws.getToken();
}
},
connect: function() {
cnt = new WebSocket(ws.gateway_url);
cnt.onmessage = ws.messageHandler;
cnt.onclose = ws.connect;
},
login: function() {
data = {
"op" : 2,
"d" : {
"token" : ws.bot_token,
"properties" : {
"$os" : "browser",
"$browser" : "chrome",
"$device" : "cloud9"
},
"compress" : false
}
};
cnt.send(JSON.stringify(data));
},
messageHandler: function(msg){
json = JSON.parse(msg.data);
if(json.op == 10) {
ws.login();
} else if(json.op == 0) {
switch(json.t) {
case 'READY':
if(!ws.send_ready) {
$('#chatbox').append("<tr> <td colspan='3'> Bot Iniciado! </td> </tr>");
ws.send_ready = true;
}
break; // ==========================
case 'MESSAGE_CREATE':
u = json.d.author;
m = json.d.content;
t = json.d.timestamp;
ws.showMessage( u.avatar, u.id, u.username, m, t );
break; // ==========================
}
}
},
showMessage: function(avatar, user_id, user_name, msg, timestamp){
timestamp = ws.formatTimestamp(timestamp);
avatar = "<img src='https://cdn.discordapp.com/avatars/" + user_id + "/" + avatar + ".png' class='avatar'>";
$('#chatbox').append("<tr class='small'> <td>"+avatar+" "+user_name+"</td> <td>"+msg+"</td> <td>"+timestamp+"</td> </tr> ");
},
formatTimestamp: function(string) {
str = string.split('T');
return str[0]+' '+(str[1].split('.'))[0]+' +'+(str[1].split('+'))[1];
}
}