This repository has been archived by the owner on Oct 12, 2023. It is now read-only.
forked from Chr1s70ph/ETIT-Master
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
115 lines (105 loc) · 2.87 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
const config = require("./privateData/config.json")
const discord = require("./node_modules/discord.js")
const client = new discord.Client()
const disbut = require("discord-buttons")(client)
const fs = require("fs")
client.commands = new discord.Collection()
client.aliases = new discord.Collection()
client.events = new discord.Collection()
client.on("ready", () => {
foo(client)
})
async function foo(client) {
await loadScripts(client)
await loadSlashCommands(client)
console.log("Online!")
}
client.login(config.botToken)
//Commands "handler"
/**
*
*/
fs.readdir("./commands/", (err, elements) => {
var path = "./commands/"
if (err) return console.log(err)
elements.forEach((file) => {
//loop through all elements in folder "commands"
const element_in_folder = fs.statSync(`./commands/${file}`)
if (element_in_folder.isDirectory() == true) {
//check if element in folder is a subfolder
const sub_directory = `./commands/${file}/`
fs.readdir(sub_directory, (err, files) => {
if (err) return console.log(err)
files.forEach((file) => {
setCommands(sub_directory, file, client) //adds commands from subfolder to collection
})
})
return
}
setCommands(path, file, client) //adds commands from parentfolder to collection
})
})
/**
* Adds all commands to the commands collection
* @param {string} path relative path to the file of each command
* @param {string} file actual filename of the file
* @param {Object} client
*/
function setCommands(path, file, client) {
if (!file.endsWith(".js")) return
let props = require(`${path}${file}`)
console.log("Successfully loaded command " + file)
let commandName = file.split(".")[0]
client.commands.set(commandName, props)
}
//Events "handler"
/**
* loads all events and adds them to the event collection
*/
fs.readdir("./events/", (err, files) => {
if (err) console.log(err)
files.forEach((file) => {
let eventFunc = require(`./events/${file}`)
console.log("Successfully loaded event " + file)
let eventName = file.split(".")[0]
client.on(eventName, (...args) => eventFunc.run(client, ...args))
})
})
/**
*
* @param {object} client necessary to start scripts relying on client
*/
async function loadScripts(client) {
let files
try {
files = await fs.promises.readdir("./startupScripts/")
} catch (e) {
console.log(e)
}
files.forEach((file) => {
let script = require(`./startupScripts/${file}`)
try {
script.run(client)
} catch (e) {
console.log(e)
}
console.log("Successfully executed startupScript " + file)
})
}
async function loadSlashCommands(client) {
let files
try {
files = await fs.promises.readdir("./slashCommands/")
} catch (e) {
console.log(e)
}
files.forEach((file) => {
let slashCommand = require(`./slashCommands/${file}`)
try {
slashCommand.run(client)
} catch (e) {
console.log(e)
}
console.log("Successfully posted slashCommand " + file)
})
}