forked from AlexzanderFlores/WOKCommands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
138 lines (113 loc) · 3.55 KB
/
index.ts
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
import { Client, Guild } from 'discord.js'
import path from 'path'
import CommandHandler from './CommandHandler'
import FeatureHandler from './FeatureHandler'
import ICommand from './interfaces/ICommand'
import mongo from './mongo'
import prefixes from './modles/prefixes'
import getAllFiles from './get-all-files'
class WOKCommands {
private _defaultPrefix = '!'
private _commandsDir = 'commands'
private _featureDir = ''
private _mongo = ''
private _syntaxError = 'Incorrect usage!'
private _prefixes: { [name: string]: string } = {}
private _commandHandler: CommandHandler
private _featureHandler: FeatureHandler | null = null
constructor(client: Client, commandsDir?: string, featureDir?: string) {
if (!client) {
throw new Error('No Discord JS Client provided as first argument!')
}
if (!commandsDir) {
console.warn(
'WOKCommands > No commands folder specified. Using "commands"'
)
}
// Get the directory path of the project using this package
// This way users don't need to use path.join(__dirname, 'dir')
if (module && module.parent) {
// @ts-ignore
const { path } = module.parent
if (path) {
commandsDir = `${path}/${commandsDir || this._commandsDir}`
if (featureDir) {
featureDir = `${path}/${featureDir}`
}
}
}
this._commandsDir = commandsDir || this._commandsDir
this._featureDir = featureDir || this._featureDir
this._commandHandler = new CommandHandler(this, client, this._commandsDir)
if (this._featureDir) {
this._featureHandler = new FeatureHandler(client, this._featureDir)
}
setTimeout(() => {
if (this._mongo) {
mongo(this._mongo)
} else {
console.warn(
'WOKCommands > No MongoDB connection URI provided. Some features might not work! See this for more details:\nhttps://github.com/AlexzanderFlores/WOKCommands#setup'
)
}
}, 500)
// Register built in commands
for (const [file, fileName] of getAllFiles(
path.join(__dirname, 'commands')
)) {
this._commandHandler.registerCommand(this, client, file, fileName)
}
// Load prefixes from Mongo
const loadPrefixes = async () => {
const results: any[] = await prefixes.find({})
for (const result of results) {
const { _id, prefix } = result
this._prefixes[_id] = prefix
}
console.log(this._prefixes)
}
loadPrefixes()
}
public get mongoPath(): string {
return this._mongo
}
public setMongoPath(mongoPath: string): WOKCommands {
this._mongo = mongoPath
return this
}
public get syntaxError(): string {
return this._syntaxError
}
public setSyntaxError(syntaxError: string): WOKCommands {
this._syntaxError = syntaxError
return this
}
public get prefixes() {
return this._prefixes
}
public get defaultPrefix(): string {
return this._defaultPrefix
}
public setDefaultPrefix(defaultPrefix: string): WOKCommands {
this._defaultPrefix = defaultPrefix
return this
}
public getPrefix(guild: Guild | null): string {
return this._prefixes[guild ? guild.id : ''] || this._defaultPrefix
}
public setPrefix(guild: Guild | null, prefix: string) {
if (guild) {
this._prefixes[guild.id] = prefix
}
}
public get commandHandler(): CommandHandler {
return this._commandHandler
}
public get commands(): ICommand[] {
return this._commandHandler.commands
}
public get commandAmount(): number {
return this.commands.length
}
}
export = WOKCommands