-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
321 lines (269 loc) · 12.6 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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
const { Client, IntentsBitField, Events, Collection, EmbedBuilder } = require("discord.js");
const { Pagination } = require("discordjs-button-embed-pagination");
const { Routes } = require("discord-api-types/v9");
const { REST } = require("@discordjs/rest");
const path = require("node:path");
const fs = require("node:fs");
const config = require("./config.js");
const { ApiFiveM, Guild } = require("./server/info.js");
const rest = new REST({ version: "10" }).setToken(config.BOT_TOKEN);
const intents = new IntentsBitField();
intents.add(IntentsBitField.Flags.GuildMessages, IntentsBitField.Flags.Guilds);
const client = new Client({
intents: intents,
partials: ["MESSAGE", "CHANNEL", "REACTION"],
});
var IPPP, Iname;
console.logCopy = console.log.bind(console);
console.log = function (data) {
this.logCopy(
`|${new Date().toLocaleString(config.Timezone, { hour: "2-digit", minute: "2-digit", second: "2-digit", hour12: false })}|`,
data
);
};
// -------------------------
function split_data(data) {
const identifiers = {};
data.identifiers.forEach(identifier => {
const [key, value] = identifier.split(':');
identifiers[key] = value;
});
return identifiers;
}
function getCheckCFXIP() {
return config.URL_CFX ? config.URL_CFX : IPPP ?? config.URL_SERVER;
}
async function deployCommands() {
const commands = fs.readdirSync("./commands").filter((file) => file.endsWith(".js")).map((file) => require(`./commands/${file}`).data.toJSON());
try {
console.log(`Started refreshing ${commands.length} application (/) commands.`);
const data = await rest.put(Routes.applicationCommands(client.user.id), { body: commands });
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
console.error(error);
}
}
// -------------------------
let cachedData = null;
async function DaTa(ip) {
const server = config.URL_CFX ? await new Guild().getServerInfo(ip) : await new ApiFiveM(ip).checkOnlineStatus();
if (server == false) {
return { server };
} else if (server) {
const [players, dynamic] = await Promise.all([
config.URL_CFX ? server.Data.players : await new ApiFiveM(ip).getPlayers(),
config.URL_CFX ? server.Data : await new ApiFiveM(ip).getDynamic()
]);
const { clients: playersonline, sv_maxclients: maxplayers, hostname: hostnametext } = dynamic;
const hostname = hostnametext.replace(/[^a-zA-Z]+/g, " ").substring(0, 40);
cachedData = { server, players, playersonline, maxplayers, hostname };
return { server, players, playersonline, maxplayers, hostname };
} else {
return cachedData;
}
}
const activity = async () => {
const { server, players, playersonline, maxplayers, hostname } = await DaTa(getCheckCFXIP());
let status;
if (server) {
let namef = players && players.filter((player) => player.name.normalize().toLowerCase().includes((Iname ?? config.NAMELIST).normalize().toLowerCase()));
status = playersonline > 0 ? `💨 ${playersonline}/${maxplayers} ${namef && namef.length ? `👮 ${namef.length} ` : ""}🌎 ${hostname}` : "⚠ Wait for Connect";
} else {
status = "🔴 Offline";
}
client.user.setPresence({ activities: [{ name: status }] });
if (config.Log_update) console.log(status);
}
// -------------------------
let counter = 0;
client.on("ready", async () => {
console.log(`Logged in as ${client.user.tag}`);
client.user.setPresence({
status: config.STATUS,
});
deployCommands();
const loop = async () => {
if (counter >= 10) {
if (config.Log_update) console.log("wait 1 min");
await new Promise((resolve) => setTimeout(resolve, 60000));
counter = 0;
}
activity();
counter++;
setTimeout(loop, config.UPDATE_TIME);
};
loop();
});
// -------------------------
client.commands = new Collection();
const commandsPath = path.join(__dirname, "commands");
const commandFiles = fs
.readdirSync(commandsPath)
.filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
client.commands.set(command.data.name, command);
}
client.on(Events.InteractionCreate, async (interaction) => {
if (!interaction.isChatInputCommand()) return;
let commandName = interaction.commandName;
let command = client.commands.get(commandName);
if (!command) return;
try {
if (commandName === "set-count") {
const text = interaction.options.data[0].value;
Iname = text;
console.log(`${commandName}: ${text} completed`);
const message = await interaction.reply({ content: `${commandName}: to ${text}`, fetchReply: true });
message.react("👌");
}
if (commandName === "set-ip") {
const text = interaction.options.data[0].value;
if (config.URL_CFX) {
const message = await interaction.reply({ content: `You use Cfx url need to set \`"URL_CFX": ""\``, fetchReply: true });
message.react("❌");
console.log(`${commandName}: You use Cfx url need to set "URL_CFX": ""`);
} else if (await new Guild().validateIpAndPort(text)) {
IPPP = text;
const message = await interaction.reply({ content: `${commandName}: to ${text}`, fetchReply: true });
message.react("👌");
console.log(`${commandName}: ${text} completed`);
} else {
const message = await interaction.reply({ content: `IP ${text} incorrect`, fetchReply: true });
message.react("❌");
console.log(`${commandName}: ${text} incorrect`);
}
}
if (commandName === "all") {
await interaction.deferReply();
const { players } = await DaTa(getCheckCFXIP());
const result = players && players.map((player, index) => `${index + 1}. ${player.name} | ID : ${player.id} | Ping : ${player.ping}\n`);
const chunksArr = await new Guild().chunkArray(result, 50);
if (chunksArr) {
const embeds = chunksArr.map((chunk) => new EmbedBuilder()
.setColor(config.COLORBOX)
.setTitle(`All_players | ${config.SERVER_NAME}`)
.setDescription(chunk.join("\n"))
);
await new Pagination(interaction.channel, embeds, "Part").paginate();
await interaction.editReply({ content: 'All players list has been generated.', ephemeral: false });
} else {
await interaction.reply({ content: `❌Offline`, fetchReply: true });
}
console.log(`${commandName}: completed`);
}
if (commandName === "search-id") {
const { players } = await DaTa(getCheckCFXIP());
const text = interaction.options.data[0].value;
const num = text.match(/[0-9]/g).join("").valueOf();
const playerdata = players.filter(player => player.id == num);
const promises = playerdata.map(async player => {
const { discord } = split_data(player);
const user = await client.users.fetch(discord);
const username = user ? user.username : 'Unknown';
return `\`\`\`${player.name}\`\`\` \`\`\`ID: ${player.id}\nPing: ${player.ping}\nDiscord:\n UserID: ${discord}\n Username: ${username}\`\`\``;
});
const results = await Promise.all(promises);
const result = results.join("\n");
const embed = new EmbedBuilder()
.setColor(config.COLORBOX)
.setTitle(`Search player | ${config.SERVER_NAME}`)
.setDescription(result.length > 0 ? result : "No Players")
.setTimestamp();
await interaction.reply({ embeds: [embed] });
console.log(`${commandName}: completed`);
}
if (commandName === "search-info") {
const text = interaction.options.data[0].value;
const embed = new EmbedBuilder();
if (await new Guild().validateIpAndPort(text)) {
await new ApiFiveM(text).checkOnlineStatus()
.then(async (server) => {
const infoplayers = server ? await new ApiFiveM(text).getDynamic() : null;
const iport = await new Guild().ipAddress(text);
const fields = server ? [
{ name: "**Server Status**", value: `\`\`\`✅Online\`\`\`` },
{ name: "**Server Name**", value: `\`\`\`${(infoplayers.hostname).replace(/[^a-zA-Z]+/g, " ")}\`\`\`` },
{ name: "**Languages**", value: `\`\`\`${iport.timezone}\`\`\`` },
{ name: "**Online Players**", value: `\`\`\`${infoplayers.clients}/${infoplayers.sv_maxclients}\`\`\`` },
] : [
{ name: "**Server Status**", value: `\`\`\`❌Offline or Invalid IP\`\`\`` },
{ name: "**Online Players**", value: `\`\`\`-/-\`\`\`` },
];
embed.setColor(config.COLORBOX)
.setTitle(`search-info: \`${text}\``)
.addFields(fields)
.setTimestamp();
await interaction.reply({ embeds: [embed] });
console.log(`${commandName}: ${text} ${server ? "online" : "offline"}`);
})
.catch((err) => console.log(err));
} else if (await new Guild().checkMessage(text)) {
await new Guild().getServerInfo(text).then(async (server) => {
const iport = await new Guild().domainAddress(server.Data.connectEndPoints);
const icon = await new Guild().get_icon(server.EndPoint, server.Data.iconVersion)
const fields = server ? [
{ name: "**Server Status**", value: `\`\`\`✅Online\`\`\`` },
{ name: "**Server Name**", value: `\`\`\`${(server.Data.vars.sv_projectName).replace(/[^a-zA-Z]+/g, " ")}\`\`\`` },
{ name: "**IP:Port**", value: `\`\`\`${iport.status == "success" ? `${iport.query}:${iport.zip}` : server.Data.connectEndPoints}\`\`\`` },
{ name: "**Owner Name**", value: `[${server.Data.ownerName}](${server.Data.ownerProfile})` },
{ name: "**Server Connect**", value: `https://cfx.re/join/${server.EndPoint}` },
{ name: "**Private**", value: `\`\`\`${server.Data.private}\`\`\`` },
{ name: "**Online Players**", value: `\`\`\`${server.Data.clients} / ${server.Data.sv_maxclients}\`\`\`` },
{ name: "**Languages**", value: `\`\`\`${iport.timezone}\`\`\`` },
{ name: "**Last Update**", value: `\`\`\`${server.Data.lastSeen}\`\`\`` },
] : [
{ name: "**Server Status**", value: `\`\`\`❌Offline or Invalid IP\`\`\`` },
{ name: "**Online Players**", value: `\`\`\`-/-\`\`\`` },
];
embed.setColor(config.COLORBOX)
.setTitle(`search-info: \`${text}\``)
.setThumbnail(icon)
.addFields(fields)
.setTimestamp()
.setFooter({ text: server.Data.server, iconURL: icon });
await interaction.reply({ embeds: [embed] });
console.log(`${commandName}: ${text} ${server ? "online" : "offline"}`);
})
.catch((err) => console.log(err));
} else {
embed.setColor(config.COLORBOX)
.addFields([{ name: "**Invalid input IP:Port or cfx.re/join/xxxxx**", value: `\`${text}\`` }])
.setTimestamp();
await interaction.reply({ embeds: [embed] });
console.log(`${commandName}: ${text} incorrect`);
}
}
if (commandName === "search-name") {
const { players } = await DaTa(getCheckCFXIP());
const text = interaction.options.data[0].value.normalize().toLowerCase();
const playerdata = players.filter(person => person.name.normalize().toLowerCase().includes(text));
const result = playerdata.map((player, index) => `${index + 1}. ${player.name} | ID : ${player.id} | Discord : ${split_data(player).discord} | Ping : ${player.ping}\n`).join('');
const embed = new EmbedBuilder()
.setColor(config.COLORBOX)
.setTitle(`Search player | ${config.SERVER_NAME}`)
.setDescription(result.length > 0 ? result : "No Players")
.setTimestamp();
interaction.reply({ embeds: [embed] });
console.log(`${commandName}: ${text} completed`);
}
} catch (error) {
console.error(error);
await interaction.reply({
content: "There was an error while executing this command!",
ephemeral: true,
});
}
});
// -------------------------
client
.login(config.BOT_TOKEN)
.then(null)
.catch(() => {
console.log(
"The token you provided is invalided. Please make sure you are using the correct one from https://discord.com/developers/applications!"
);
console.error();
process.exit(1);
});