Skip to content

Attachments Usage

Arthur Neuman edited this page Jul 6, 2020 · 2 revisions

Using the attachments system

The attachments system is used to keep environments usable in commands that are defined outside the scope.

For example, with a proper modular commands system, you normally would not have access to your database manager. However, if your database manager is added as an attachment, your commands will be able to reference it with the agent attached.

Main file:

const {
  TOKEN,
  DATABASE_URL
} = process.env

const Eris = require('eris')
const Knex = require('knex')

const {
  commands
} = require('./data/')

const knex = new Knex({
  client: 'pg',
  connection: DATABASE_URL
})

const agent = new Agent({
  Eris,
  token: TOKEN,
  handlerData: {
    commands
  }
})

agent.attach('db', knex)

agent.connect()

Command file:

const {
  Command
} = require('cyclone-engine')

const data = {
  name: 'points',
  desc: 'See how many points you have',
  action: ({ agent, msg }) => {
    return agent.attachments.db('users')
      .select('points')
      .where({
        id: msg.author.id
      })
      .limit(1)
      .then((res) => {
        if (res) return res.points
        else return 'You aren\'t registered in the database'
      })
  }
}

module.exports = new Command(data)

Components


tui.jsdoc-template (Fork)

Clone this wiki locally