-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtenanmain.js
136 lines (107 loc) · 4.67 KB
/
tenanmain.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
////// @Tenandrobilgi
// Import Modules
const Discord = require('discord.js');
const config = require('./config.json')
const mysqlConfig = require('./mysqlconfig.json')
// Initialize the Discord Client (With Intents and Partials)
global.client = new Discord.Client({
intents: [Discord.GatewayIntentBits.Guilds, Discord.GatewayIntentBits.GuildMessages, Discord.GatewayIntentBits.MessageContent, Discord.GatewayIntentBits.DirectMessages, Discord.GatewayIntentBits.GuildMembers, Discord.GatewayIntentBits.GuildBans],
partials: [Discord.Partials.Channel, Discord.Partials.Message, Discord.Partials.Reaction, Discord.Partials.GuildMember, Discord.Partials.User, Discord.Partials.ThreadMember]
});
// Mysql Database configuration and handling
const mysql = require('mysql2')
const fs = require('fs');
const database_config = {
host: mysqlConfig.HOST,
user: mysqlConfig.USER,
password: mysqlConfig.PASSWORD,
database: mysqlConfig.DATABASE,
port: mysqlConfig.PORT
};
global.database;
async function handleDisconnect() {
return new Promise(function (resolve, reject) {
database = mysql.createConnection(database_config);
// Recreate the connection, since the old one cannot be reused.
// The server is either down or restarting.
database.connect(function (err) {
if (err) {
console.log('Error when connecting to database:', err);
setTimeout(handleDisconnect, 2000)
// We introduce a delay before attempting to reconnect, to avoid a hot loop, and to allow our node script to process asynchronous requests in the meantime.
}
});
// If you're also serving http, display a 503 error.
database.on('error', function (err) {
//console.log('db error', err);
// Connection to the MySQL server is usually lost due to either server restart, or a connnection idle timeout (the wait_timeout server variable configures this)
if (err.code === 'PROTOCOL_CONNECTION_LOST') {
handleDisconnect().catch(err => {
console.log(err)
});
} else if (err.fatal) {
// The connection encountered a fatal error and shut down.
handleDisconnect().catch(err => {
console.log(err)
});
} else {
reject(err);
}
});
resolve()
})
}
handleDisconnect().catch(err => {
console.error("Could not connect to mySQL databse: ", err)
client.destroy();
process.exit();
});
// Initialize the commands
client.commands = new Discord.Collection();
const FunDir = fs.readdirSync('./Bot/Commands/Fun').filter(file => file.endsWith('.js'));
const ModDir = fs.readdirSync('./Bot/Commands/Moderation').filter(file => file.endsWith('.js'));
const NSFWDir = fs.readdirSync('./Bot/Commands/NSFW').filter(file => file.endsWith('.js'));
const OthersDir = fs.readdirSync('./Bot/Commands/Others').filter(file => file.endsWith('.js'));
const commandfile = FunDir.concat(ModDir, NSFWDir, OthersDir)
for (const file of commandfile) {
var command
if (FunDir.includes(file)) {
command = require(`./Bot/Commands/Fun/${file}`);
}
if (ModDir.includes(file)) {
command = require(`./Bot/Commands/Moderation/${file}`);
}
if (NSFWDir.includes(file)) {
command = require(`./Bot/Commands/NSFW/${file}`);
}
if (OthersDir.includes(file)) {
command = require(`./Bot/Commands/Others/${file}`);
}
client.commands.set(command.name, command);
}
// Start the modules when the client is logged in.
function startModules() {
var requireDir = require('require-dir');
var dir = requireDir('./Bot/Side_Modules')
Object.keys(dir).forEach(async function (key) {
var module = dir[key];
if (!module.hasOwnProperty('start')) return
await module.start(client)
});
}
client.on('ready', () => {
startModules()
console.log('Online!');
client.user.setActivity(`${config.DEFAULTPREFIX}help | ANDRO Studios.`, {
status: "online",
game: {
type: "PLAYING" // PLAYING, WATCHING, LISTENING, STREAMING
}
});
})
// Handling uncaught exceptions.
process.on('uncaughtException', err => {
console.error('There was an uncaught error: ', err)
})
// Logging in.
client.login(config.TOKEN);