This repository was archived by the owner on Jun 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathAtlas.js
More file actions
225 lines (179 loc) · 5.57 KB
/
Atlas.js
File metadata and controls
225 lines (179 loc) · 5.57 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
const Sentry = require('@sentry/node');
const path = require('path');
const Eris = require('eris');
const flatten = require('flat');
const fs = require('fs').promises;
const lib = require('atlas-lib');
const Util = require('./src/util');
const Agenda = require('./src/agenda');
const loader = require('./src/commands');
const structs = require('./src/structures');
const constants = require('./src/constants');
const Database = require('./src/structures/Database');
const ActionsInterval = require('./src/actionsInterval');
const { version } = require('./package.json');
// load eris addons cus lazy
const addons = require('./src/addons');
Object.keys(addons).forEach((a) => {
Object.values(addons[a]).forEach((Prop) => {
Prop(Eris);
});
});
module.exports = class Atlas {
constructor({
client,
}) {
module.exports = this;
this.version = version;
this.userAgent = `Atlas (https://github.com/atlasbot/bot, ${version})`;
this.client = client;
this.auditOverrides = [];
// an array of ID's for things like roles where if a member gets it the event should be ignored
// used for things like overriding the action log for "mute" to show more info
this.ignoreUpdates = [];
this.Sentry = Sentry;
this.structs = structs;
this.util = (new Util(this));
this.constants = constants;
this.colors = constants.colors;
// messages sent by the responder
this.sent = [];
this.commands = {
labels: new Map(),
aliases: new Map(),
get(label) {
return this.labels.get(label) || this.labels.get(this.aliases.get(label));
},
has(label) {
return this.labels.has(label) || this.aliases.has(label);
},
};
this.eventHandlers = new Map();
this.locales = new Map();
this.filters = new Map();
this.plugins = new Map();
this.agenda = new Agenda();
this.DB = new Database();
this.collectors = {
emojis: {
map: {},
get: id => this.collectors.emojis.map[id],
delete: (id, collector) => {
if (this.collectors.emojis.map[id]) {
const index = this.collectors.emojis.map[id].findIndex(c => c === collector);
if (this.collectors.emojis.map[id][index]) {
return !!this.collectors.emojis.map[id].splice(index);
}
}
},
add: (id, collector) => {
if (this.collectors.emojis.map[id]) {
this.collectors.emojis.map[id].push(collector);
} else {
this.collectors.emojis.map[id] = [collector];
}
},
},
};
// when a message ID matching the key is deleted, delete the map value (another ID)
this.deleteAliases = new Map();
this.version = require('./package.json').version;
this.lib = lib;
this.env = process.env.NODE_ENV || 'development';
this.actionsInterval = new ActionsInterval(this);
// whether this shard is the master shard
this.master = client.options.firstShardID === 0;
}
// im lazy
/**
* gets the bot's avatar URL
*/
get avatar() {
return this.client.user.avatarURL;
}
async launch() {
if (!process.env.SENTRY_DSN) {
console.warn('Error reporting disabled, set SENTRY_DSN to enable it.');
}
// setting up sentry for error tracking when possible
// if no SENTRY_DSN is provided sentry will will still "work"
Sentry.init({
dsn: process.env.SENTRY_DSN,
debug: process.env.NODE_ENV === 'development',
release: require('./package.json').version,
environment: process.env.NODE_ENV,
maxBreadcrumbs: 5,
// discord has "hiccups" where everything breaks and things time out which we can't do shit about
ignoreErrors: [
// should match "Gateway Time-out on xxx"
'Time-out on',
// should match "Request timed out (>Nms) on METHOD"
'Request timed out (',
// should match "DiscordHTTPError: 500 INTERNAL SERVER ERROR on POST xxx"
'500 INTERNAL SERVER ERROR on',
],
});
process.on('unhandledRejection', (reason) => {
console.warn(reason);
});
process.on('uncaughtException', async (err) => {
console.warn(err);
});
const events = await fs.readdir('src/events');
// Loading events
events.forEach((e) => {
const Handler = require(`./src/events/${e}`);
const handler = new Handler(this);
const name = e.split('.')[0];
this.eventHandlers.set(name, handler);
this.client.on(name, handler.execute.bind(handler));
delete require.cache[require.resolve(`./src/events/${e}`)];
console.log(`Loaded event handler: "${e}"`);
});
const filters = await fs.readdir('src/filters');
filters.forEach((f) => {
const Filter = require(`./src/filters/${f}`);
const filter = new Filter(this);
filter.info = Filter.info;
this.filters.set(f.split('.')[0], filter);
console.log(`Loaded chat filter: "${f}"`);
});
console.log(`Loaded ${filters.length} filters`);
console.log(`Loaded ${events.length} events`);
await this.loadLocales();
console.log(`Loaded ${this.locales.size} languages`);
// load commands
loader.setup(this);
}
async loadLocales() {
const sourceFiles = await fs.readdir('./locales/source');
const translatedFiles = await fs.readdir('./locales/translated');
const loadLocale = (dir, files) => {
const locale = {};
for (const file of files) {
const d = require(`./locales/${dir}/${file}`);
if (d) {
locale[path.basename(file).split('.').shift()] = d;
}
}
return flatten(locale, {
safe: true,
});
};
const en = loadLocale('source', sourceFiles);
for (const locale of translatedFiles) {
const flattened = loadLocale(`translated/${locale}`, sourceFiles);
this.locales.set(locale, {
data: {
...en,
...flattened,
},
code: locale,
});
}
this.locales.set('en', {
data: en,
code: 'en',
});
}
};