-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
169 lines (160 loc) · 5.05 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
const { Client, Collection } = require("discord.js");
const Discord = require("discord.js");
const db = require("quick.db");
require("dotenv").config();
const { default_prefix } = require("./config.json");
const client = new Client({
disableEveryone: true,
partials: ["MESSAGE", "REACTION"]
});
client.commands = new Collection();
client.aliases = new Collection();
client.queue = new Map();
["commands"].forEach(handler => {
require(`./handlers/${handler}`)(client);
});
/*******/ /*******/ /*******/ /*******/
/*******/ /*Message Event*//*******/
/*******/ /*******/ /*******/ /*******/
client.on("message", async message => {
if (message.author.bot) return;
if (!message.guild) return;
let prefix = db.get(`prefix_${message.guild.id}`);
if (prefix === null) prefix = default_prefix;
if (!message.content.startsWith(prefix)) return;
if (!message.member)
message.member = await message.guild.fetchMember(message);
const args = message.content
.slice(prefix.length)
.trim()
.split(/ +/g);
const cmd = args.shift().toLowerCase();
if (cmd.length === 0) return;
let command = client.commands.get(cmd);
if (!command) command = client.commands.get(client.aliases.get(cmd));
if (command) command.run(client, message, args);
});
/*******/ /*******/ /*******/ /*******/
/*******/ /*Reaction Add*//*******/
/*******/ /*******/ /*******/ /*******/
client.on("messageReactionAdd", async (reaction, user) => {
if (user.bot) return
let message = await reaction.message;
if (
message.reactions.cache.forEach(reaction =>
reaction.users.cache.has(user.id)
)
) {
console.log("yes");
}
let menus = db.get(`rr_${message.guild.id}`);
let menu;
let menuIndex;
if (!menus) return;
for (let i = 0; i < menus.length; i++) {
if (menus[i].ChannelID == message.channel.id && menus[i].ID == message.id) {
menuIndex = i;
menu = menus[i];
}
}
if (!menu) return;
let role;
for (let i = 0; i < menu.roles.length; i++) {
let type = reaction.emoji.id ? reaction.emoji.id : reaction.emoji.name;
if (type == menu.roles[i].reaction) {
role = menu.roles[i].role;
}
}
if (menu.type != "single") {
role = message.guild.roles.cache.find(x => x.id == role);
if (!role) return;
let member = message.guild.members.cache.find(x => x.id == user.id);
if (!member) return;
if (member.roles.cache.has(role.id)) return;
member.roles.add(role.id);
return;
} else {
console.log(menu.usersReacted)
if (menu.usersReacted.includes(user.id)){
await reaction.users.remove(user.id);
return user
.send(
"You have already took role from this reaction set and you cannot take another!"
)
.catch(e => {
return;
});
}
role = message.guild.roles.cache.find(x => x.id == role);
if (!role) return;
let member = message.guild.members.cache.find(x => x.id == user.id);
if (!member) return;
if (member.roles.cache.has(role.id)) return;
member.roles.add(role.id);
menu.usersReacted.push(user.id);
menus[menuIndex] = menu;
db.set(`rr_${message.guild.id}`, menus);
return;
}
});
/*******/ /*******/ /*******/ /*******/
/*******/ /*Reaction Remove*//*******/
/*******/ /*******/ /*******/ /*******/
client.on("messageReactionRemove", async (reaction, user) => {
if (user.bot) return
let message = await reaction.message;
let menus = db.get(`rr_${message.guild.id}`);
let menu;
let menuIndex;
if (!menus) return;
for (let i = 0; i < menus.length; i++) {
if (menus[i].ChannelID == message.channel.id && menus[i].ID == message.id) {
menu = menus[i];
menuIndex = i;
}
}
if (!menu) return;
let role;
for (let i = 0; i < menu.roles.length; i++) {
let type = reaction.emoji.id ? reaction.emoji.id : reaction.emoji.name;
if (type == menu.roles[i].reaction) {
role = menu.roles[i].role;
}
}
if (menu.type != "single") {
role = message.guild.roles.cache.find(x => x.id == role);
if (!role) return;
let member = message.guild.members.cache.find(x => x.id == user.id);
if (!member) return;
if (!member.roles.cache.has(role.id)) return;
member.roles.remove(role.id);
return;
} else {
role = message.guild.roles.cache.find(x => x.id == role);
if (!role) return;
let member = message.guild.members.cache.find(x => x.id == user.id);
if (!member) return;
if (!member.roles.cache.has(role.id)) return;
console.log(menu.usersReacted)
if (menu.usersReacted.includes(user.id)) {
let index;
for (let i = 0; i < menu.usersReacted.length; i++) {
if (menu.usersReacted[i] == user.id) {
index = i;
}
}
if (index != -1) {
menu.usersReacted.splice(index, 1);
console.log(menu.usersReacted)
menus[menuIndex] = menu;
db.set(`rr_${message.guild.id}`, menus);
}
}
member.roles.remove(role.id);
return;
}
});
/*******/ /*******/ /*******/ /*******/
/*******/ /*Login to API*//*******/
/*******/ /*******/ /*******/ /*******/
client.login(process.env.token);