diff options
| author | Max Isom <[email protected]> | 2020-03-12 22:41:26 -0500 |
|---|---|---|
| committer | Max Isom <[email protected]> | 2020-03-12 22:41:26 -0500 |
| commit | 17ba78f7b7d78c638ab00b9d4af79110130b0bcd (patch) | |
| tree | df0671a4b2845333198b57906b5dde68b709d37a /src/index.ts | |
| parent | 8eb4c8a6c06f672cb50efae5ea30215d465000af (diff) | |
| download | muse-17ba78f7b7d78c638ab00b9d4af79110130b0bcd.tar.xz muse-17ba78f7b7d78c638ab00b9d4af79110130b0bcd.zip | |
Use IoC, impliment queue
Diffstat (limited to 'src/index.ts')
| -rw-r--r-- | src/index.ts | 75 |
1 files changed, 11 insertions, 64 deletions
diff --git a/src/index.ts b/src/index.ts index b4c760f..c4ce13e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,68 +1,15 @@ -import fs from 'fs'; -import path from 'path'; -import makeDir from 'make-dir'; -import Discord from 'discord.js'; -import {DISCORD_TOKEN, DISCORD_CLIENT_ID, DATA_DIR, CACHE_DIR} from './utils/config'; -import {Settings} from './models'; -import {sequelize} from './utils/db'; -import {CommandHandler} from './interfaces'; -import handleGuildCreate from './events/guild-create'; +import container from './inversify.config'; +import Spotify from 'spotify-web-api-node'; +import {TYPES} from './types'; +import Bot from './bot'; -const client = new Discord.Client(); -const commands = new Discord.Collection(); +let bot = container.get<Bot>(TYPES.Bot); +const spotify = container.get<Spotify>(TYPES.Lib.Spotify); -// Load in commands -const commandFiles = fs.readdirSync(path.join(__dirname, 'commands')).filter(file => file.endsWith('.js')); +(async () => { + const auth = await spotify.clientCredentialsGrant(); -for (const file of commandFiles) { - const command = require(`./commands/${file}`).default; + spotify.setAccessToken(auth.body.access_token); - commands.set(command.name, command); -} - -// Generic message handler -client.on('message', async (msg: Discord.Message) => { - // Get guild settings - const settings = await Settings.findByPk(msg.guild!.id); - - if (!settings) { - // Got into a bad state, send owner welcome message - return client.emit('guildCreate', msg.guild); - } - - const {prefix, channel} = settings; - - if (!msg.content.startsWith(prefix) || msg.author.bot || msg.channel.id !== channel) { - return; - } - - const args = msg.content.slice(prefix.length).split(/ +/); - const command = args.shift()!.toLowerCase(); - - if (!commands.has(command)) { - return; - } - - try { - const handler = commands.get(command) as CommandHandler; - - handler.execute(msg, args); - } catch (error) { - console.error(error); - msg.reply('there was an error trying to execute that command!'); - } -}); - -client.on('ready', async () => { - // Create directory if necessary - await makeDir(DATA_DIR); - await makeDir(CACHE_DIR); - - await sequelize.sync({}); - - console.log(`Ready! Invite the bot with https://discordapp.com/oauth2/authorize?client_id=${DISCORD_CLIENT_ID}&scope=bot`); -}); - -client.on('guildCreate', handleGuildCreate); - -client.login(DISCORD_TOKEN); + bot.listen(); +})(); |
