-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
85 lines (73 loc) · 2.69 KB
/
main.js
File metadata and controls
85 lines (73 loc) · 2.69 KB
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
const { info } = require('console')
const Discord = require('discord.js')
const fs = require('fs')
const { Client } = require('pg')
const PGConnection = require('./src/database')
const util = require('util')
const discordclient = global.discordclient = new Discord.Client({ partials: ['MESSAGE', 'CHANNEL', 'REACTION'] })
const db = new PGConnection()
// requiring path and fs modules
const path = require('path')
discordclient.commands = new Discord.Collection()
commandCategories = []
// joining path of directory
const directoryPath = path.join(__dirname, 'commands')
const { readdirSync } = require('fs')
const getDirectories = source =>
readdirSync(source, { withFileTypes: true })
.filter(dirent => dirent.isDirectory())
.map(dirent => dirent.name)
getDirectories(directoryPath).forEach(function (dir) {
// passing directoryPath and callback function
fs.readdir(directoryPath + '/' + dir, function (err, files) {
// handling error
var cat_name = dir
var cat_description = 'No Description.'
var cat_commands = []
if (err) {
return console.log('Unable to scan directory: ' + err)
}
if (fs.existsSync(`${directoryPath}/${dir}/.info`)) {
cat_description = fs.readFileSync(`${directoryPath}/${dir}/.info`, 'utf8')
}
console.log(cat_description)
// listing all files using forEach
files.filter(name => name.endsWith('.js')).forEach(function (file) {
// Do whatever you want to do with the file
const command = require(`./commands/${dir}/${file}`)
if (typeof command.name !== 'undefined') {
discordclient.commands.set(command.name, command)
cat_commands.push({ name: command.name, description: command.description })
console.log(file)
}
})
commandCategories.push({
catName: cat_name,
catDescription: cat_description,
catCommands: cat_commands
})
})
})
discordclient.once('ready', () => {
db.connect()
console.log('Jarvis is Online!')
})
fs.readdir(path.join(__dirname, 'events'), function (err, files) {
if (err) return console.error(err)
files.forEach((file) => {
console.log(__dirname)
const eventFunction = require(`./events/${file}`)
if (eventFunction.disabled) return
const event = eventFunction.event || file.split('.')[0]
const emitter = (typeof eventFunction.emitter === 'string' ? discordclient[eventFunction.emitter] : eventFunction.emitter) || discordclient
const { once } = eventFunction
try {
emitter[once ? 'once' : 'on'](event, (...args) => eventFunction.run(...args))
} catch (error) {
console.error(error.stack)
}
})
})
const botToken = process.env.COSCYBOT
// console.log(bot_token);
discordclient.login(botToken)