This repository has been archived by the owner on Oct 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
118 lines (110 loc) · 3.29 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
/* eslint-disable no-async-promise-executor */
"use strict";
// Essentials
const Discord = require("discord.js");
const fs = require("fs");
const db = require("./Data/dbConnection.js");
const { token } = require("./config.json");
const cc = require("kleur");
// Define the bot
const bot = new Discord.Client({
partials: ["MESSAGE", "CHANNEL", "REACTION"],
});
// Create Bot commands as collection
bot.commands = new Discord.Collection();
//Create file handlers
fs.readdir("./Commands", (err, files) => {
if (err) console.error(err);
const commFiles = files.filter((f) => f.split(".").pop() === "js");
if (commFiles.length <= 0) {
return console.log("No commands Found!");
} else {
console.log(commFiles.length + " commands found!");
}
commFiles.forEach((f) => {
const command = require(`./Commands/${f}`);
console.log(`Command ${f} loaded!`);
bot.commands.set(command.name, command);
});
});
fs.readdir("./Events/", (err, files) => {
if (err) return console.error(err);
files.forEach((file) => {
const event = require(`./Events/${file}`);
const eventName = file.split(".")[0];
bot.on(eventName, event.bind(null, bot));
});
});
// Create Bot Database Connection
const botConnect = () =>
new Promise(async (resolve) => {
bot.con = await db.Connect();
bot.con.on("error", async (e) => {
await db.reConnect(bot.con, e);
botConnect();
});
return resolve();
});
// Get Invites
const invites = {};
// activation to get the bot to work
bot.on("ready", async () => {
// Get all invites
bot.guilds.cache.forEach(g => {
g.fetchInvites().then(guildInvites => {
bot.invites = invites
bot.invites[g.id] = guildInvites;
})
})
// Update GuildDB on Start up
await botConnect();
async function guildInfo() {
try {
let guildDB = bot.guilds.cache.forEach((g) => {
let gInfo = {
guild_id: g.id,
guild_name: g.name,
guild_owner: g.owner.user.username,
guild_owner_id: g.ownerID,
guild_member_count: g.memberCount,
guild_prefix: g.prefix,
};
let gSql = "INSERT IGNORE INTO Guilds Set ?";
bot.con.query(gSql, gInfo, (err, result) => {
if (err) {
console.error(
"Unable to update Guild data Table: Index.js - Line 58"
);
}
return result;
});
});
guildDB;
// Get Guild Info and output as table
bot.con.query(
"SELECT G.guild_id, G.guild_name, G.guild_owner, G.guild_owner_id, G.guild_member_count, G.guild_prefix, G.def_channel FROM Guilds G",
(err, result) => {
if (err) {
console.error("Unable to get Guild data Table: Index.js - Line 79");
}
console.table(result);
bot.prefix = {};
bot.defChan = {};
result.forEach((r) => {
bot.prefix[r.guild_id] = r.guild_prefix;
bot.defChan[r.guild_id] = r.def_channel;
});
// Output that the bot is connected!
console.log(cc.green(`${bot.user.tag} Logged In!`));
}
);
} catch (e) {
console.error(e, "Unable to Connect!");
}
}
await guildInfo();
// Set Bot's activity
bot.user.setActivity("https://discord.gg/Tz3mRyJ");
});
// log-in
bot.login(token);