-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
154 lines (141 loc) · 4.59 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
// Require the necessary discord.js classes
const fs = require('node:fs');
const path = require('node:path');
const { Client, GatewayIntentBits, Partials } = require('discord.js');
require('dotenv').config();
const { Collection, ActivityType } = require('discord.js')
const selfroles = require('./modals/selfroles-schema.js');
const chalk = require("chalk");
const mongoose = require('mongoose');
const token = process.env.TOKEN;
// Create a new client instance
const client = new Client({ intents: [GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.DirectMessages
], partials: [Partials.Message, Partials.Channel, Partials.Reaction] });
client.commands = new Collection();
client.subCommands = new Collection();
client.on('ready', async () => {
mongoose.connect(process.env.MONGO_URI, {
keepAlive: true,
});
console.log(`${chalk.green("Success! ✔")} We are connected to the ${chalk.yellow("database")}!`);
client.user.setActivity('your commands', { type: ActivityType.Listening });
});
client.on('messageReactionAdd', async (reaction, user) => {
if (reaction.partial) {
// If the message this reaction belongs to was removed, the fetching might result in an API error which should be handled
try {
await reaction.fetch();
} catch (error) {
console.error('Something went wrong when fetching the message:', error);
// Return as `reaction.message.author` may be undefined/null
return;
}
}
if (user.bot) return;
const guild = reaction.message.guild;
// check if the message is a panel
if (!reaction.message.embeds[0]) {
return;
}
if (!reaction.message.embeds[0].title) {
return;
}
let panelName = reaction.message.embeds[0].title || "";
const panel = await selfroles.findOne({ guild });
if (!panel) {
return;
}
let targetPanel;
for(let i = 0; i< panel.panels.length; i++) {
if(panel.panels[i].panelName === panelName) {
targetPanel = panel.panels[i];
break;
}
}
if(!targetPanel) {
return;
}
for (const role of targetPanel.roles) {
if (role.emoji === reaction.emoji.name) {
const guildMember = await guild.members.fetch(user.id);
await guildMember.roles.add(`${role.roleID}`);
}
}
});
client.on('messageReactionRemove', async (reaction, user) => {
if (reaction.partial) {
// If the message this reaction belongs to was removed, the fetching might result in an API error which should be handled
try {
await reaction.fetch();
} catch (error) {
console.error('Something went wrong when fetching the message:', error);
// Return as `reaction.message.author` may be undefined/null
return;
}
}
const guild = reaction.message.guild;
if (user.bot) return;
// find panel
let panelName = reaction.message.embeds[0].title || "";
const panel = await selfroles.findOne({
guild,
});
if (!panel) {
return;
}
let targetPanel;
for(let i = 0; i< panel.panels.length; i++) {
if(panel.panels[i].panelName === panelName) {
targetPanel = panel.panels[i];
break;
}
}
if(!targetPanel) {
return;
}
for (const role of targetPanel.roles) {
if (role.emoji === reaction.emoji.name) {
const guildMember = await guild.members.fetch(user.id);
await guildMember.roles.remove(`${role.roleID}`);
}
}
})
async function loadCommandsFromFolder(folderName) {
await client.commands.clear();
await client.subCommands.clear();
const commandFiles = fs.readdirSync(path.join(__dirname, 'commands', folderName))
.filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join(__dirname, 'commands', folderName, file);
const command = require(filePath);
if (command.subCommand) {
client.subCommands.set(command.subCommand, command)
} else {
if ('data' in command && !command.subCommand || 'execute' in command && !command.subCommand) {
client.commands.set(command.data.name, command);
} else if (!command.subCommand) {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
}
}
loadCommandsFromFolder('economy');
loadCommandsFromFolder('moderation');
const eventsPath = path.join(__dirname, 'events');
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'));
for (const file of eventFiles) {
const filePath = path.join(eventsPath, file);
const event = require(filePath);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args));
}
}
// Log in to Discord with the client's token
client.login(token);