This repository has been archived by the owner on Nov 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
176 lines (158 loc) · 4.22 KB
/
app.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
/******************
* Initialization *
******************/
require('./lib/format');
const EventEmitter = require('events').EventEmitter;
const mongodb = require('mongodb').MongoClient;
const discordjs = require('discord.js');
const dotenv = require('dotenv').config({
path: './private.env'
});
global._connections = new Map();
global._options = new Map();
global._symbols = /(\W)/g;
global._langfiles = new Map([['alloybot', require('./lib/lang.json')]]);
global._loader = require('./lib/loader');
global._logger = require('./lib/logger');
global._events = new EventEmitter();
global._bot = {
permissions: new discordjs.Permissions(
Number(process.env['DISCORD_PERMISSIONS'])
),
commands: new Map(),
metadata: new Map(),
prefix: process.env['CMD_PREFIX'],
groups: {
General: [],
Music: [],
Other: [],
Playlist: [],
Voice: []
}
};
_connections.set('database', {
promise: mongodb.connect(
`mongodb://${process.env['DB_HOST']}/${process.env['DB_NAME']}`,
{ useNewUrlParser: true }
)
});
_connections.set('discord', { client: new discordjs.Client() });
_connections.get('discord').promise = _connections
.get('discord')
.client.login(process.env['DISCORD_TOKEN']);
/******************
* Event Handling *
******************/
const database = _connections.get('database').promise;
const discord = _connections.get('discord');
_logger.start({ prefix: '0/1', message: 'Alloybot:', suffix: 'Starting.' });
database.catch(() => {
_logger.error({ prefix: '0/1', message: 'Database:', suffix: 'Failed.' });
});
database.then(client => {
_connections.get('database').client = client;
_connections.get('database').db = client.db();
_logger.success({
prefix: '1/1',
message: 'Database:',
suffix: 'Connected.'
});
});
discord.promise.then(() => {
_logger.success({
prefix: '1/1',
message: 'Discord:',
suffix: 'Connected.'
});
_logger.info({
prefix: '1/1',
message: 'Server Count:',
suffix: discord.client.guilds.size.toString()
});
discord.embed = function() {
let embed = new discordjs.RichEmbed();
embed.setColor('RANDOM');
embed.setAuthor(
discord.client.user.username,
discord.client.user.avatarURL
);
return embed;
};
});
discord.promise.catch(() => {
_logger.error({ prefix: '0/1', message: 'Discord:', suffix: 'Failed.' });
});
Promise.all([database, discord.promise])
.then(() => {
_bot.createCollection = require('./lib/cycleDb');
_loader(require('path').join(__dirname, './commands'));
_logger.complete({
prefix: '1/1',
message: 'Alloybot:',
suffix: 'Started.'
});
})
.catch(error => {
_logger.fatal({
prefix: '0/1',
message: 'Alloybot:',
suffix: `${error} | Failed to start.`
});
});
if (process.env['DEBUG'] == true) {
discord.client.on('debug', debug => {
_logger.debug({ prefix: 'Discord', message: debug });
});
}
/*************
* onMessage *
*************/
discord.client.on('message', message => {
if (message.content.startsWith(_bot.prefix)) onMessage(message);
});
function onMessage(message) {
if (message.author.id == discord.client.user.id) return;
_bot.split = message.content.split(' ');
_bot.command = message.content
.split(' ')
.shift()
.replace(global._symbols, '');
switch (message.channel.type) {
case 'dm':
discord.embed.thumbnail = message.recipient.displayAvatarURL;
_bot.createCollection(
message.channel.type,
message.channel.id,
message.channel.recipient.id
);
break;
case 'group':
_bot.createCollection(
message.channel.type,
message.channel.id,
message.channel.ownerID
);
discord.embed.thumbnail = message.channel.iconURL;
break;
case 'text':
_bot.createCollection('guild', message.guild.id, message.channel.ownerID);
discord.embed.thumbnail = message.guild.iconURL;
break;
default:
return;
}
try {
_bot.commands.get(_bot.command)(message);
} catch (error) {
message.channel.send(
lang.noCommand.format(
_bot.command.inlineCode(),
'cmdlist'.prefixed().inlineCode()
)
);
}
}
/***********
* Modules *
***********/
require('./modules/example/index');