WOKCommands
Search…
MongoDB
WOKCommands has built in MongoDB connection support. You can customize the connection options still, but this allows you to not worry about connecting to your database. Example:
JavaScript
TypeScript
index.js
1
const DiscordJS = require('discord.js')
2
const WOKCommands = require('wokcommands')
3
const path = require('path')
4
5
const { Intents } = DiscordJS
6
7
const client = new DiscordJS.Client({
8
// These intents are recommended for the built in help menu
9
intents: [
10
Intents.FLAGS.GUILDS,
11
Intents.FLAGS.GUILD_MESSAGES,
12
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
13
],
14
})
15
16
client.on('ready', () => {
17
const dbOptions = {
18
// These are the default values
19
keepAlive: true
20
}
21
22
new WOKCommands(client, {
23
// The name of the local folder for your command files
24
commandsDir: path.join(__dirname, 'commands'),
25
// Pass in the new dbOptions
26
dbOptions,
27
// Pass in your own mongo connection URI
28
mongoUri: ''
29
})
30
})
31
32
client.login('YOUR TOKEN HERE')
Copied!
index.ts
1
import DiscordJS, { Intents } from 'discord.js'
2
import WOKCommands from 'wokcommands'
3
import path from 'path'
4
5
const client = new DiscordJS.Client({
6
// These intents are recommended for the built in help menu
7
intents: [
8
Intents.FLAGS.GUILDS,
9
Intents.FLAGS.GUILD_MESSAGES,
10
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
11
],
12
})
13
14
client.on('ready', () => {
15
const dbOptions = {
16
// These are the default values
17
keepAlive: true
18
}
19
20
new WOKCommands(client, {
21
// The name of the local folder for your command files
22
commandsDir: path.join(__dirname, 'commands'),
23
// Allow importing of .ts files if you are using ts-node
24
typeScript: true,
25
// Pass in the new dbOptions
26
dbOptions,
27
// Pass in your own mongo connection URI
28
mongoUri: ''
29
})
30
})
31
32
client.login('YOUR TOKEN HERE')
Copied!
.env
1
MONGO_URI=YOUR_URI_HERE
Copied!

Listening for when the database is connected

A custom event is fired whenever the database is connected, read more about it here.

Accessing built-in Mongo models/schemas

You can access any of the built-in models by accessing the MongoDB connection and then accessing the model from the models array like so:
JavaScript
TypeScript
index.js
1
const DiscordJS = require('discord.js')
2
const WOKCommands = require('wokcommands')
3
const path = require('path')
4
5
const { Intents } = DiscordJS
6
7
const client = new DiscordJS.Client({
8
// These intents are recommended for the built in help menu
9
intents: [
10
Intents.FLAGS.GUILDS,
11
Intents.FLAGS.GUILD_MESSAGES,
12
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
13
],
14
})
15
16
client.on('ready', () => {
17
const dbOptions = {
18
// These are the default values
19
keepAlive: true,
20
useNewUrlParser: true,
21
useUnifiedTopology: true,
22
useFindAndModify: false
23
}
24
25
const wok = new WOKCommands(client, {
26
// The name of the local folder for your command files
27
commandsDir: path.join(__dirname, 'commands'),
28
// Pass in the new dbOptions
29
dbOptions,
30
// Pass in your own mongo connection URI
31
mongoUri: ''
32
})
33
34
wok.on('databaseConnected', async (connection, state) => {
35
const model = connection.models['wokcommands-languages']
36
37
const results = await model.countDocuments()
38
console.log(results)
39
})
40
})
41
42
client.login('YOUR TOKEN HERE')
Copied!
index.ts
1
import DiscordJS, { Intents } from 'discord.js'
2
import WOKCommands from 'wokcommands'
3
import path from 'path'
4
5
const client = new DiscordJS.Client({
6
// These intents are recommended for the built in help menu
7
intents: [
8
Intents.FLAGS.GUILDS,
9
Intents.FLAGS.GUILD_MESSAGES,
10
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
11
],
12
})
13
14
client.on('ready', () => {
15
const dbOptions = {
16
// These are the default values
17
keepAlive: true,
18
useNewUrlParser: true,
19
useUnifiedTopology: true,
20
useFindAndModify: false
21
}
22
23
const wok = new WOKCommands(client, {
24
// The name of the local folder for your command files
25
commandsDir: path.join(__dirname, 'commands'),
26
// Pass in the new dbOptions
27
dbOptions,
28
// Pass in your own mongo connection URI
29
mongoUri: ''
30
})
31
32
wok.on('databaseConnected', async (connection, state) => {
33
const model = connection.models['wokcommands-languages']
34
35
const results = await model.countDocuments()
36
console.log(results)
37
})
38
})
39
40
client.login('YOUR TOKEN HERE')
Copied!
Here is a list of all mongo models/schemas:
  • wokcommands-cooldowns
  • wokcommands-disabled-commands
  • wokcommands-languages
  • wokcommands-prefixes
  • wokcommands-required-roles
  • wokcommands-channel-commands
  • wokcommands-slash-commands