This repository was archived by the owner on Mar 7, 2021. It is now read-only.
forked from r3volved/LaZBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlazbot.js
More file actions
executable file
·246 lines (192 loc) · 8.31 KB
/
Copy pathlazbot.js
File metadata and controls
executable file
·246 lines (192 loc) · 8.31 KB
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
//Build client
const Discord = require('discord.js');
const client = new Discord.Client();
let instance = {};
client.banlist = [];
/**
* MONITOR CHANNEL
*/
//ON MESSAGE RECEIVED
client.on('message', message => {
// If author is bot, ignore - otherwise register message
if( message.author.bot ) { return; }
if( client.banlist && client.banlist.includes( message.author.id ) ) { return; }
try {
instance.registry.registerMessage( message, instance );
} catch(e) {
console.warn("Message listener problem!");
console.error(e);
}
});
//ON MESSAGE UPDATED
client.on('messageUpdate', (oldMessage, newMessage) => {
// If author is bot, ignore - otherwise register message
if( newMessage.author.bot ) { return; }
if( client.banlist && client.banlist.includes( newMessage.author.id ) ) { return; }
try {
instance.registry.registerMessage( newMessage, instance );
} catch(e) {
console.warn("Message listener problem!");
console.error(e);
}
});
//ON GUILD JOIN
client.on('guildCreate', guild => {
try {
if( !instance.logging ) { return; }
instance.dbHandler.setRows(instance.settings.database,"INSERT INTO `botlog` VALUES (?, ?)",[new Date(), ` ! ${instance.client.user.username} joined ${guild.name}`]);
} catch(e) {
console.warn("Message listener problem!");
console.error(e);
}
});
//ON GUILD MEMBER ADD
client.on('guildMemberAdd', async (member) => {
try {
let result = null;
let sql = "SELECT * FROM `factions` WHERE `factions`.`serverId` = ?";
let args = [];
args.push( await member.guild.id );
try {
result = await instance.dbHandler.doSQL(instance.settings.database, sql, args)
if( !result || result.length === 0 ) { return; }
} catch(e) {
return;
}
//Otherwise build embed
const Discord = require('discord.js');
const embed = new Discord.RichEmbed();
let description = 'To participate on this server, you must pick a side!\nAre you:```md\n';
for( let r of result ) {
if( r.type === 'DEFAULT' ) { continue; }
description += '# '+r.faction+'\n'
}
description += '```\nTo access the server, please choose your loyalty by typing in the command below:\n```'+instance.settings.prefix+"join <faction>```";
embed.setColor('0x6F9AD3');
let title = 'Welcome to the server!';
if( member.guild.id === "333971980497977345" ) {
title = 'Welcome to Wrigley Starport- CubsFanHan\'s discord server!';
}
embed.setTitle('Welcome to the server!');
embed.setDescription(description);
await client.channels.find("id",result[0].channelId).send('Hello there <@'+member.id+'>! That was a smooth landing- nice work.',{embed});
} catch(e) {
console.warn("Message listener problem!");
console.error(e);
}
});
/**
* MONITOR CLIENT CONNECTION
*/
//ON READY
client.on('ready', async () => {
instance.client = client;
instance.status = '';
const colors = require('./utilities/console-colors.js');
try {
let result = await instance.dbHandler.setRows(instance.settings.database,"INSERT INTO `botlog` VALUES (?, ?)",[new Date(), `Connected as: ${instance.client.user.username}`]);
if( !result ) { instance.logging = false; }
} catch(e) {
console.error(e);
instance.logging = false;
}
let len = 28;
let version = ' '.repeat(len - instance.settings.version.length)+instance.settings.version;
let name = ' '.repeat(len - instance.client.user.username.length)+instance.client.user.username;
let prefix = ' '.repeat(len - 9)+'Prefix: '+instance.settings.prefix;
console.info('='.repeat(80));
console.info('='.repeat(80));
console.info(` ██╗ █████╗ ███████╗██████╗ ██████╗ ████████╗\n ██║ ██╔══██╗╚══███╔╝██╔══██╗██╔═══██╗╚══██╔══╝${version}\n ██║ ███████║ ███╔╝ ██████╔╝██║ ██║ ██║ \n ██║ ██╔══██║ ███╔╝ ██╔══██╗██║ ██║ ██║ ${name}\n ███████╗██║ ██║███████╗██████╔╝╚██████╔╝ ██║ ${prefix}\n ╚══════╝╚═╝ ╚═╝╚══════╝╚═════╝ ╚═════╝ ╚═╝ `)
console.info('='.repeat(80));
console.info(`Started successfully with configuration: ${process.argv[2]}`);
if( !instance.logging ) {
console.log(colors.Bright+colors.FgRed);
console.log(' ! Could not connect to database - logging has been disabled');
console.log(colors.Reset);
}
/**
* Once connected, build module registry
* The module registry is the core of the bot and ties all the module configurations together and organizes scheduling
*/
try {
const ModuleRegistry = require(instance.path+'/modules/modules.registry.js');
instance.registry = new ModuleRegistry();
await instance.registry.load( instance );
await instance.client.user.setPresence({game:{ name:instance.settings.prefix+"help", type:"LISTENING" }});
} catch(e) { console.error(e); }
});
//ON DISCONNECT
client.on('disconnect', async (event) => {
console.error(`\n ! Client disconnected: [${event.code}] ${event.reason}`);
try{
if( !instance.logging ) { return; }
instance.dbHandler.setRows(instance.settings.database,"INSERT INTO `botlog` VALUES (?, ?)",[new Date(), ` ! Client disconnected: [${event.code}] ${event.reason}`]);
} catch(e) { console.error(e); }
//Try login again
if( event.code !== 4004 ) {
try {
await doLogin();
} catch(e) {
console.error('Error: trying to re-login\n',e);
}
}
});
//ON RECONNECTING
client.on('reconnecting', async (e) => {
console.warn('\n ! Client reconnecting -'+new Date());
if(e) console.error(e);
try {
if( !instance.logging ) { return; }
instance.dbHandler.setRows(instance.settings.database,"INSERT INTO `botlog` VALUES (?, ?)",[new Date(), 'Client reconnecting']);
} catch(e) { console.error(e); }
});
//ON RESUME
client.on('resumed', async (replayed) => {
console.info('\n ! Client resumed -'+new Date());
if(replayed) console.log(replayed);
try {
if( !instance.logging ) { return; }
instance.dbHandler.setRows(instance.settings.database,"INSERT INTO `botlog` VALUES (?, ?)",[new Date(), 'Client resumed']);
} catch(e) { console.error(e); }
});
//ON ERROR
client.on('error', async (error) => {
console.error('\n ! Client connection error -'+new Date());
if(error) console.error(error);
try {
if( !instance.logging ) { return; }
instance.dbHandler.setRows(instance.settings.database,"INSERT INTO `botlog` VALUES (?, ?)",[new Date(), 'Client connection error']);
} catch(e) { console.error(e); }
});
//ON WARNING
client.on('warn', async (info) => {
console.warn('\n ! Client warning -'+new Date());
if(info) console.warn(info);
try{
if( !instance.logging ) { return; }
instance.dbHandler.setRows(instance.settings.database,"INSERT INTO `botlog` VALUES (?, ?)",[new Date(), 'Client warning']);
} catch(e) { console.error(e); }
});
/**
* LOGIN WITH TOKEN
*/
async function doLogin() {
try {
instance.path = process.cwd().toString().replace(/\\/g,'\/');
instance.settings = require(instance.path+'/config/'+process.argv[2].replace(/(\.json)/,'')+'.json');
instance.settings.version = require(instance.path+'/config/base/version.json');
instance.settings.reaction = require(instance.path+'/config/base/reactions.json');
let utilities = instance.path+'/utilities/';
instance.cmdHandler = require(utilities+'command-handler.js');
instance.dbHandler = require(utilities+'db-handler.js');
instance.rssHandler = require(utilities+'rss-handler.js');
instance.permHandler = require(utilities+'permission-handler.js');
instance.logging = true;
await client.login(instance.settings.token);
} catch(err) {
console.error('\n ! '+err);
process.exit(-1);
}
}
//TRIGGER LOGIN
doLogin();