This repository has been archived by the owner on Mar 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
245 lines (211 loc) · 7.6 KB
/
index.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
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
if (process.version.slice(1).split(".")[0] < 8) throw new Error("Node 8.0.0 or higher is required. Update Node on your system.");
const Discord = require("discord.js");
const { promisify } = require("util");
const readdir = promisify(require("fs").readdir);
const Enmap = require("enmap");
const EnmapLevel = require("enmap-level");
const {PlayerManager} = require("discord.js-lavalink");
const snekfetch = require('snekfetch');
var email = require("emailjs");
const client = new Discord.Client({
autoReconnect: true,
shardCount: "auto"
});
client.config = require("./config.js");
client.logger = require("./modules/Logger");
client.db = require("./modules/database");
require("./modules/functions.js")(client);
client.commands = new Enmap();
client.aliases = new Enmap();
client.queue = new Enmap();
client.settings = new Enmap({provider: new EnmapLevel({name: "settings"})});
client.lavaLinkNodes = client.config["lava-link"].nodes;
process.on('SIGINT', () => {
client.guilds.forEach(guild => {
if (client.playerManager.get(guild)) client.playerManager.leave(guild);
});
process.exit();
});
process.on('SIGHUP', () => process.emit('SIGINT'));
process.on('SIGTERM', () => process.emit('SIGINT'));
client.getSongs = async function getSongs(string) {
const res = await snekfetch.get(`http://localhost:2333/loadtracks?identifier=ytsearch:${string}`)
.set("Authorization", "youshallnotpass")
.catch(err => {
console.error(err);
return null;
});
if (!res) return {error: "There was an error, try again"};
if (res.body.length < 1) return {error: "No tracks found"};
return res.body;
};
class Player extends PlayerManager {
constructor(client, lavaLinkNodes, param3) {
super(client, lavaLinkNodes, param3);
}
join(data, {selfmute = false, selfdeaf = false} = {}) {
const player = this.get(data.guild.id);
if (player) return player;
this.client.ws.shards[data.guild.shardID].send({
op: 4,
d: {
guild_id: data.guild.id,
channel_id: data.channel,
self_mute: selfmute,
self_deaf: selfdeaf
}
});
return this.spawnPlayer({
host: data.host,
guild: data.guild.id,
channel: data.channel
});
}
leave(guild) {
this.client.ws.shards[guild.shardID].send({
op: 4,
d: {
guild_id: guild.id,
channel_id: null,
self_mute: false,
self_deaf: false
}
});
const player = this.get(guild.id);
if (!player) return false;
player.removeAllListeners();
player.destroy();
return this.delete(guild.id);
}
async voiceServerUpdate(data) {
const guild = this.client.guilds.get(data.guild_id);
if (!guild) return;
const player = this.get(data.guild_id);
if (!player) return;
if (!guild.me) await guild.members.fetch(this.client.user.id).catch(() => null);
player.connect({
session: guild.me.voiceSessionID,
event: data
});
}
}
client.on('ready', () => {
client.playerManager = new Player(client, client.lavaLinkNodes, {
user: client.user.id
});
});
client.mailer = email.server.connect({
user: client.config.mailer.address,
password: client.config.mailer.password,
host: client.config.mailer.host,
ssl: client.config.mailer.ssl
});
const init = async () => {
const cmdFiles = await readdir("./commands/");
client.logger.log(`Loading a total of ${cmdFiles.length} commands.`);
cmdFiles.forEach(f => {
if (!f.endsWith(".js")) return;
const response = client.loadCommand(f);
if (response) console.log(response);
});
const evtFiles = await readdir("./events/");
client.logger.log(`Loading a total of ${evtFiles.length} events.`);
evtFiles.forEach(file => {
const eventName = file.split(".")[0];
const event = require(`./events/${file}`);
client.on(eventName, event.bind(null, client));
const mod = require.cache[require.resolve(`./events/${file}`)];
delete require.cache[require.resolve(`./events/${file}`)];
for (let i = 0; i < mod.parent.children.length; i++) {
if (mod.parent.children[i] === mod) {
mod.parent.children.splice(i, 1);
break;
}
}
});
client.on('guildMemberAdd', async (member) => {
client.db.get(`SELECT * FROM servers WHERE id = ?`, [member.guild.id], (err, row) => {
if (err) return console.log(err.message);
if (!row) return;
if (row.welcomeLog == null) return;
if (row.welcomeLog == 'off') return;
if (row.welcomeMessage == '') return;
if (row.welcomeMessage == null) return;
const channel = client.channels.get(row.welcomeLog)
if (!channel) return;
const welcomeMessage = row.welcomeMessage
.replace("{user.name}", member.user.username)
.replace("{user.mention}", "<@" + member.user.id + ">")
.replace("{member.count}", member.guild.memberCount)
.replace("{server.name}", member.guild.name);
channel.send(welcomeMessage)
})
})
client.on('guildMemberAdd', async (member) => {
client.db.get(`SELECT * FROM servers WHERE id = ?`, [member.guild.id], (err, row) => {
if (err) return console.log(err.message);
if (!row) return;
if (row.joinRole == null) return;
if (row.joinRole == 'off') return;
if (!member.guild.roles.get(row.joinRole)) return;
member.roles.add(row.joinRole).catch(e => console.error("Failed to add role to " + member.user.username + " in guild " + member.guild.name))
})
})
client.on('guildMemberRemove', async (member) => {
client.db.get(`SELECT * FROM servers WHERE id = ?`, [member.guild.id], (err, row) => {
if (err) return console.log(err.message);
if (!row) return;
if (row.welcomeLog == null) return;
if (row.welcomeLog == 'off') return;
if (row.leaveMessage == '') return;
if (row.leaveMessage == null) return;
const channel = client.channels.get(row.welcomeLog)
if (!channel) return;
const leaveMessage = row.leaveMessage
.replace("{user.name}", member.user.username)
.replace("{user.mention}", "<@" + member.user.id + ">")
.replace("{member.count}", member.guild.memberCount)
.replace("{server.name}", member.guild.name);
channel.send(leaveMessage)
})
})
// server-log
client.on('guildMemberAdd', async (member) => {
client.db.get("SELECT * FROM servers WHERE id = ?", [member.guild.id], (err, row) => {
if (!row) return;
if (err) return console.log(err.message);
if (row.serverlog == 'off') return;
if (row.serverlog == null) return;
const channel = client.channels.get(row.serverlog)
const embed = new Discord.MessageEmbed()
.setTitle("Member Joined")
.setThumbnail(member.user.avatarURL())
.addField("Username:", member.user.tag, true)
channel.send(embed)
});
})
client.on('guildMemberRemove', async (member) => {
client.db.get("SELECT * FROM servers WHERE id = ?", [member.guild.id], (err, row) => {
if (!row) return;
if (err) return console.log(err.message);
if (row.serverlog == 'off') return;
if (row.serverlog == null) return;
const channel = client.channels.get(row.serverlog)
const embed = new Discord.MessageEmbed()
.setTitle("Member Left")
.setThumbnail(member.user.avatarURL())
.addField("Username:", member.user.tag, true)
channel.send(embed)
});
})
client.levelCache = {};
for (let i = 0; i < client.config.permLevels.length; i++) {
const thisLevel = client.config.permLevels[i];
client.levelCache[thisLevel.name] = thisLevel.level;
}
client.on('error', (error) => {
console.log(error)
})
client.login(client.config.token);
};
init();