-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.js
160 lines (141 loc) · 4.95 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
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 { Client } = require('discord.js')
const { readdir } = require('fs-nextra')
const Enmap = require('enmap')
const EnmapLevel = require('enmap-level')
const klaw = require('klaw')
const path = require('path')
class designhubBot extends Client {
constructor(options) {
super(options)
try {
this.config = require('./configs/config.json')
} catch (error) {
throw new Error('Huch! Hier wurde was ned richtig eingerichtet..')
}
this.commands = new Enmap()
this.aliases = new Enmap()
this.monitors = new Enmap()
this.settings = new Enmap({ provider: new EnmapLevel({ name: 'settings' }) })
this.stats = new Enmap({ provider: new EnmapLevel({ name: 'stats' }) })
this.christmas = new Enmap({ provider: new EnmapLevel({ name: 'christmas' }) })
this.cooldown = new Set()
this.mutes = new Set()
}
permlevel(message) {
// Bot Owner gets 10
if (client.config.ownerID.includes(message.author.id)) return 10
// DM Channels
if (!message.guild || !message.member) return 0
if (message.guild.id !== client.config.mainGuildID) return 0
if (message.author.id === message.guild.ownerID)
// Guild Owner gets 10
return 9
// Loop through config
const ranks = require('./configs/permlevel.json')
for (let rank in ranks) {
if (ranks.hasOwnProperty(rank)) {
const role = message.guild.roles.find(r => r.name.toLowerCase() === rank.toLowerCase())
if (role && message.member.roles.has(role.id)) return ranks[rank]
}
}
// else
return 0
}
log(type, msg, title) {
if (!title) title = 'Log'
client.guilds
.get(client.config.mainGuildID)
.channels.get(client.config.logChannel)
.send('**[' + type + ']** [' + title + '] `' + msg + '`')
console.log(`[${type}] [${title}] ${msg}`)
}
loadCommand(commandPath, commandName) {
try {
const props = new (require(`${commandPath}${path.sep}${commandName}`))(client)
props.conf.location = commandPath
//console.log(`[log] Loading Command: ${props.help.name}. 👌`)
if (props.init) {
props.init(client)
}
client.commands.set(props.help.name, props)
props.conf.aliases.forEach(alias => {
client.aliases.set(alias, props.help.name)
})
return false
} catch (e) {
return e
}
}
async unloadCommand(commandPath, commandName) {
let command
if (client.commands.has(commandName)) {
command = client.commands.get(commandName)
} else if (client.aliases.has(commandName)) {
command = client.commands.get(client.aliases.get(commandName))
}
if (!command) return `The command \`${commandName}\` doesn"t seem to exist, nor is it an alias. Try again!`
if (command.shutdown) {
await command.shutdown(client)
}
delete require.cache[require.resolve(`${commandPath}/${commandName}.js`)]
return false
}
loadMonitor(monitorPath, monitorName) {
try {
const props = new (require(`${monitorPath}${path.sep}${monitorName}`))(client)
props.conf.location = monitorPath
//console.log(`[log] Loading Monitor: ${props.help.name}. 👌`)
if (props.init) {
props.init(client)
}
client.monitors.set(props.help.name, props)
return false
} catch (e) {
return e
}
}
async unloadMonitor(monitorPath, monitorName) {
let monitor
if (client.monitors.has(monitorName)) {
monitor = client.monitors.get(monitorName)
}
if (!monitor) return `The monitor \`${monitorName}\` doesn"t seem to exist. Try again!`
if (monitor.shutdown) {
await monitor.shutdown(client)
}
delete require.cache[require.resolve(`${monitorPath}/${monitorName}.js`)]
return false
}
}
const client = new designhubBot()
require('./functions/functions.js')(client)
const init = async () => {
// COMMANDS
klaw('./commands').on('data', item => {
const file = path.parse(item.path)
if (!file.ext || file.ext !== '.js') return
const response = client.loadCommand(file.dir, `${file.name}${file.ext}`)
if (response) console.log(response)
})
// MONITORS
klaw('./monitors').on('data', item => {
const file = path.parse(item.path)
if (!file.ext || file.ext !== '.js') return
const response = client.loadMonitor(file.dir, `${file.name}${file.ext}`)
if (response) console.log(response)
})
// EVENTS
const evtFiles = await readdir('./events/')
console.log(`[log] Loading a total of ${evtFiles.length} events.`)
evtFiles.forEach(file => {
const eventName = file.split('.')[0]
const event = new (require(`./events/${file}`))(client)
client.on(eventName, (...args) => event.run(...args))
//console.log(`[log] Loading Event: ${eventName}. ✔`)
delete require.cache[require.resolve(`./events/${file}`)]
})
// LOGIN
client.login(client.config.tokens.discord)
}
init()