-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.js
228 lines (202 loc) · 5.91 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
// Import the discord.js module
const Discord = require('discord.js');
//Import quakeworld module
const quakeworld = require('quakeworld');
// Create an instance of a Discord client
const client = new Discord.Client();
// The token of your bot - https://discordapp.com/developers/applications/me
const token = 'TOKEN';
var hostname = "";
var serveripfull = "Your server ip and port, to show where to connect in Discord";
var mode = "";
var map = "";
var playerstotal = "";
var player = [];
var spectator = [];
var playerString = "";
var spectatorString = "";
var amountPlayers = 0;
var amountSpectators = 0;
// The ready event is vital, it means that your bot will only start reacting to information
// from Discord _after_ ready is emitted
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
//Check the messages user send
client.on('message', message =>
{
//Do nothing if message is from a bot
if (message.author.bot) return;
//If message has !qserver (And only "!qserver")
if (message.content === '!qserver')
{
//Check if user has permissions
//Default rolename is "Quakemod". You can add more roles to the array if you wish!
if(!message.member.roles.some(r=>["Quakemod"].includes(r.name)))
{
return message.reply("Sorry, you don't have permissions to use this!");
}
//Look for data
//Set your server ip and port here
quakeworld('QUAKESERVERIP', QUAKESERVERPORT, 'status', [31], function (err, data)
{
console.log("Reloading data!");
//Don't remove these, we have to refresh the data every time so nothing gets stuck
hostname = "";
mode = "";
map = "";
playerstotal = "";
player = [];
spectator = [];
playerString = "";
spectatorString = "";
amountPlayers = 0;
amountSpectators = 0;
//Error handling
if (err)
{
console.log('ERROR: ', err);
}
//Hostname
hostname = data.hostname;
//Map
map = data.map;
//Teamplay or FFA
if (data.teamplay == null)
{
mode = 'FFA';
}
else if (data.teamplay == '4')
{
mode = 'CTF';
}
else if (data.teamplay == '2')
{
mode = 'TDM';
}
else
{
mode = 'Unlisted';
}
//If no players, set first player and first spectator text
if (data.players === null)
{
player[0] = "No players.";
spectator[0] = "No spectators.";
}
//Check players and spectators
else if (data.players != null)
{
//Go through all players
for (var i = 0; i < data.players.length; i++)
{
//If player is not spectator
if (data.players[i].frags != 'S')
{
var team;
//Check if teamplay is on
if (data.teamplay != null)
{
//Add players team
team = "Team: " + data.players[i].team;
}
else
{
//No team
team = '';
}
//Add player to array
player[i] = '' + data.players[i].name + ' | Frags: ' + data.players[i].frags + ' | Ping: ' + data.players[i].ping + team;
}
}
//Look through the list again for spectators
for (var i = 0; i < data.players.length; i++)
{
//If player is spectator
if (data.players[i].frags === 'S')
{
//Add player to array
spectator[i] = '' + data.players[i].name;
}
}
}
//Make the player array into a spring
player.join('').split('');
for (var i = 0; i < player.length; i++)
{
if (player[i] === undefined)
{
//Do nothing
}
else
{
//Add players to playerstring, also raise amount of players by one
playerString += "" + player[i].toString() + "\n";
amountPlayers++;
}
}
//Make spectator array into a string
spectator.join('').split('');
for (var i = 0; i < spectator.length; i++)
{
if (spectator[i] === undefined)
{
//Do nothing
}
else
{
//Add spectators to spectatorstring, also raise amount of spectators by one
spectatorString += "" + spectator[i].toString() + "\n";
amountSpectators++;
}
}
//Total players is amount of players. Max players is maximum clients minus spectators
playerstotal = "" + amountPlayers + "/" + (data.maxclients - data.maxspectators);
//If undefined, no players
if (playerString === undefined || playerString === null || playerString === "")
{
playerString = "No players!";
spectatorString = "No spectators!";
}
//Total players into string
playerstotal = playerstotal.toString();
console.log('Done reloading data!');
console.log(playerString);
console.log(spectatorString);
//Message continues here
//STRINGS CAN NOT BE EMPTY! Even if string is "", it will crash the bot.
//To avoid this, make string to have somekind of text or symbol. " " should work, for example.
message.channel.send({embed:
{
color: 3447003,
author: {
name: client.user.username,
icon_url: client.user.avatarURL
},
title: hostname,
description: serverip,
fields: [{
name: "Map",
value: map
},
{
name: "Mode",
value: mode
},
{
name: "Players (" + playerstotal + ")",
value: playerString
}
],
timestamp: new Date(),
footer: {
icon_url: client.user.avatarURL,
text: "Last updated: "
}
}
});
});
}
});
// Log our bot in
client.login(token);