-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhistbot.js
188 lines (171 loc) · 6.42 KB
/
histbot.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
const Discord = require('discord.js');
/*const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const { SlashCommandBuilder } = require('@discordjs/builders');*/
const { Client } = require('discord.js');
const client = new Client({ intents: 38479 }); //38734 https://discord-intents-calculator.vercel.app/
const fs = require('fs');
const mysql = require('mysql2');
const config = require("./config.json");
const hidden = require("./hidden.json");
const xpmanager = require("./xpmanager");
const path = require("path");
//let commandstoregister = [];
client.commands = new Discord.Collection();
client.mysqldiscord = mysql.createConnection({
host: hidden.mysql.host,
port: hidden.mysql.port,
user: hidden.mysql.user,
database: hidden.mysql.schema,
password: hidden.mysql.password
});
client.mysqlingame = mysql.createConnection({
host: hidden.mysql.host,
port: hidden.mysql.port,
user: hidden.mysql.user,
database: 'v6',
password: hidden.mysql.password
});
client.tags = new Discord.Collection();
client.autorespond = new Discord.Collection();
client.xpapi = new xpmanager.xpmanager(client);
async function start() {
loadtags();
loadautorespond();
fs.readdir(path.resolve(__dirname, './events/'), (error, f) => {
if (error) {
return console.error(error);
}
f.forEach((f) => {
let events = require(`./events/${f}`);
let event = f.split('.')[0];
client.on(event, events.bind(null, client));
});
console.log(`${f.length} events chargés`);
});
await client.login(hidden.token).catch(error => console.log("Erreur de connexion : " + error));
await loadcommands();
refreshrank()
setInterval(refreshrank, 60 * 60 * 1000); // 1 hour
setInterval(everyday, (24 - new Date().getHours()) * 60 * 60 * 1000); // 1 day
}
function getDirectories(path) {
return fs.readdirSync(path).filter(function (file) {
return fs.statSync(path+'/'+file).isDirectory();
});
}
async function loadcommands() {
let f = fs.readdirSync(path.resolve(__dirname, './commands/'));
let commands = f.filter(f => f.split('.').pop() === 'js');
let total = 0;
commands.forEach((f) => { //useless (commandes à la racine)
let cmd = require(`./commands/${f}`);
loadcommand(cmd);
});
let directories = getDirectories(path.resolve(__dirname, './commands/'));
for (const direct of directories) {
let filedirect = fs.readdirSync(path.resolve(__dirname, './commands/' + direct));
let commandsdirect = filedirect.filter(filedirect => filedirect.split('.').pop() === 'js');
commandsdirect.forEach((f) => {
let cmd = require(`./commands/${direct}/${f}`);
loadcommand(cmd);
/*let conf = cmd.config;
if(conf.category !== "hidden"){
let slashcmd = new SlashCommandBuilder()
.setName(conf.name)
.setDescription(conf.description);
if (conf.needed_args) {
for (let i = 1; conf.needed_args >= i; i++) {
slashcmd = slashcmd.addStringOption(option => option.setName("arg_" + i).setDescription("Un argument obligatoire").setRequired(true));
}
}
commandstoregister.push(slashcmd.toJSON());
}*/
total++;
});
}
/*const rest = new REST({version: '9'}).setToken(hidden.token);
await (async () => {
try {
console.log('Started refreshing application (/) commands.');
await rest.put(
Routes.applicationGuildCommands(client.user.id, "589373751359963146"),
{body: commandstoregister},
);
console.log('Successfully reloaded application (/) commands.');
} catch (error) {
console.error(error);
}
})();*/
console.log(total+" commandes chargés");
}
function loadcommand(commande)
{
client.commands.set(commande.config.name, commande);
if (commande.config.alias) {
commande.config.alias.forEach((ali) => {
client.commands.set(ali, commande);
});
}
}
function loadtags()
{
client.mysqldiscord.query('SELECT * FROM `tags`;', function(err, results) {
if(!results) return;
results.forEach(row => {
client.tags.set(row["tag"], row["content"]);
});
});
}
function loadautorespond()
{
client.mysqldiscord.query('SELECT * FROM `autorespond`;', function(err, results) {
if(!results) return;
results.forEach(row => {
if(row["server"] === config.serverId) client.autorespond.set(row["autorespond"], row["content"]);
});
});
}
function refreshrank()
{
client.commands.get("refreshrank").update(client);
}
//This is probably not his place but this was the easiest way to do it
function everyday()
{
client.mysqlingame.query(`DELETE FROM skins WHERE hash NOT IN (SELECT DISTINCT hash FROM player_skins)`); //cleanup unused skins
}
process.stdin.resume();
client.log = async function log(message, platform = "all") {
let channelid;
switch (platform) {
case "gen": case "general": channelid = config.logs; break;
case "staff": channelid = config.logsStaff; break;
default: case "all": channelid = config.logsStaff;break;
}
let channel = client.channels.cache.get(channelid);
if (!channel) {
channel = await client.channels.resolve(channelid);
if (!channel) return console.log("Pas de salon de log trouvé pour " + platform + " avec pour ID " + channelid);
}
let d = new Date();
channel.send(`**${d.getHours()}:${d.getMinutes()}:${d.getSeconds()}** ${message}`);
}
async function exitHandler(options, exitCode) {
console.log(`Bot down pour ${exitCode}`)
await client.log(`Bot down pour ${exitCode}`)
if (options.cleanup) {
await client.xpapi.save();
return;
}
if (options.exit) process.exit();
}
process.on('exit', exitHandler.bind(null,{cleanup:true}));
process.on('SIGINT', exitHandler.bind(null, {exit:true}));//Ctrl+C
process.on('SIGUSR1', exitHandler.bind(null, {exit:true})); //KILL PID
process.on('SIGUSR2', exitHandler.bind(null, {exit:true}));
process.on('uncaughtException', function (exception) {
console.log(exception)
exitHandler({exit: true}, exception.message+"\n"+exception.stack);
}); //ERreur non log
start();