forked from ciabidev/sunfish-karoo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
116 lines (95 loc) · 3.4 KB
/
Copy pathindex.js
File metadata and controls
116 lines (95 loc) · 3.4 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// express health check
const express = require("express");
const app = express();
// This route just confirms the bot is online
app.get("/", (req, res) => {
res.send("✅ kuma is alive!");
});
// Render automatically assigns a port in process.env.PORT
const server = app.listen(process.env.PORT || 3000, () => {
console.log("🌐 Express keep-alive server running.");
});
// INDEX.JS COPY PASTE TEMPLATE
// Require the necessary discord.js classes
const fs = require('node:fs');
const path = require('node:path');
const { Client, Collection, GatewayIntentBits, SlashCommandSubcommandBuilder } = require('discord.js');
require('dotenv').config();
const { discordToken } = require('#config');
// Create a new client instance
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.MessageContent,
],
});
// Log in to Discord with your client's token
client.commands = new Collection();
const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);
for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter((file) => file.endsWith('.js'));
for (const file of commandFiles) {
const commandImport = `#commands/${folder}/${file.slice(0, -3)}`;
const command = require(commandImport);
if (command.data instanceof SlashCommandSubcommandBuilder) continue;
command.__import = commandImport; // this is needed for reloading commands
// Set a new item in the Collection with the key as the command name and the value as the exported module
if ("data" in command && "execute" in command) {
client.commands.set(command.data.name, command);
} else {
console.log(
`[WARNING] ${commandImport} is missing a required "data" or "execute" property.`
);
}
}
}
// load other files
const eventsPath = path.join(__dirname, 'events');
const eventFiles = fs.readdirSync(eventsPath).filter((file) => file.endsWith('.js'));
for (const file of eventFiles) {
const event = require(`#events/${file.slice(0, -3)}`);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args));
}
}
const modulesPath = path.join(__dirname, "src/modules");
const moduleFiles = fs.readdirSync(modulesPath).filter((file) => file.endsWith(".js"));
client.modules = {};
for (const file of moduleFiles) {
try {
const imported = require(`#modules/${file.slice(0, -3)}`);
const name = file.replace(".js", "");
if (typeof imported === "function" && imported.length === 0) {
imported(client);
console.log(`[MODULE] Loaded boot: ${file}`);
continue;
}
client.modules[name] = imported;
console.log(`[MODULE] Loaded utility: ${file}`);
} catch (err) {
console.error(`[MODULE] Failed to load ${file}:`, err);
}
}
async function start() {
await client.modules.db.initDb();
console.log("[DATABASE] Connected to MongoDB Atlas.");
await client.login(discordToken);
}
async function shutdown() {
server.close();
client.destroy();
await client.modules.db.closeDb();
}
process.once("SIGINT", shutdown);
process.once("SIGTERM", shutdown);
start().catch((error) => {
console.error("Failed to start:", error);
process.exitCode = 1;
void shutdown();
});