-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
122 lines (115 loc) · 3.98 KB
/
index.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
// Live chat with other people using socket.io
const io = require('socket.io-client');
const readline = require('readline');
const colors = require('colors');
const term = require('terminal-kit').terminal;
const checkID = require('./modules/checkID');
const time = require('node-get-time');
const versionControl = require('./modules/versionControl');
const serverList = require('./modules/serverList.js');
const settings = require('./settings.json');
let user;
let socket;
// Settings for readline node.
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
console.log("Required all the modules... Starting up.");
// Checking id of the user inside another script.
checkID((res) => {
user = res; //Sets the user to the response given when checking the id.
});
MainMenu = () => {
term( '\n' ).eraseLineAfter.green('Console Chat');
var items = [
'Server List' ,
'Change Username' ,
'Randomize ID'
] ;
term.singleColumnMenu( items , function( error , response ) {
if(response.selectedIndex === 0){
ServerList();
}
if(response.selectedIndex === 1){
ChangeUsername();
}
} ) ;
}
setTimeout(MainMenu, 500);
ChangeUsername = () => {
console.log("Soon!");
}
ServerList = () => {
serverList((data) => {
// Data is our variable for the server list given as a json array.
term.green('\nChoose a server to connect to...');
if(data !== '[]' || data !== undefined){ //If there are servers online, if the array given by the main server is not empty.
list = JSON.parse(data);
term.singleColumnMenu(list, (err, res) => {
term('\n').eraseLineAfter.green("Connecting to: ", res.selectedText);
chat(res.selectedText);
});
}else {
console.log("\nThere are not any servers online at the moment.. :(".red);
}
});
}
MainMenu();
let onlineUser = user.username; // Easy to access name of the current user.
sendMessage = (message) => {
// Send the message, (sent it when the ENTER key is activated on the clients keyboard..)
if(message === '/leave'){
socket.disconnect();
console.log("Disconnected from server...".red);
ServerList();
}else {
socket.emit('newMSG', {
user: onlineUser,
id: user.id,
message: message,
});
rl.prompt();
}
}
chat = (adress) => {
socket = io('http://'+adress+':4000'); //Connects to the right socket or ip adress...
socket.on('connected',(data) => { // When we are connected.
console.log("\nThere are " + data.onlineAmount+ " other users inside this global chatroom!");
term('\n').eraseLineAfter.red("Use /help for available commands");
console.log("")
socket.emit('newUser', { //Register a new user to the server.
user: user.username,
id: user.id
});
// Set up prompt and more....
rl.setPrompt("> ");
rl.prompt();
rl.on('line', (message) => {
sendMessage(message);
});
// When we recieve a message
socket.on('recieveMSG', (data) => {
let user = data.user;
let id = data.id;
let message = data.message;
let admin = data.isAdmin;
if(user === undefined){
user = "[".white+"SERVER".red+"]".white;
if(message === "You have been kicked!"){
ServerList();
}
}
let timestamp = '';
if(settings.timestamp === true) timestamp = '['+time.now().gray+'] ';
if(admin && settings.adminprefix === true){
console.log(timestamp+"[".white+"ADMIN".yellow+"] ".white+ user+ ": " + message);
}
else {
console.log(timestamp+user+ ": " + message);
}
// Prompt the prefix for the line (>)
rl.prompt();
});
});
}