-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
87 lines (76 loc) · 2.24 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
const {
Client,
GatewayIntentBits,
ActivityType,
Collection,
} = require("discord.js");
const fs = require("node:fs");
const path = require("node:path");
const utils = require("./utils");
const config = require("./config.json");
const { token } = require("./token.json");
// Start a new discord client
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages],
});
// Set up bot commands
client.commands = new Collection();
const commandsPath = path.join(__dirname, "commands"); // Look in command folder
const commandFiles = fs
.readdirSync(commandsPath)
.filter((file) => file.endsWith(".js")); // find .js files in command folder
// For each .js file found...
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
// Set a /command. Each /command is the same as the file name
client.commands.set(command.data.name, command);
}
// When the server is ready
client.once("ready", () => {
console.log(`[${utils.timestamp()}] ark-bot connected!`);
// Set ark-bot discord status
client.user.setPresence({
activities: [
{
name: "with otters",
type: ActivityType.Playing,
},
],
status: "online",
});
});
// Command handling
client.on("interactionCreate", async (interaction) => {
if (!interaction.isChatInputCommand()) return;
// Try to find interaction in command list
const command = interaction.client.commands.get(interaction.commandName);
if (!command) return;
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
await interaction.reply({
content: "There was an error while executing this command!",
ephemeral: true,
});
}
});
// Attempt to reconnect if the bot ever disconnects
client.on("error", (error) => {
console.log(`[${utils.timestamp()}] (ERROR) - ${error.message}`);
console.log(`[${utils.timestamp()}] attempting to reconnect...`);
let reconnect = setInterval(() => {
if (client.status === 0) {
clearInterval(reconnect);
} else {
client.login(token).catch(() => {
console.log(
`[${utils.timestamp()}] reconnect failed. trying again...`
);
});
}
}, config.reconnect_delay);
});
// Login to discord with the token from token.json
client.login(token);