1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- #!/usr/bin/env node
- const discord = require('discord.js')
- const config = require('./config')
- const db = require('./db')
- class Game {
- // Starts the game. Should only run once per process!
- async boot() {
- // Load the config file
- await config.load()
- // Connect to the database (nedb or mongodb)
- await db.connect()
- // Create the bot pool
- this.clientPool = await Promise.all(config.get('discord_bot_tokens').map(async token => {
- const client = new discord.Client()
- try {
- await client.login(token)
- } catch (err) {
- console.warn('Bot login failure (is the token correct?)')
- return null
- }
- if (client.guilds.find(g => g.id === config.get('discord_server_id'))) {
- console.log(`Bot ${client.user.tag} logged in successfully`)
- return client
- } else {
- console.warn('Bot not connected to configured Discord server - add it using the following URL:')
- console.warn(`https://discordapp.com/oauth2/authorize?&client_id=${client.user.id}&scope=bot&permissions=${0x00000008}&response_type=code`)
- return null
- }
- }).filter(promise => promise !== null))
- if (this.clientPool.length === 0) {
- throw 'No bots connected, cannot start'
- }
- // Add all players to the database (if they don't exist already)
- // This could take a while on large servers
- await Promise.all(this.guild.members.filter(m => !m.user.bot).map(async member => {
- const player = await db.Player.findOne({discordID: member.id})
- if (!player) {
- console.log(`Creating player data for new user ${member.user.tag}`)
- await db.Player.create({discordID: member.id}).save()
- }
- }))
- // TODO: other things
- }
- get guild() {
- return this.clientPool[0].guilds.find(g => g.id === config.get('discord_server_id'))
- }
- }
- // Let's go!!
- const game = new Game()
- game.boot()
- .then(() => console.log('Game started'))
- .catch(err => {
- // :(
- console.error('Unhandled error during boot:')
- console.error(err) // Human-friendly, handled errors are just strings
- process.exit(1)
- })
|