-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
125 lines (107 loc) · 3.23 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
const { Client, Intents, Collection } = require("discord.js");
const fs = require("fs");
const { token, heartbeatlink, activity } = require("./config.json");
const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_MEMBERS,
Intents.FLAGS.GUILD_PRESENCES,
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
],
partials: ["MESSAGE", "CHANNEL", "REACTION"],
});
client.commands = new Collection();
const commandFolders = fs.readdirSync("./commands");
const handlerFiles = fs
.readdirSync("./handlers")
.filter((file) => file.endsWith(".js"));
for (const folder of commandFolders) {
const commandFiles = fs
.readdirSync(`./commands/${folder}`)
.filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
const command = require(`./commands/${folder}/${file}`);
const commandName = command.help.name.toLowerCase();
console.log(`-----------------------------`);
console.log(`| Loaded command: ${commandName} |`);
console.log(`| Command category: ${command.help.cat} |`);
client.commands.set(commandName, command);
}
}
console.log(`----------------------------`);
console.log("Successfully loaded commands to memory");
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}!`);
if (heartbeatlink === "") {
console.log("No heartbeat link provided, bot will not send heartbeats");
}
client.user.setActivity(
`${
activity[Math.round(Math.random() * (activity.length - 1))]
} | m!help`
);
setInterval(function () {
client.user.setActivity(
`${
activity[
Math.floor(Math.random() * (activity.length - 1))
]
} | m!help`
);
}, 5000);
});
// When slash commands are ran
client.on("interactionCreate", async (interaction) => {
if (!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;
try {
await command.interaction(interaction, client);
console.log(
`Command ${interaction.commandName} was ran by ${interaction.user.tag} (${interaction.user.id})`
); // Logs the command
} catch (error) {
console.error(error);
if (interaction.deferred) {
interaction.editReply({
content: "There was an error while executing this command!",
ephemeral: true,
});
return;
}
await interaction.reply({
content: "There was an error while executing this command!",
ephemeral: true,
});
}
});
// Error handlers to prevent randomly closing
client.on("error", (error) => {
console.log(`[ERROR] ` + error.stack);
});
client.on("warn", (info) => {
console.log(`[INFO] ` + info);
});
// A task that runs every 5 minutes to let better uptime know bot is online via heartbeat
setInterval(async function () {
if (heartbeatlink === "none") {
// This pass shit for javascript makes me wanna kms
}
try {
await fetch(heartbeatlink, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
bot: client.user.id,
status: "online",
}),
});
console.log("Sent heartbeat to better uptime")
} catch {
console.log('Heartbeat failed.');
}
}, 300000);
client.login(token);