aboutsummaryrefslogtreecommitdiff
path: root/src/commands/help.ts
blob: 90c5d986acb787a83b45c6712e638981bce81f4d (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
import {Message} from 'discord.js';
import {injectable} from 'inversify';
import Command from '.';
import {TYPES} from '../types';
import {Settings} from '../models';
import container from '../inversify.config';

@injectable()
export default class implements Command {
  public name = 'help';
  public aliases = ['h'];
  public examples = [
    ['help', 'you don\'t need a description']
  ];

  private commands: Command[] = [];

  public async execute(msg: Message, _: string []): Promise<void> {
    if (this.commands.length === 0) {
      // Lazy load to avoid circular dependencies
      this.commands = container.getAll<Command>(TYPES.Command);
    }

    const settings = await Settings.findOne({where: {guildId: msg.guild!.id}});

    if (!settings) {
      return;
    }

    const {prefix} = settings;

    const res = this.commands.sort((a, b) => a.name.localeCompare(b.name)).reduce((content, command) => {
      const aliases = command.aliases.reduce((str, alias, i) => {
        str += alias;

        if (i !== command.aliases.length - 1) {
          str += ', ';
        }

        return str;
      }, '');

      if (aliases === '') {
        content += `**${command.name}**:\n`;
      } else {
        content += `**${command.name}** (${aliases}):\n`;
      }

      command.examples.forEach(example => {
        content += `- \`${prefix}${example[0]}\`: ${example[1]}\n`;
      });

      content += '\n';

      return content;
    }, '');

    await msg.author.send(res);
    await msg.react('🇩');
    await msg.react('🇲');
  }
}