<commit_before>import Eris from 'eris'
import chalk from 'chalk'
import config from './config/config.js'
import cmdConfig from './config/command-config.js'
import { registerCommands } from './commands'
import { init } from './lib'
import redis from './data/redis.js'

let _bot

export async function connect () {
  _bot = new Eris.CommandClient(config.token, {}, {
    prefix: ['@mention ', 'b!'],
    owner: 'Gin#1913',
    // defaultHelpCommand: false,
    defaultCommandOptions: cmdConfig.defaultCommandOptions
  })

  _bot.on('ready', async () => {
    init(_bot)
    registerCommands(_bot)
    await applyGuildPrefixes()
    if (process.send) {
      process.send('ready')
    }

    // Purple looks so nice with the avatar...
    _bot.editStatus('online', { name: 'b!help', type: 1, url: 'https://www.twitch.tv/blairbot-nostream' })

    console.log('Ready!')
  })

  _bot.on('error', (msg) => console.error(chalk.red('ERROR:', msg)))
  _bot.on('warn', (msg) => console.warn(chalk.yellow('WARN:', msg)))

  if (process.env.NODE_ENV === 'dev') {
    _bot.on('debug', (msg) => console.log(chalk.green('DEBUG:', msg)))
  }

  // For systems with signals support
  process.on('SIGINT', async () => {
    await disconnect()
  })
  // For systems without signals support (I'm looking at you, Windows)
  process.on('message', async (msg) => {
    if (msg === 'shutdown') {
      await disconnect()
    }
  })

  return _bot.connect()
}

async function disconnect () {
  if (!_bot) {
    return
  }

  try {
    await _bot.disconnect({ reconnect: false })
    console.info(chalk.green('INFO: Bot killed gracefully by SIGINT'))
    process.exit(0)
  } catch (e) {
    console.error(e)
    process.exit(1)
  }
}

async function applyGuildPrefixes () {
  redis.client = redis.connect()

  const keys = await redis.client.keys(`guild_prefix:*`)
  const values = await redis.client.mget(keys)

  var substrLength = 'guild_prefix:'.length

  for (var i = 0; i < keys.length; i++) {
    _bot.registerGuildPrefix(keys[i].substring(substrLength), ['@mention ', values[i]])
  }

  redis.client.disconnect()
}

// STARTUP

if (process.env.NODE_ENV !== 'test') {
  try {
    connect()
  } catch (e) {
    console.error(chalk.red(e))
  }
}<commit_msg>Fix minor error on applying guild prefixes<commit_after>import Eris from 'eris'
import chalk from 'chalk'
import config from './config/config.js'
import cmdConfig from './config/command-config.js'
import { registerCommands } from './commands'
import { init } from './lib'
import redis from './data/redis.js'

let _bot

export async function connect () {
  _bot = new Eris.CommandClient(config.token, {}, {
    prefix: ['@mention ', 'b!'],
    owner: 'Gin#1913',
    // defaultHelpCommand: false,
    defaultCommandOptions: cmdConfig.defaultCommandOptions
  })

  _bot.on('ready', async () => {
    init(_bot)
    registerCommands(_bot)
    await applyGuildPrefixes()
    if (process.send) {
      process.send('ready')
    }

    // Purple looks so nice with the avatar...
    _bot.editStatus('online', { name: 'b!help', type: 1, url: 'https://www.twitch.tv/blairbot-nostream' })

    console.log('Ready!')
  })

  _bot.on('error', (msg) => console.error(chalk.red('ERROR:', msg)))
  _bot.on('warn', (msg) => console.warn(chalk.yellow('WARN:', msg)))

  if (process.env.NODE_ENV === 'dev') {
    _bot.on('debug', (msg) => console.log(chalk.green('DEBUG:', msg)))
  }

  // For systems with signals support
  process.on('SIGINT', async () => {
    await disconnect()
  })
  // For systems without signals support (I'm looking at you, Windows)
  process.on('message', async (msg) => {
    if (msg === 'shutdown') {
      await disconnect()
    }
  })

  return _bot.connect()
}

async function disconnect () {
  if (!_bot) {
    return
  }

  try {
    await _bot.disconnect({ reconnect: false })
    console.info(chalk.green('INFO: Bot killed gracefully by SIGINT'))
    process.exit(0)
  } catch (e) {
    console.error(e)
    process.exit(1)
  }
}

async function applyGuildPrefixes () {
  redis.client = redis.connect()

  const keys = await redis.client.keys(`guild_prefix:*`)

  if (keys.length === 0) {
    redis.client.disconnect()
    return
  }

  const values = await redis.client.mget(keys)

  var substrLength = 'guild_prefix:'.length

  for (var i = 0; i < keys.length; i++) {
    _bot.registerGuildPrefix(keys[i].substring(substrLength), ['@mention ', values[i]])
  }

  redis.client.disconnect()
}

// STARTUP

if (process.env.NODE_ENV !== 'test') {
  try {
    connect()
  } catch (e) {
    console.error(chalk.red(e))
  }
}
{'fraction_non_alphanumeric': 0.17049180327868851, 'fraction_numerical': 0.003715846994535519, 'mean_word_length': 3.3415559772296017, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4064659', 'n_tokens_mistral': 1603, 'n_tokens_neox': 1561, 'n_words': 355}