-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
96 lines (78 loc) · 3.27 KB
/
index.js
File metadata and controls
96 lines (78 loc) · 3.27 KB
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
import WebSocket from "ws";
import dotenv from "dotenv";
import fs from "node:fs";
import path from "node:path";
import { Collection } from "@discordjs/collection";
import { Reply } from "./utils/Message.js";
import { IntReply } from "./utils/Interactions.js"
import mongoose from "mongoose";
import "reflect-metadata"
import { initServer } from "./localServer.js";
const ws = new WebSocket("wss://gateway.discord.gg/?v=10&encoding=json");
dotenv.config()
var interval = 0, lastPing;
ws.events = new Map();
ws.commands = new Collection();
ws.guildMap = new Map();
ws.pingMs = "Cargando ping...";
ws.guildCount = 0;
ws.loadingState = true;
ws.loadingStart = Date.now();
const payload = {
op: 2,
d: {
token: process.env["token"],
intents: 46727,
properties: {
device: "pc",
browser: "chrome",
os: process.platform
},
presence: {
activities: [{
name: "Mantenimiento",
type: 3
}],
status: "dnd",
afk: false,
since: Date.now()
}
}
}
ws.on("open", () => ws.send(JSON.stringify(payload)));
fs.readdirSync(path.resolve("./events/")).filter(f => f.endsWith(".js"))
.forEach(async file => {
const { default: event } = await import(`./events/${file}`);
ws.events.set(event.name, event);
});
fs.readdirSync(path.resolve("./commands/")).filter(f => f.endsWith(".js"))
.forEach(async file => {
const { default: command } = await import(`./commands/${file}`);
ws.commands.set(command.name, command);
});
mongoose.connect(`mongodb://${process.env["mongoUser"]}:${process.env["mongoPwd"]}@ac-n8wr7zc-shard-00-00.8oahhzm.mongodb.net:27017,ac-n8wr7zc-shard-00-01.8oahhzm.mongodb.net:27017,ac-n8wr7zc-shard-00-02.8oahhzm.mongodb.net:27017/?ssl=true&replicaSet=atlas-14abre-shard-0&authSource=admin&retryWrites=true&w=majority`, { connectTimeoutMS: 60000 });
const MongoDB = mongoose.connection;
MongoDB.on("disconnect", () => mongoose.connect(`mongodb://${process.env["mongoUser"]}:${process.env["mongoPwd"]}@ac-n8wr7zc-shard-00-00.8oahhzm.mongodb.net:27017,ac-n8wr7zc-shard-00-01.8oahhzm.mongodb.net:27017,ac-n8wr7zc-shard-00-02.8oahhzm.mongodb.net:27017/?ssl=true&replicaSet=atlas-14abre-shard-0&authSource=admin&retryWrites=true&w=majority`, { connectTimeoutMS: 60000 }))
MongoDB.on("error", console.error.bind(console, "Connection error to MongoDB: "))
MongoDB.on("open", () => console.info("Connected successfully to MongoDB"));
initServer(ws)
ws.on("message", (data) => {
const payload = JSON.parse(data);
const { d, t, op } = payload;
switch (op) {
case 10:
interval = heartbeat(d.heartbeat_interval);
console.info("Heartbeat Interval", d.heartbeat_interval);
case 11:
if (!lastPing) return;
ws.pingMs = Date.now() - lastPing;
}
const event = ws.events.get(t);
if (!event) return;
else event.run(ws, d)
});
ws.on("close", (code, reason) => {
console.info(`Client has just closed!\n\nInfo:\nClose Code: ${code}\nWith ${reason ? reason + " reason" : "no reason"}\n\nExiting now...`);
process.exit(0)
})
const heartbeat = (ms) => { return setInterval(() => { ws.send(JSON.stringify({ op: 1, d: null })); lastPing = Date.now(); }, ms); };