-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
92 lines (76 loc) · 2.63 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
// Node libraries
const fs = require("node:fs");
const path = require("node:path");
// Sequelize
const Sequelize = require("sequelize");
// Dates
const dayjs = require("dayjs");
// Node scheduler
const schedule = require("node-schedule");
// Setup segfault handler
const SegfaultHandler = require("segfault-handler");
SegfaultHandler.registerHandler("crash.log");
// Require the necessary discord.js classes
const { Client, Intents, Collection } = require("discord.js");
const { token } = require("./config.json");
// Create a new client instance
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MEMBERS],
presence: {
status: "online",
activities: [
{
name: "with your mother, bro.",
type: "PLAYING",
},
],
},
});
// Connect a database
const sql = new Sequelize("database", "user", "password", {
host: "localhost",
dialect: "sqlite",
logging: false,
// SQLite only
storage: "haljr.sqlite",
});
// Instantiate all models
client.models = new Collection();
const modelsPath = path.join(__dirname, "models");
const modelFiles = fs.readdirSync(modelsPath).filter((file) => file.endsWith(".js"));
for (const file of modelFiles) {
const filePath = path.join(modelsPath, file);
const model = require(filePath);
let sqldef = sql.define(model.data.name.toLowerCase(), model.data.schema);
console.log(`Adding model: ${model.data.name} -> ${typeof sqldef} (${typeof sql})`);
client.models.set(model.data.name, sqldef);
console.log(`Set: ${model.data.name} = ${client.models.get(model.data.name)}`);
}
// Globalize scheduler
client.schedule = schedule;
client.scheduled = new Collection();
// Add a commands collection
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);
// Set a new item in the Collection
// With the key as the command name and the value as the exported module
client.commands.set(command.data.name, command);
}
// Load events
const eventsPath = path.join(__dirname, "events");
const eventFiles = fs.readdirSync(eventsPath).filter((file) => file.endsWith(".js"));
for (const file of eventFiles) {
const filePath = path.join(eventsPath, file);
const event = require(filePath);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args));
}
}
// Login to Discord with your client's token
client.login(token);