-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
160 lines (125 loc) · 5.13 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Require the necessary discord.js classes
import fs from 'fs';
import { Client, GatewayIntentBits, Partials , Collection } from 'discord.js';
import { createRequire } from "module";
const require = createRequire(import.meta.url);
const { token, prefix, self_id } = require ('./config.json');
import remindLoop from './loop/reminder.js';
import { messageLogger, slashLogger } from './utils/logger.js';
import { EmbedBuilder } from "discord.js";
// Create a new client instance
const myIntents = [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMessageReactions, GatewayIntentBits.DirectMessages, GatewayIntentBits.DirectMessageReactions];
const client = new Client({ intents: myIntents });
client.commands = new Collection();
client.messageCommands = new Collection();
//get all files in the commands folder
const messageCommandFiles = fs.readdirSync('./message_commands').filter(file => file.endsWith('.js'));
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
const eventFiles = fs.readdirSync('./events').filter(file => file.endsWith('.js'));
for (const file of messageCommandFiles) {
const command = await import(`./message_commands/${file}`);
client.messageCommands.set(command.default.name, command.default);
// check if the command has aliases and add it to the collection
if (command.default.aliases && command.default.aliases.length > 0) {
for (const alias of command.default.aliases) {
client.messageCommands.set(alias, command.default);
}
}
}
for (const file of commandFiles) {
const command = await import(`./commands/${file}`);
// 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.default.data.name, command.default);
}
for (const file of eventFiles) {
const cimport = await import(`./events/${file}`);
const command = cimport.default;
if (command.once) {
client.once(command.name, (...args) => command.execute(...args));
} else {
client.on(command.name, (...args) => command.execute(...args));
}
}
// handle normal message because Discord is slow af
client.on('messageCreate', async message =>{
if ( (!message.content.startsWith(`<@${self_id}>`) && !message.content.startsWith(prefix)) || message.author.bot) return;
let prefix_length = message.content.startsWith(prefix)? prefix.length : `<@${self_id}>`.length;
const args = message.content.slice(prefix_length).trim().split(' ');
const command = args.shift().toLocaleLowerCase(); //get the command in the first element and get rid of it from the args array
//deprecation notice
if (message.content.startsWith(prefix)) {
// send deprecation notice
const embed = new EmbedBuilder()
.setTitle("Deprecation Notice")
.setDescription(`Calling this bot by prefix \`dl!\` is deprecated, please mention the bot with your command instead\n Example: <@${self_id}> ${command}`);
message.channel.send({embeds: [embed]});
return;
}
if (!client.messageCommands.has(command)) return;
try {
await client.messageCommands.get(command).execute(message, args);
try{
messageLogger(command, message, args);
} catch (error) {
console.log(error);
}
} catch (error) {
try{
console.error(error);
message.reply('there was an error trying to execute that command!');
try{
messageLogger(command, message, args, 1);
} catch (error) {}
} catch (error) {
console.error(error);
}
}
})
// responnse to interaction
client.on('interactionCreate', async interaction => {
//ignore all interactions that is not slash command
if (!interaction.isCommand()) return;
// console.log("Command name", interaction);
const command = client.commands.get(interaction.commandName);
// return if no command found
if (!command) return;
try{
await command.execute(interaction);
slashLogger(interaction);
} catch (error) {
// keep the bot going even if there is an error
try{
console.error(error);
await interaction.reply({
content: 'There was an error trying to execute that command!',
ephemeral: true
});
slashLogger(interaction,1);
} catch (error) {
console.error(error);
}
}
});
client.on("ready", () => {
// remindLoop(client);
setInterval(() => {
try{
remindLoop(client); // try catch this as I don't trust myself
} catch (error) {
console.log(error);
}
},60000);
// setInterval(() => {
// client.channels.fetch('827819975167311892').then(channel => {
// channel.send(`a`);
// }).catch(console.error);
// }, 5000);
})
// Login to Discord with your client's token
client.login(token);
// handle uncaught exceptions
process.on('unhandledRejection', error => {
console.error('Unhandled promise rejection:', error);
});
export {client};