-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
195 lines (167 loc) · 5.91 KB
/
main.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
require("dotenv").config();
const Discord = require("discord.js");
const fs = require("fs");
const brain = require("brain.js");
const moment = require("moment-timezone");
const lowdb = require("lowdb");
const FileSync = require("lowdb/adapters/FileSync");
const {
globalPrefix,
richPresence,
token,
env,
nnConfig,
} = require("./assets/config");
const trainingData = require("./assets/training");
const client = new Discord.Client();
client.commands = new Discord.Collection();
const cooldowns = new Discord.Collection();
const net = new brain.recurrent.LSTM();
moment.locale("pt-BR");
moment.tz.setDefault("America/Maceio");
const adapter = new FileSync("assets/database.json");
const database = lowdb(adapter);
database
.defaults({
filters: [],
channels: [],
})
.write();
client.on("ready", () => {
console.log(
`Preparado com o ID: ${client.user.id}, iniciando treinamento.`
);
setInterval(() => {
let presence = Math.floor(Math.random() * richPresence.length);
client.user.setActivity(richPresence[presence], {
type: "COMPETING",
});
}, 7000);
const trainingStart = moment();
net.train(trainingData, nnConfig);
const trainingTime = moment(moment() - trainingStart).format(
"mm [minutos e] ss [segundos]"
);
console.log("O treinamento foi finalizado em:", trainingTime);
});
const commandsList = fs
.readdirSync("./commands")
.filter((file) => file.endsWith(".js"));
for (const command of commandsList) {
const cmd = require(`./commands/${command}`);
client.commands.set(cmd.name, cmd);
}
client.on("message", async (message) => {
if (
!message.content.startsWith(globalPrefix) &&
message.author.id !== client.user.id
) {
const isChannelBlocked = database
.get("channels")
.find({ guildId: message.channel.guild.id })
.value();
if (
isChannelBlocked === undefined ||
!isChannelBlocked.blocked.includes(message.channel.id)
) {
const isFilterEnabled = database
.get("filters")
.find({ guildId: message.channel.guild.id })
.value();
if (
isFilterEnabled === undefined ||
isFilterEnabled.mode === "enabled"
) {
const messageLevel = net.run(message.content);
env === "development" ? console.log(messageLevel) : null;
if (messageLevel === "1") {
const embed = new Discord.MessageEmbed()
.setColor("#fdfd96")
.setTitle("Alerta")
.addField(
"Descrição:",
`Frase com palavra(s) inadequada(s) detectada. [${message.content}]`
)
.setFooter(
`Mensagem enviada por: ${message.author.username}`,
message.author.avatarURL()
);
message.reply(embed);
}
}
}
}
if (!message.content.startsWith(globalPrefix) || message.author.bot) return;
const args = message.content.slice(globalPrefix.length).split(" ");
const commandName = args.shift().toLowerCase();
const command =
client.commands.get(commandName) ||
client.commands.find(
(cmd) => cmd.aliases && cmd.aliases.includes(commandName)
);
if (!command) return;
if (command.guildOnly === true && message.channel.type !== "text") {
const embed = new Discord.MessageEmbed()
.setColor("#ff6961")
.setTitle("Erro")
.addField(
"Descrição:",
"Este comando só pode ser executado em servidores."
);
return message.reply(embed);
}
if (command.args && !args.length) {
let description = "Este comando precisa de argumentos.";
if (command.usage) {
description += `\nO modo de usar este comando é: **${globalPrefix}${command.name} ${command.usage}**`;
}
const embed = new Discord.MessageEmbed()
.setColor("#ff6961")
.setTitle("Erro")
.addField("Descrição:", description);
return message.channel.send(embed);
}
if (!cooldowns.has(command.name)) {
cooldowns.set(command.name, new Discord.Collection());
}
const now = moment().valueOf();
const timestamps = cooldowns.get(command.name);
const cooldownAmount = 5 * 1000;
if (timestamps.has(message.author.id)) {
const expirationTime =
timestamps.get(message.author.id) + cooldownAmount;
if (now < expirationTime) {
const timeLeft = (expirationTime - now) / 1000;
const embed = new Discord.MessageEmbed()
.setColor("#ff6961")
.setTitle("Erro")
.addField(
"Descrição:",
`Você ainda precisa esperar ${timeLeft.toFixed(
1
)} segundos para usar esse comando.`
);
return message.reply(embed).then((sendedMessage) => {
setTimeout(() => {
sendedMessage.delete();
}, 3000);
});
}
}
timestamps.set(message.author.id, now);
setTimeout(() => timestamps.delete(message.author.id), cooldownAmount);
try {
command.execute(message, args, database);
} catch (error) {
console.error(error);
const embed = new Discord.MessageEmbed()
.setColor("#ff6961")
.setTitle("Erro")
.addField(
"Descrição:",
"Houve um erro ao tentar executar esse comando."
);
message.reply(embed);
}
});
client.login(token);