aboutsummaryrefslogtreecommitdiff
path: root/src/bot.ts
blob: 2b9f07db61aab88d3e97aea95114ea7be19d5854 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import makeDir from 'make-dir';
import {Client, Message, Collection} from 'discord.js';
import {inject, injectable} from 'inversify';
import {TYPES} from './types';
import {Settings} from './models';
import {sequelize} from './utils/db';
import handleGuildCreate from './events/guild-create';
import container from './inversify.config';
import Command from './commands';

@injectable()
export default class {
  private readonly client: Client;
  private readonly token: string;
  private readonly clientId: string;
  private readonly dataDir: string;
  private readonly cacheDir: string;
  private readonly commands!: Collection<string, Command>;

  constructor(@inject(TYPES.Client) client: Client, @inject(TYPES.Config.DISCORD_TOKEN) token: string, @inject(TYPES.Config.DISCORD_CLIENT_ID) clientId: string, @inject(TYPES.Config.DATA_DIR) dataDir: string, @inject(TYPES.Config.CACHE_DIR) cacheDir: string) {
    this.client = client;
    this.token = token;
    this.clientId = clientId;
    this.dataDir = dataDir;
    this.cacheDir = cacheDir;
    this.commands = new Collection();
  }

  public async listen(): Promise<string> {
    // Load in commands
    container.getAll<Command>(TYPES.Command).forEach(command => {
      this.commands.set(command.name, command);
    });

    this.client.on('message', async (msg: Message) => {
      // Get guild settings
      if (!msg.guild) {
        return;
      }

      const settings = await Settings.findByPk(msg.guild.id);

      if (!settings) {
        // Got into a bad state, send owner welcome message
        return this.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 (!this.commands.has(command)) {
        return;
      }

      try {
        const handler = this.commands.get(command);

        handler!.execute(msg, args);
      } catch (error) {
        console.error(error);
        msg.reply('there was an error trying to execute that command!');
      }
    });

    this.client.on('ready', async () => {
      // Create directory if necessary
      await makeDir(this.dataDir);
      await makeDir(this.cacheDir);

      await sequelize.sync({});

      console.log(`Ready! Invite the bot with https://discordapp.com/oauth2/authorize?client_id=${this.clientId}&scope=bot`);
    });

    // Register event handlers
    this.client.on('guildCreate', handleGuildCreate);

    return this.client.login(this.token);
  }
}