Skip to content

Serverside Prefixes

Arthur Neuman edited this page Jul 27, 2020 · 1 revision

Server-specific prefixes

Some servers encounter an issue where multiple bots have the same prefix. That's why some prefer to set a custom prefix for bots for their server. This is where guild prefixes come in.

Guild prefixes are usually stored in an external database and can, therefore, be initially supplied in the Agent constructor.

const agent = new Agent({
  Eris,
  token: TOKEN,
  options: {
    guildOptions: {
      [GUILD_ID]: {
        prefix: 'PREFIX'
      }
    }
})
  • If due to your code flow, the promise from the database is not resolved, it can be supplied anyway and the agent will await it.

The agent also comes with a method to ORM the usual database result format into something for the agent to use

const data = knex('guilds').select()
// [{
//   id: '123',
//   permissions: {},
//   prefix: '>'
// }]

const agent = new Agent({
  Eris,
  token: TOKEN,
  options: {
    guildOptions: data.then((res) => agent.compileGuildSQL(res))
  }
})

However, if a prefix needs to be set/altered while the bot is online, you can use the agent method Agent.setGuildPrefix(GUILD_ID, PREFIX)

To make sure these changes are pushed to the database, you can assign a "listener" for when a prefix is altered with the postEventFunctions system.

const agent = new Agent({
  Eris,
  token: TOKEN,
  options: {
    postEventFunctions: {
      prefix: postPrefix
    }
  }
})

function postPrefix (guildID, prefix, oldPrefix) {
  const guild = agent.client.guilds.get(guildID)

  console.log(`Guild ${guild.name} has changed their prefix from ${oldPrefix} to ${prefix}`)

  agent.attachments.db('guilds')
    .update('prefix', prefix)
    .where('id', guildID)
}

Components


tui.jsdoc-template (Fork)

Clone this wiki locally