diff options
| author | Max Isom <[email protected]> | 2020-03-09 11:57:39 -0500 |
|---|---|---|
| committer | Max Isom <[email protected]> | 2020-03-09 11:57:39 -0500 |
| commit | eca84c8b6964af29948510a02ebfeb5f23244921 (patch) | |
| tree | ffa8a25a19ea9e57b33db661c28a80104a0087e7 /src/index.ts | |
| parent | afadcb9ee5482c0a1c52b3d55e948e2a8a9ac0cb (diff) | |
| download | muse-eca84c8b6964af29948510a02ebfeb5f23244921.tar.xz muse-eca84c8b6964af29948510a02ebfeb5f23244921.zip | |
Inital commit
Diffstat (limited to 'src/index.ts')
| -rw-r--r-- | src/index.ts | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..5771988 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,58 @@ +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} from './utils/config'; +import {sequelize} from './utils/db'; +import {CommandHandler} from './interfaces'; +import handleGuildCreate from './events/guild-create'; + +const PREFIX = '!'; + +const client = new Discord.Client(); +const commands = new Discord.Collection(); + +// Load in commands +const commandFiles = fs.readdirSync(path.join(__dirname, 'commands')).filter(file => file.endsWith('.js')); + +for (const file of commandFiles) { + const command = require(`./commands/${file}`).default; + + commands.set(command.name, command); +} + +// Generic message handler +client.on('message', (msg: Discord.Message) => { + if (!msg.content.startsWith(PREFIX) || msg.author.bot) { + 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 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); |
