-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
executable file
·196 lines (153 loc) · 6.33 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
const Discord = require("discord.js");
const fs = require("fs");
const mongoose = require("mongoose");
const dayjs = require("dayjs");
require('dotenv').config();
const onmsg = require("./utils/onmsg");
const color = require("./utils/color.json");
const config = require("./utils/config.json");
const Snipe = require("./models/snipe.js");
const replies = require("./utils/replies.json");
const prefix = config.prefix;
const client = new Discord.Client();
const allowed_channels = [
"678301439835111455", // bots
"819146399764840448" // bots-2
];
dayjs.extend(require('dayjs/plugin/utc'));
dayjs.extend(require('dayjs/plugin/timezone'));
dayjs.extend(require('dayjs/plugin/calendar'));
client.login(process.env.token);
mongoose.connect(process.env.mongoURL, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log("Succesfuly connected to MongoDB"))
.catch(err => console.log(`MongoDB connection error: ${err.message}`));
const guildInvites = new Map();
client.on("ready", () => {
console.log("Connected");
client.user.setAvatar('./duino.png');
client.guilds.cache.forEach(async (g) => {
g.fetchInvites().then(invites => {
guildInvites.set(g.id, invites);
}).catch(err => console.log(err));
await g.members.fetch();
})
console.log(`Successfully fetched members`);
console.log(`Successfully fetched invites`);
if (process.env.NODE_ENV !== "dev") {
client.commands.get("start").run(client);
console.log("Started the statistics");
}
client.user.setActivity("with your duinos - !help", { type: "PLAYING" });
})
client.commands = new Discord.Collection();
client.aliases = new Discord.Collection();
fs.readdir("./commands/", (err, files) => {
if (err) console.log(err);
const jsfiles = files.filter(f => f.split(".").pop() === "js");
if (jsfiles.length < 0) {
return console.log("Can't find commands folder or there aren't any commands!");
}
jsfiles.forEach((f, i) => {
let pull = require(`./commands/${f}`);
client.commands.set(pull.config.name, pull);
pull.config.aliases.forEach(alias => {
client.aliases.set(alias, pull.config.name)
})
console.log(`#${i +1}: ${f} loaded!`);
})
})
client.on("message", async (message) => {
if (!message.guild) return;
if (message.author.bot) return;
if (message.mentions.has(client.user)) {
if (message.mentions.everyone) return;
const reply = replies[Math.floor(Math.random() * replies.length)];
message.reply(reply);
}
const args = message.content.slice(prefix.length).split(" ");
const cmd = args[0];
if (message.content.startsWith(prefix)) {
if (!allowed_channels.includes(message.channel.id) && !message.member.hasPermission("MANAGE_MESSAGES")) {
message.channel.send(message.author.username
+ ", use the <#678301439835111455> channel for bot commands!")
.then(msg => {
msg.delete({
timeout: 5000
});
});
setTimeout(() => {
message.delete().catch();
}, 5000);
} else {
const commandfile = client.commands.get(cmd) || client.commands.get(client.aliases.get(cmd));
if (commandfile) {
message.channel.startTyping();
commandfile.run(client,message,args,color);
message.channel.stopTyping();
}
}
} else {
onmsg.run(client, message, args, color);
}
})
client.on("messageDelete", async (message) => {
if (message.author.bot) return;
const query = await Snipe.findOne({});
if (!query) {
const newSnipe = new Snipe({
date: dayjs(message.createdTimestamp).tz("Europe/Paris").calendar(),
content: message.content,
authorAvatar: message.author.avatarURL(),
authorUsername: message.author.username,
channel: message.channel.name,
isImage: false
})
newSnipe.save().catch(err => message.channel.send(err));
} else {
query.date = dayjs(message.createdTimestamp).tz("Europe/Paris").calendar();
query.authorAvatar = message.author.avatarURL();
query.authorUsername = message.author.username;
query.channel = message.channel.name;
const attachment = message.attachments.first();
if (attachment) {
query.isImage = true;
query.attachment = attachment.proxyURL;
query.content = attachment.name;
} else {
query.isImage = false;
query.content = message.content;
}
query.save().catch(err => message.channel.send(err));
}
})
let lastJoined = [];
client.on("guildMemberAdd", async (member) => {
if (member.guild.id != "677615191793467402") return;
if (config.antiRaid) {
if (lastJoined.length >= 5) {
console.log(lastJoined[0] - Date.now())
/*if (lastJoined[0] - Date.now() < 60000) {
member.send("You've been kicked from Duino-Coin Discord because it's currently under attack! \
If you're a not a bot you can try to join again in 1 minute https://discord.gg/k48Ht5y");
member.kick();
client.channels.cache.get(config.logChannelID).send(`Raid detected! <@${member.id}> has been kicked`);
}*/
lastJoined.shift();
}
lastJoined.push(Date.now());
}
const channel = member.guild.channels.cache.find(c => c.id === "677617050503479325");
const role = member.guild.roles.cache.find(r => r.name === "Member");
if (!role)
return;
member.roles.add(role);
try {
const cachedInvites = guildInvites.get(member.guild.id);
const newInvites = await member.guild.fetchInvites();
guildInvites.set(member.guild.id, newInvites);
const usedInvite = newInvites.find(invite => cachedInvites.get(invite.code).uses < invite.uses)
channel.send(`<:duco_logo:832307063395975218> Welcome on the **official Duino-Coin Discord**, **<@${member.id}>**!\nInvited by **${usedInvite.inviter.tag}**`);
} catch {
channel.send(`<:duco_logo:832307063395975218> Welcome on the **official Duino-Coin Discord**, **<@${member.id}>**!\nBut I couldn't figure out the inviter ¯\\_(ツ)_/¯`);
}
})