import Commando from 'discord.js-commando'
import Community from './community/community'

import path from 'path'
import sqlite from 'sqlite'
import winston from 'winston'

import config from '/data/config.json'

String.prototype.capitalizeFirstLetter = function() { return this.charAt(0).toUpperCase() + this.slice(1) }

winston.remove(winston.transports.Console)
winston.add(winston.transports.Console, { level: config.logLevel })

const client = new Commando.Client({ owner: config.owner, commandPrefix: config.prefix })

client.on('debug', winston.debug)
client.on('warn', winston.warn)
client.on('error', winston.error)

client.on('guildCreate', guild => { winston.info(`Joined Guild: ${guild.name}.`) })
client.on('guildDelete', guild => { winston.info(`Departed Guild: ${guild.name}.`) })

client.on('guildMemberRemove', guildMember => { Community.dropDepartedMember(guildMember.guild.id, guildMember.id) })

client.on('disconnect', event => { winston.warn(`Disconnected [${event.code}]: ${event.reason || 'Unknown reason'}`)} )
client.on('reconnecting', () => { winston.info('Reconnecting...')})


client.on('ready', async () => {
    await Community.init(client).catch(winston.error)
    winston.info('Client Ready.')
    winston.verbose(`Current Guilds (${client.guilds.size}): ${client.guilds.map(guild => { return guild.name }).join('; ')}.`)
    client.user.setGame('Hearthstone')
})

client.setProvider(
    sqlite.open('/data/settings.sqlite3').then(db => new Commando.SQLiteProvider(db))
).catch(winston.error)

client.registry
    .registerDefaultTypes()
    .registerGroups([
        ['card', 'Card Information'],
        ['community', 'Community']
    ])
    .registerDefaultGroups()
    .registerCommandsIn(path.join(__dirname, 'commands'))
    .registerDefaultCommands({ eval_: false, commandState: false })

client.login(config.token)

@@ -52,16 +52,23 @@
 ommunity
+Manager
  from '.
@@ -91,61 +91,154 @@
 nity
+-manager
 '%0A
-%0A
 import 
-path from 'path'%0Aimport sqlite from 'sqlite
+PostgreSQL from './postgresql/postgresql'%0Aimport SequelizeProvider from './postgresql/sequelize-provider'%0A%0Aimport path from 'path
 '%0Aim
@@ -527,16 +527,86 @@
 vel %7D)%0A%0A
+const postgresql = new PostgreSQL(config.database)%0Apostgresql.init()%0A%0A
 const cl
@@ -686,24 +686,78 @@
 refix %7D)
+%0Aconst communityManager = new CommunityManager(client)
 %0A%0Aclient
 .on('deb
@@ -744,24 +744,29 @@
 ent)%0A%0Aclient
+%0A    
 .on('debug',
@@ -781,22 +781,20 @@
 .debug)%0A
-client
+    
 .on('war
@@ -811,22 +811,20 @@
 n.warn)%0A
-client
+    
 .on('err
@@ -843,23 +843,205 @@
 .error)%0A
-%0Aclient
+    .on('disconnect', event =%3E %7B winston.warn(%60Disconnected %5B$%7Bevent.code%7D%5D: $%7Bevent.reason %7C%7C 'Unknown reason'%7D%60) %7D)%0A    .on('reconnecting', () =%3E %7B winston.info('Reconnecting...') %7D)%0A    
 .on('gui
@@ -1070,20 +1070,23 @@
 winston.
-info
+verbose
 (%60Joined
@@ -1113,22 +1113,20 @@
 %7D.%60) %7D)%0A
-client
+    
 .on('gui
@@ -1138,32 +1138,40 @@
 ete', guild =%3E %7B
+%0A       
  winston.info(%60D
@@ -1163,20 +1163,23 @@
 winston.
-info
+verbose
 (%60Depart
@@ -1204,27 +1204,81 @@
 name%7D.%60)
- %7D)%0A%0Aclient
+%0A        communityManager.handleGuildDepart(guild.id)%0A    %7D)%0A    
 .on('gui
@@ -1311,25 +1311,25 @@
 er =%3E %7B 
-C
+c
 ommunity
 .dropDep
@@ -1324,27 +1324,34 @@
 nity
-.dropDepartedMember
+Manager.handleMemberDepart
 (gui
@@ -1392,205 +1392,12 @@
  %7D)%0A
-%0Aclient.on('disconnect', event =%3E %7B winston.warn(%60Disconnected %5B$%7Bevent.code%7D%5D: $%7Bevent.reason %7C%7C 'Unknown reason'%7D%60)%7D )%0Aclient.on('reconnecting', () =%3E %7B winston.info('Reconnecting...')%7D)%0A%0A%0Aclient
+    
 .on(
@@ -1427,23 +1427,21 @@
     
-await C
+    c
 ommunity
 .ini
@@ -1440,43 +1440,28 @@
 nity
-.init(client).catch(winston.error)%0A
+Manager.start()%0A    
     
@@ -1490,16 +1490,20 @@
 eady.')%0A
+    
     wins
@@ -1626,16 +1626,20 @@
 .%60)%0A    
+  
+  
 client.u
@@ -1665,16 +1665,20 @@
 stone')%0A
+    
 %7D)%0A%0Aclie
@@ -1696,96 +1696,45 @@
 der(
-%0A    sqlite.open('/data/settings.sqlite3').then(db =%3E new Commando.SQLiteProvider(
+new SequelizeProvider(postgresql.
 db))
-%0A)
 .cat

Update main app file to use new sequelized data