-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient.js
More file actions
162 lines (134 loc) · 3.85 KB
/
client.js
File metadata and controls
162 lines (134 loc) · 3.85 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
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
161
162
import { readdirSync } from 'fs'
import { join } from 'path'
import { Collection } from 'discord.js'
import { CommandoClient } from 'discord.js-commando'
import { setDefaults } from './services/tasks'
/*
Ensure that NODE_ENV is set to development if it is unset.
*/
if (!process.env.NODE_ENV) {
process.env.NODE_ENV = 'development'
}
const { NODE_ENV, COMMAND_PREFIX = '!' } = process.env
/*
We allow a comma-separated list of owner IDs, so check for
that and apply it back onto process.env if found.
*/
let OWNER_IDS = process.env.OWNER_IDS || '269617876036616193' // Default to @evan#9589
if (OWNER_IDS.includes(',')) {
OWNER_IDS = OWNER_IDS.split(',')
} else {
OWNER_IDS = [OWNER_IDS]
}
process.env.OWNER_IDS = OWNER_IDS
const PATH_TASKS = join(__dirname, 'tasks')
const PATH_TYPES = join(__dirname, 'types')
const PATH_COMMANDS = join(__dirname, 'commands')
const client = new CommandoClient({
owner: OWNER_IDS,
commandPrefix: COMMAND_PREFIX,
})
/*
Initialise tasks.
*/
client.tasks = new Collection()
const taskFiles = readdirSync(PATH_TASKS).filter(file => file.endsWith('.js'))
for (const file of taskFiles) {
try {
const { default: taskDefinition } = require(`./tasks/${file}`)
const taskInstance = new taskDefinition(client)
client.tasks.set(taskInstance.name, taskInstance)
} catch (e) {
console.warn('Could not load task file: ' + file)
console.error(e)
}
}
/*
Write configuration file if applicable (DB doesn't yet exist).
*/
setDefaults(client.tasks)
/*
Register command groups.
https://discord.js.org/#/docs/commando/master/class/CommandoRegistry?scrollTo=registerGroups
*/
client.registry.registerGroups([
{
id: 'documentation',
name: 'Documentation',
},
{
id: 'informational',
name: 'Informational',
},
{
id: 'moderation',
name: 'Moderation',
},
{
id: 'tasks',
name: 'Tasks',
},
{
id: 'rfcs',
name: 'RFCs',
},
])
if (NODE_ENV === 'development') {
client.registry.registerGroup('development', 'development')
}
/*
Register default command groups, commands and argument types.
And then register our own types and commands.
https://discord.js.org/#/docs/commando/master/class/CommandoRegistry?scrollTo=registerDefaults
https://discord.js.org/#/docs/commando/master/class/CommandoRegistry?scrollTo=registerTypesIn
https://discord.js.org/#/docs/commando/master/class/CommandoRegistry?scrollTo=registerCommandsIn
*/
client.registry.registerDefaults()
client.registry.registerTypesIn(PATH_TYPES)
client.registry.registerCommandsIn({
dirname: PATH_COMMANDS,
// NOTE: Exclude any commands in the development group, when in production.
excludeDirs: NODE_ENV === 'production' ? '^\\..*|development$' : undefined,
})
if (NODE_ENV === 'production') {
const evalCommand = client.registry.findCommands('eval')
if (evalCommand.length === 1) {
client.registry.unregisterCommand(evalCommand[0])
}
}
/*
Set up some global error handling and some purely informational event handlers.
*/
client.on('warn', console.warn)
client.on('error', console.error)
client.on('ready', () => console.info('Client ready!'))
client.on('resume', () => console.info('Connection resumed!'))
client.on('disconnect', () => console.info('Lost connection!'))
client.on('reconnecting', () => console.info('Attempting to reconnect.'))
process.on('unhandledRejection', console.error)
/*
Process jobs.
*/
client.on('message', msg => {
// Don't process own messages.
if (msg.author.id === msg.client.user.id) {
return
}
client.tasks
.filter(task => {
if (!task.enabled) {
return false
}
// Check for guild-specific tasks.
if (task.guild && msg.guild && task.guild !== msg.guild.id) {
return false
}
return true
})
.forEach(task => {
if (task.shouldExecute(msg)) {
task.run(msg)
}
})
})
export default client