-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
185 lines (162 loc) · 6.62 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
const { Client, GatewayIntentBits, Events, REST, Routes } = require('discord.js');
const { log } = require('nyx-logger');
const mysql = require('mysql2');
const fs = require('fs');
const path = require('path');
const axios = require('axios');
const { version: currentVersion } = require('./package.json');
class DiscordEasy {
constructor(token, clientId, guildId, prefix = '!', useSlashCommands = true, useMessageCommands = true) {
this.token = token;
this.clientId = clientId;
this.guildId = guildId;
this.prefix = prefix;
this.useSlashCommands = useSlashCommands;
this.useMessageCommands = useMessageCommands;
this.slashCommands = [];
this.messageCommands = [];
this.database = null;
this.defaultIntents = [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
];
this.intents = [...this.defaultIntents];
this.client = new Client({
intents: this.intents,
});
this.client.on(Events.InteractionCreate, async (interaction) => {
if (!interaction.isCommand() || !this.useSlashCommands) return;
const command = this.slashCommands.find(cmd => cmd.name === interaction.commandName);
if (command) {
await command.execute(interaction, this.database);
}
});
this.client.on(Events.MessageCreate, async (message) => {
if (useMessageCommands) {
if (!message.author.bot && message.content.startsWith(this.prefix)) {
const args = message.content.slice(this.prefix.length).trim().split(/ +/);
const commandName = args.shift().toLowerCase();
const command = this.messageCommands.find(cmd => cmd.name === commandName);
if (command) {
try {
await command.execute(message);
} catch (error) {
log.print("err", `Error executing command ${commandName}:`, error);
}
}
}
}
});
}
async addDatabase(host, user, password, database) {
this.database = await mysql.createConnection({
host: host,
user: user,
password: password,
database: database
});
log.print("info", 'Connected to database');
}
addIntents(...newIntents) {
this.intents.push(...newIntents);
this.client = new Client({ intents: this.intents });
log.print("info", `Added intents: ${newIntents.join(', ')}`);
}
setPath(type, dirPath) {
const absolutePath = path.resolve(process.cwd(), dirPath);
if (!fs.existsSync(absolutePath)) {
log.print("err", `Directory not found: ${absolutePath}`);
return;
}
const files = fs.readdirSync(absolutePath);
for (const file of files) {
const filePath = path.join(absolutePath, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
this.setPath(type, path.join(dirPath, file));
} else if (file.endsWith('.js')) {
try {
const instance = require(filePath);
this.set(type, instance);
} catch (error) {
}
}
}
}
set(type, instance) {
if (type === 'command') {
if (instance.name && typeof instance.execute === 'function') {
if (instance.description) {
this.slashCommands.push(instance);
log.print("info", `Added slash command: ${instance.name}`);
} else {
this.messageCommands.push(instance);
log.print("info", `Added message command: ${instance.name}`);
}
} else {
log.print("err", 'Invalid command format:', instance);
}
} else if (type === 'event') {
if (typeof instance.register === 'function') {
instance.register(this);
log.print("info", `Added event: ${instance.name}`);
} else {
log.print("err", 'Invalid event format:', instance);
}
} else {
if (this.useMessageCommands) {
log.print("err", `Unknown type: ${type}`);
}
}
}
add(commandOrEvent) {
if (commandOrEvent.name) {
if (commandOrEvent.execute) {
if (commandOrEvent.description) {
this.slashCommands.push(commandOrEvent);
log.print("info", `Added slash command: ${commandOrEvent.name}`);
} else {
this.messageCommands.push(commandOrEvent);
log.print("info", `Added message command: ${commandOrEvent.name}`);
}
} else if (typeof commandOrEvent.register === 'function') {
commandOrEvent.register(this);
log.print("info", `Added event: ${commandOrEvent.name}`);
} else {
log.print("err", 'Invalid command or event format:', commandOrEvent);
}
} else {
log.print("err", 'Invalid command or event format:', commandOrEvent);
}
}
async registerSlashCommands() {
if (!this.useSlashCommands) return;
const rest = new REST({ version: '9' }).setToken(this.token);
try {
log.print("info", 'Waiting for slash commands registry...');
const commandsToRegister = this.slashCommands.filter(cmd => cmd.description);
await rest.put(Routes.applicationGuildCommands(this.clientId, this.guildId), {
body: commandsToRegister,
});
log.print("info", 'Slash commands registered');
} catch (error) {
log.print("err", 'Error registering commands:', error);
}
}
async checkForUpdates() {
const response = await axios.get(`https://registry.npmjs.org/discord-easy`);
const latestVersion = response.data['dist-tags'].latest;
if (latestVersion !== currentVersion) {
log.print(`[discord-easy] Update available ${currentVersion} → ${latestVersion} : npm i discord-easy@latest`);
}
}
async run() {
if (this.useSlashCommands) {
await this.registerSlashCommands();
}
this.checkForUpdates();
this.client.login(this.token);
}
}
module.exports = DiscordEasy;