-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
384 lines (357 loc) Β· 12.4 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// Express
const express = require("express");
const app = express();
app.listen(process.env.PORT);
app.get("/", (request, response) => {
//client.login(process.env.TOKEN)
return response.send("<h1>HELLO</h1>");
});
// MySQL
const mysql = require("mysql2")
const connection = mysql.createPool({
user: "GM5DQI3hct",
database: "GM5DQI3hct",
password: process.env.DBPASS,
host: "remotemysql.com"
})
module.exports.connection = connection
// Package setup
const fetch = require("node-fetch");
const u = require("./utils.js");
const fs = require("fs");
const Discord = require("discord.js");
const client = new Discord.Client({ disableEveryone: true });
let webhook;
// Command handler
client.commands = new Discord.Collection();
client.aliases = new Discord.Collection();
fs.readdir("./commands", (err, files) => {
if (!files) return console.log("There are no files to load...");
const jsfiles = files.filter(f => f.split(".").pop() === "js");
jsfiles.forEach((file, index) => {
const props = require(`./commands/${file}`);
console.log(`${file} loaded!`);
client.commands.set(props.help.name, props);
if (props.help.aliases)
props.help.aliases.forEach(alias => {
client.aliases.set(alias, props.help.name);
});
});
});
// Defining the prefix
client.prefix = "!";
// Message event
client.on("message", async msg => {
if (msg.type === "PINS_ADD" && msg.channel.name === "suggestions")
return msg.delete(500);
// MC Notification
if (
msg.author.id === client.user.id &&
msg.channel.name === "discord-and-minecraft" &&
msg.content.split(" ")[1] === "**Server" &&
msg.content.split(" ")[3] === "started**"
)
msg.channel.send(`${msg.guild.roles.cache.find(r => r.name === "Server")}`);
if (msg.author.bot) return;
if (msg.channel.type === "dm") return;
// Bot Chat
if (msg.channel.name === "bot-chat") {
if (!msg.content.startsWith(client.prefix)) {
const message = msg.content;
/*if (!message.match(/\w+/gi)) {
msg.channel.send("Unknown caracters found in message!").then(mesg => {
mesg.delete(10000);
msg.delete(10000);
});
return;
}*/
if (message.startsWith("-")) return;
msg.channel.startTyping();
const r = await fetch(
encodeURI(`https://acobot-brainshop-ai-v1.p.rapidapi.com/get?bid=178&key=sX5A2PcYZbsN5EY6&uid=mashape&msg=${message}`),
{
headers: {
"Content-Type": "application/json",
"x-rapidapi-host": "acobot-brainshop-ai-v1.p.rapidapi.com",
"x-rapidapi-key": "2e55a127a2msh9292691d1821a35p19c403jsnf6732d176f6b"
}
}
);
const re = await r.json().catch(e => {
msg.channel.stopTyping()
msg.channel.send("There was an error while fetching the response from the message: " + message)
});
msg.channel.stopTyping();
msg.channel.send(re.cnt, {tts: true});
}
}
// Suggestion function
if (msg.channel.name === "suggestions") {
let wantsToSuggest = true;
if (msg.member.hasPermission("MANAGE_CHANNELS")) {
if (!msg.content.startsWith("!suggest ")) {
wantsToSuggest = false;
} else {
msg.content = msg.content.substring(9, msg.content.length);
if (msg.content.length === 0) wantsToSuggest = false;
}
}
if (wantsToSuggest) {
msg.delete({timeout: 500});
const embed = new Discord.MessageEmbed()
.setAuthor(msg.guild.name, msg.guild.iconURL({format: "png"}))
.setTitle("**Suggestion**")
.setColor("GREEN")
.setDescription(msg.content)
.setFooter(
`Suggested By ${msg.author.tag}`,
msg.author.displayAvatarURL({format: "png"})
);
await msg.channel.send(embed).then(async message => {
await message.react("613728573492166666");
await message.react("613729322108452868");
});
}
}
if (!msg.content.startsWith(client.prefix)) return;
// Command Runner
const args = msg.content
.slice(client.prefix.length)
.trim()
.split(" ");
const cmd = args.shift().toLowerCase();
let command;
if (client.commands.has(cmd)) {
command = client.commands.get(cmd);
command.run(client, msg, args).catch(e => {
console.log(e)
msg.channel.send("There was an error, please contact the bot creator if the issue persists")
msg.delete({timeout: 500})
});
} else if (client.aliases.has(cmd)) {
command = client.commands.get(client.aliases.get(cmd));
command.run(client, msg, args).catch(e => {
console.log(e)
msg.channel.send("There was an error, please contact the bot creator if the issue persists")
msg.delete({timeout: 500})
});
}
});
// Guild Member Update event
client.on("guildMemberUpdate", (oldM, newM) => {
if (
!(newM.nickname
? newM.nickname.match(/[a-zA-Z0-9`~!@#$%^&*()_+{}|":?><,./;'\[\]=-\\'"]/)
: newM.user.username.match(
/[a-zA-Z0-9`~!@#$%^&*()_+{}|":?><,./;'\[\]=-\\'"]/
))
)
newM.setNickname("WRITABLE NAME PLS");
});
// Guild Member Add event
client.on("guildMemberAdd", async member => {
if (
!member.user.username.match(
/[a-zA-Z1-9`~!@#$%^&*()_+{}|":?><,./;'\[\]=-\\'"]/
)
)
member.setNickname("WRITABLE NAME PLS");
const embed = new Discord.MessageEmbed()
.setTitle("**Member Joined**")
.setDescription(`${member.user.tag} has joined the server!`)
.setColor("GREEN")
.setImage(member.user.displayAvatarURL({format: "png"}))
.setFooter(
`There are now ${member.guild.members.cache.size} members on the server.`,
member.guild.iconURL({format: "png"})
);
member.guild.channels.cache.find(c => c.name === "join-leave").send(embed);
member.roles.add(member.guild.roles.cache.find(r => r.name === "Player"));
member.guild.channels.cache
.find(c => c.name === "welcome")
.send(
`Welcome ${member} to the server! Please read the message sent above.`
)
.then(msg => msg.delete({timeout: 30000}));
});
// Guild Member Remove event
client.on("guildMemberRemove", async member => {
const embed = new Discord.MessageEmbed()
.setTitle("**Member Left**")
.setDescription(`${member.user.tag} has left the server!`)
.setColor("RED")
.setImage(member.user.displayAvatarURL({format: "png"}))
.setFooter(
`There are now ${member.guild.members.cache.size} members on the server.`,
member.guild.iconURL({format: "png"})
);
member.guild.channels.cache.find(c => c.name === "join-leave").send(embed);
});
// Ready Event
client.on("ready", async () => {
member()
setInterval(member, 1800000)
client.channels.cache.get("685797705746087997").send("restart")
console.log(" ")
console.log("" + client.user.username + " is ready!");
console.log(" ")
client.user.setActivity("the Discord Server", { type: "WATCHING" });
});
client.on("raw", packet => {
if (!["MESSAGE_REACTION_ADD", "MESSAGE_REACTION_REMOVE"].includes(packet.t))
return;
const channel = client.channels.cache.get(packet.d.channel_id);
if (channel.messages.cache.has(packet.d.message_id)) return;
channel.messages.fetch(packet.d.message_id).then(message => {
const emoji = packet.d.emoji.id
? `${packet.d.emoji.name}:${packet.d.emoji.id}`
: packet.d.emoji.name;
const reaction = message.reactions.cache.find(r => r.emoji.id === packet.d.emoji.id);
if (reaction) {
reaction.users = new Discord.ReactionUserManager(client, null, reaction)
reaction.users.cache = new Discord.Collection()
reaction.users.cache.set(packet.d.user_id, client.users.cache.get(packet.d.user_id));
}
if (packet.t === "MESSAGE_REACTION_ADD") {
client.emit(
"messageReactionAdd",
reaction,
client.users.cache.get(packet.d.user_id)
);
}
if (packet.t === "MESSAGE_REACTION_REMOVE") {
client.emit(
"messageReactionRemove",
reaction,
client.users.cache.get(packet.d.user_id)
);
}
});
});
// Suggestions
client.on("messageReactionAdd", async (reaction, user) => {
if (reaction.message.channel.name !== "suggestions") return;
if (user.bot) return;
reaction.message.embeds.forEach(embed => {
if (embed.footer.text.split(" By ")[1] === user.tag) reaction.users.remove(user);
});
reaction.users.cache = await reaction.users.fetch();
reaction.message.reactions.cache.find(
r => r.emoji.id !== reaction.emoji.id
).users.cache = await reaction.message.reactions.cache
.find(r => r.emoji.id !== reaction.emoji.id)
.users.fetch();
if (reaction.users.cache.has(user.id)) {
if (
reaction.message.reactions.cache
.find(r => r.emoji.id !== reaction.emoji.id)
.users.cache.has(user.id)
)
reaction.users.remove(user);
}
reaction.message.reactions.cache.forEach(r => {
if (!r.users.cache.has(client.user.id)) reaction.message.react(r.emoji.id);
});
if (
reaction.message.reactions.cache.find(r => r.emoji.id === "613728573492166666")
.count >= 10 &&
reaction.message.reactions.cache.find(r => r.emoji.name === "no").count <=
reaction.message.reactions.cache.find(r => r.emoji.name === "yes").count / 2
)
reaction.message.pin();
else reaction.message.unpin();
});
client.on("messageReactionRemove", async (reaction, user) => {
if (reaction.message.channel.name !== "suggestions") return;
if (
reaction.message.reactions.cache.find(r => r.emoji.id === "613728573492166666")
.count >= 10 &&
reaction.message.reactions.cache.find(r => r.emoji.name === "no").count <=
reaction.message.reactions.cache.find(r => r.emoji.name === "yes").count / 2
)
reaction.message.pin();
else reaction.message.unpin();
});
// Roles
client.on("messageReactionAdd", async (reaction, user) => {
if (reaction.message.channel.name === "roles") {
//console.log(reaction)
if (reaction.emoji.name === "π")
reaction.message.guild.members.cache
.get(user.id)
.roles.add(
reaction.message.guild.roles.cache.find(r => r.name === "Changelog")
);
if (reaction.emoji.name === "π’")
reaction.message.guild.members.cache
.get(user.id)
.roles.add(
reaction.message.guild.roles.cache.find(r => r.name === "Announcements")
);
if (reaction.emoji.name === "yes")
reaction.message.guild.members.cache
.get(user.id)
.roles.add(reaction.message.guild.roles.cache.find(r => r.name === "Server"));
if (reaction.emoji.name === "β")
reaction.message.guild.members.cache
.get(user.id)
.roles.add(reaction.message.guild.roles.cache.find(r => r.name === "Polls"));
}
});
client.on("messageReactionRemove", async (reaction, user) => {
if (reaction.message.channel.name === "roles") {
if (reaction.emoji.name === "π")
reaction.message.guild.members.cache
.get(user.id)
.roles.remove(
reaction.message.guild.roles.cache.find(r => r.name === "Changelog")
);
if (reaction.emoji.name === "π’")
reaction.message.guild.members.cache
.get(user.id)
.roles.remove(
reaction.message.guild.roles.cache.find(r => r.name === "Announcements")
);
if (reaction.emoji.name === "yes")
reaction.message.guild.members.cache
.get(user.id)
.roles.remove(
reaction.message.guild.roles.cache.find(r => r.name === "Server")
);
if (reaction.emoji.name === "β")
reaction.message.guild.members.cache
.get(user.id)
.roles.remove(
reaction.message.guild.roles.cache.find(r => r.name === "Polls")
);
}
});
// Login
client.login(process.env.TOKEN).catch(console.error)
function member() {
let date = new Date()
date = new Date(date.getTime() - 86400000)
date.setMinutes(0)
date.setSeconds(0)
date.setMilliseconds(0)
date.setHours(0)
connection.query("SELECT * FROM `Member Count`", (error, results, fields) => {
if (error) return console.log(error)
if (results.filter(r => r.Date === date.getTime()).length === 0) {
return connection.query(`INSERT INTO \`Member Count\` (Date, Count) VALUES (${date.getTime()}, ${client.guilds.cache.get("570888169575153694").members.cache.size})`, () => {
const chart = require("./chart.js")
chart()
.then(() => {
console.log("Chart Updated")
})
.catch(console.log)
})
}
const chart = require("./chart.js")
chart()
.then(() => {
console.log("Chart Loaded")
})
.catch(console.log)
})
}