-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbot.js
More file actions
205 lines (198 loc) · 5.26 KB
/
bot.js
File metadata and controls
205 lines (198 loc) · 5.26 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
const discord = require('discord.js');
const System = require('./system/index');
const jndb = require('jndb');
const fs = require('fs');
const path = require('path');
const FileWatch = require('./filewatch');
/**
*
*
* @class Bot
* @extends {discord.Client}
*/
class Bot extends discord.Client {
/**
*
* @param {discord.ClientOptions} [options]
*/
constructor(options) {
super(options);
this.db = new jndb.Connection();
this.system = new System(this);
/**
* @type {discord.Collection<string,string>}
*/
this.banlist = new discord.Collection();
this.lockdown = {
enabled: false,
time: 0,
};
/**
* @type {discord.Collection<string,string>}
*/
this.csrCooldowns = new discord.Collection();
/**
* @type {string[]}
*/
this.filter = [];
this.prefixDB = new jndb.Connection({ fileName: 'prefixes.json' });
this.prefixDB.use('prefixes');
this.color = '#146ea4';
/**
* @type {Map<string,string>}
*/
this._allCmds = new Map();
fs.readdir(`./commands/`, (err, files) => {
// read dir
const jsfile = files.filter(
(f) =>
f.split('.').pop() === 'js' &&
!fs.statSync(process.cwd() + `/commands/` + f).isDirectory()
); // get all .js files
const categorys = files.filter((f) =>
fs.statSync(process.cwd() + `/commands/` + f).isDirectory()
);
jsfile.forEach((f, i) => {
// if commands present
try {
const props = require(`${process.cwd()}/commands/${f}`); // => load each one
if (props.help.aliases && !Array.isArray(props.help.aliases))
props.help.aliases = [props.help.aliases];
this._allCmds.set(props.help.name, `${process.cwd()}/commands/${f}`); // => add command to command list
} catch (err) {}
});
categorys.forEach((category) => {
const catfiles = fs
.readdirSync(`./commands/` + category)
.filter(
(f) =>
f.split('.').pop() === 'js' &&
!fs
.statSync(process.cwd() + `/commands/` + category + '/' + f)
.isDirectory()
);
catfiles.forEach((f, i) => {
try {
const props = require(`${process.cwd()}/commands/${category}/${f}`); // => load each one
props.help.category = category;
if (props.help.aliases && !Array.isArray(props.help.aliases))
props.help.aliases = [props.help.aliases];
this._allCmds.set(
props.help.name,
`${process.cwd()}/commands/${category}/${f}`
); // => add command to command list
} catch (err) {}
});
});
});
const fileWatch = new FileWatch();
fileWatch.watch('commands', (event, file) => {
if (event != 'change') return;
this.reloadCommands('commands');
});
//this.color = [20, 110, 164, 0.62];
}
/**
* @returns {Map<string,string>}
*/
get staff() {
const map = new Map();
this.db.use('staff');
const staff = this.db.fetchAll();
for (const i in staff) {
map.set(i, staff[i]);
}
return map;
}
/**
* @returns {discord.RichEmbed}
*/
get rules() {
const code = require('./ircrules');
const cd = new discord.RichEmbed()
.setColor([0, 200, 138])
.setDescription(code)
// @ts-ignore
.setFooter('IRC Code Of Conduct', this.user.displayAvatarURL);
return cd;
}
/**
*
* @param {string} message
*/
debug(message) {
const err = new Error();
const frame = err.stack.split('\n')[2];
const numbers = frame.match(/:\d/g).map((x) => x.replace(':', ''));
const lineNumber = numbers[0];
const functionName = frame.split(' ')[5];
console.log(functionName + ':' + lineNumber + ' ' + message);
}
backup() {
fs.copyFileSync('jndb.json', 'jndbBackup.json');
}
reloadCommands(folder) {
folder = path.resolve(folder);
const commandFiles = fs
.readdirSync(folder)
.filter((file) => file.endsWith('.js'));
const subDirs = fs
.readdirSync(folder)
.filter((file) => fs.statSync(folder + '/' + file).isDirectory());
for (const file of commandFiles) {
delete require.cache[require.resolve(`${folder}/${file}`)];
try {
const command = require(`${folder}/${file}`);
// @ts-ignore
this.commands.delete(command.help.name);
// @ts-ignore
this.commands.set(command.help.name, command);
} catch (err) {
console.log(err);
}
}
subDirs.forEach((category) => {
category = path.resolve(path.join(folder, category));
const catfiles = fs
.readdirSync(category)
.filter(
(f) =>
f.split('.').pop() === 'js' &&
!fs.statSync(category + '/' + f).isDirectory()
);
catfiles.forEach((f, i) => {
f = path.resolve(category + '/' + f);
delete require.cache[require.resolve(f)];
try {
const props = require(f); // => load each one
props.help.category = category.split('\\').pop();
if (props.help.aliases && !Array.isArray(props.help.aliases))
props.help.aliases = [props.help.aliases];
this.commands.set(props.help.name, props); // => add command to command list
} catch (err) {
console.log(
`${i} ${f} failed to in ${category} load!\n${err}\n${err.stack}\n`
);
}
});
});
}
/**
*
* @param {string} cmd
*/
unload(cmd) {
this.commands.delete(cmd);
}
/**
*
* @param {string} cmd
*/
load(cmd) {
if (!this._allCmds.has(cmd)) return;
const file = this._allCmds.get(cmd);
const props = require(file);
this.commands.set(props.help.name, props);
}
}
module.exports = Bot;