-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBot.js
78 lines (65 loc) · 2.26 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
import NDK from "@nostr-dev-kit/ndk";
import { Client, GatewayIntentBits, Partials } from "discord.js";
import {} from "dotenv/config";
import fs from "fs";
import { connect } from "mongoose";
import { log } from "./src/handlers/log.js";
import { requiredEnvVar } from "./src/utils/helperFunctions.js";
import WebSocket from "ws";
Object.assign(global, { WebSocket });
export const connectedNdk = new NDK({
explicitRelayUrls: ["wss://relay.lawallet.ar/", "wss://relay.hodl.ar/"],
autoConnectUserRelays: false,
autoFetchUserMutelist: false,
});
const mongoURI = requiredEnvVar("MONGODB_URI");
const botToken = requiredEnvVar("BOT_TOKEN");
export const knownRelays = [];
async function runBot() {
// Create a new Client with the Guilds intent
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessageReactions,
],
partials: [Partials.Message, Partials.Channel, Partials.Reaction],
});
// Fetch all js files in ./events
const events = fs
.readdirSync("./src/events")
.filter((file) => file.endsWith(".js"));
// Check for an event and execute the corresponding file in ./events
for (let event of events) {
// The #events ES6 import-abbreviation is defined in the package.json
// Note that the entries in the list of files (created by readdirSync) end with .js,
// so the abbreviation is different to the #commands abbreviation
const eventFile = await import(`#events/${event}`);
// But first check if it's an event emitted once
if (eventFile.once) {
client.once(eventFile.name, (...args) => {
eventFile.invoke(...args);
});
} else {
client.on(eventFile.name, (...args) => {
eventFile.invoke(...args);
});
}
}
// Login with the credentials stored in .env
client.login(botToken);
connectedNdk.pool.relays.forEach(async (relay) => {
log(`conectando relay: ${relay.url}`, "done");
knownRelays.push(relay.url);
await relay.connect();
});
// setInterval(validateRelaysStatus, 30000);
log("Started connecting to MongoDB...", "warn");
connect(mongoURI)
.then(() => {
log("MongoDB is connected!", "done");
})
.catch((err) => {
log("Error conectándose a MongoDB: ", err, "err");
});
}
runBot();