-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.js
208 lines (185 loc) · 7.17 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
const fs = require("fs");
const { join } = require("path");
const Discord = require("discord.js");
const { Collection, Client, MessageEmbed } = require("discord.js");
const client = new Client();
const colors = {
error: 0xF91A3C,
info: 0x303136,
success: 0x13EF8D
}
const emotes = {
false: "❌",
true: "✔️",
owner: "👑",
bot: '🤖',
user: '👤'
}
const supportGuildId = ''
const supportGuildLogChannelId = ''
//Specify your bot token
const Bottoken = ''
// Insert here your userid if you wnat to use the status command
const ownerID = ""
const rawEmb = () => {
return new MessageEmbed()
.setColor(colors.info);
}
module.exports = { rawEmb }
client.ownerID = ownerID
client.colors = colors
client.emotes = emotes
client.supportGuildId = supportGuildId
client.supportGuildLogChannelId = supportGuildLogChannelId
if (!Bottoken) throw new Error('Please enter a Bot Token!');
//==================================================================================================================================================
//Loading Things
//==================================================================================================================================================
const { Server, syncDatabase } = require('./database/dbInit');
var server_cache = new Collection();
Reflect.defineProperty(server_cache, "getGuild", {
/**
* @param {number} id Guild ID
* @returns {Model} new User
*/
value: async function (id) {
var guild = server_cache.get(id);
if (!guild) guild = await Server.findOne({ where: { key: id } });
if (!guild) {
guild = await Server.create({ key: id });
server_cache.set(id, guild);
}
return guild;
}
});
Reflect.defineProperty(server_cache, "getChannel", {
/**
* @param {number} id Channel ID
* @returns {Model} new User
*/
value: async function () {
let arr = []
var channels = await Server.findAll();
channels.forEach(entry => arr.push(entry.channel))
return arr;
}
});
//Sync
const initDatabase = async () => {
await syncDatabase();
try {
for (let entr of (await Server.findAll())) {
server_cache.set(entr.user_id, entr);
}
console.log(" > 🗸 Cached Database Entries");
} catch (e) {
console.log(" > ❌ Error While Caching Database")
console.log(e);
//process.exit(1);
}
}
client.database = { server_cache };
//==================================================================================================================================================
//Initialize the Commands
//==================================================================================================================================================
client.commands = new Discord.Collection();
const commandFiles = fs
.readdirSync("./commands")
.filter(file => file.endsWith(".js"));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
//==================================================================================================================================================
//Starting the Bot
//==================================================================================================================================================
const start = async () => {
try {
console.log("Logging in...");
await client.login(Bottoken).catch(e => {
console.log(e.code);
switch (e.code) {
case 'TOKEN_INVALID':
console.error(" > ❌ Invalid Token");
break;
case 500:
console.error(" > ❌ Fetch Error");
break;
default:
console.error(" > ❌ Unknown Error");
console.error(' > ' + e);
break;
}
setTimeout(() => { throw e }, 5000);
});
await initDatabase();
} catch (e) {
console.log(e);
}
}
start();
client.on("ready", () => {
if (!supportGuildId) throw new Error('Please enter your Support-Guild-ID')
if (!supportGuildLogChannelId) throw new Error('Please enter your Support-Guild-Log-Channel-ID')
console.log(" > Logged in as: " + client.user.tag);
client.user.setPresence({ activity: { name: "Bump your server", type: 'PLAYING' }, status: 'idle' });
});
client.on('guildMemberAdd', async member => {
let { guild } = member
let settings = await client.database.server_cache.getGuild(guild.id)
if (!settings.wlc) return
let ch = await guild.channels.resolve(settings.wlc)
if (!ch) {
settings.wlc = undefined
return settings.save()
}
let emb = rawEmb().setTitle('Member Joined').setDescription(`${member} joined **${guild.name}**! Welcome you'r member No. **${guild.memberCount}**`)
ch.send(emb).catch(() => { })
})
client.on('guildCreate', async guild => {
let supGuild = await client.guilds.resolve(supportGuildId)
let channel = await supGuild.channels.resolve(supportGuildLogChannelId)
let emb = rawEmb().setTitle('Server joined').setColor(colors.success).setDescription(guild.name)
channel.send(emb).catch(() => { })
})
client.on('guildMemberRemove', async member => {
let { guild } = member
let settings = await client.database.server_cache.getGuild(guild.id)
if (!settings.gb) return
let ch = await guild.channels.resolve(settings.gb)
if (!ch) {
settings.gb = undefined
return settings.save()
}
let emb = rawEmb().setTitle('Member Leaved').setDescription(`${member} leaved from **${guild.name}** Bye Bye`)
ch.send(emb).catch(() => { })
})
client.on("message", async message => {
let emb = rawEmb()
if (message.author.bot) return;
let settings = await client.database.server_cache.getGuild(message.guild.id)
let prefix = settings.prefix;
if (message.mentions.users.first()) {
if (message.mentions.users.first().id == client.user.id) message.channel.send('My prefix is ' + settings.prefix)
}
if (!message.content.startsWith(prefix)) return;
const args = message.content.slice(prefix.length).split(/ +/);
const commandName = args.shift().toLowerCase();
const command = client.commands.find(cmd =>
cmd.commands.includes(commandName)
);
if (!command) {
emb.setDescription(`**I don´t know this command. Use ${prefix}help to see my commands** `)
return message.channel.send(emb.setColor(colors.error));
}
if (command.perm) {
if (!(message.member.hasPermission(command.perm))) {
emb.setDescription("**You are missing following permission:** `" + command.perm + "`")
return message.channel.send(emb.setColor(colors.error));
}
}
try {
message.client = client
command.execute(message, args);
} catch (error) { console.log(error) }
});