aboutsummaryrefslogtreecommitdiff
path: root/src/commands.js
blob: 59c6506985f9fef2c98f13dee2a4cec8046bf995 (plain) (blame)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const { CommandManager, defaultParameterTypes, TypeConversionError } = require('knub-command-manager');
const config = require('./config');
const utils = require('./utils');
const threads = require('./data/threads');

module.exports = {
  createCommandManager(bot) {
    const manager = new CommandManager({
      prefix: config.prefix,
      types: Object.assign({}, defaultParameterTypes, {
        userId(value) {
          const userId = utils.getUserMention(value);
          if (! userId) throw new TypeConversionError();
          return userId;
        },

        delay(value) {
          const ms = utils.convertDelayStringToMS(value);
          if (ms === null) throw new TypeConversionError();
          return ms;
        }
      })
    });

    const handlers = {};
    const aliasMap = new Map();

    bot.on('messageCreate', async msg => {
      if (msg.author.bot) return;
      if (msg.author.id === bot.user.id) return;
      if (! msg.content) return;

      const matchedCommand = await manager.findMatchingCommand(msg.content, { msg });
      if (matchedCommand === null) return;
      if (matchedCommand.error !== undefined) {
        utils.postError(msg.channel, matchedCommand.error);
        return;
      }

      const allArgs = {};
      for (const [name, arg] of Object.entries(matchedCommand.args)) {
        allArgs[name] = arg.value;
      }
      for (const [name, opt] of Object.entries(matchedCommand.opts)) {
        allArgs[name] = opt.value;
      }

      handlers[matchedCommand.id](msg, allArgs);
    });

    /**
     * Add a command that can be invoked anywhere
     */
    const addGlobalCommand = (trigger, parameters, handler, commandConfig = {}) => {
      let aliases = aliasMap.has(trigger) ? [...aliasMap.get(trigger)] : [];
      if (commandConfig.aliases) aliases.push(...commandConfig.aliases);

      const cmd = manager.add(trigger, parameters, { ...commandConfig, aliases });
      handlers[cmd.id] = handler;
    };

    /**
     * Add a command that can only be invoked on the inbox server
     */
    const addInboxServerCommand = (trigger, parameters, handler, commandConfig = {}) => {
      const aliases = aliasMap.has(trigger) ? [...aliasMap.get(trigger)] : [];
      if (commandConfig.aliases) aliases.push(...commandConfig.aliases);

      const cmd = manager.add(trigger, parameters, {
        ...commandConfig,
        aliases,
        preFilters: [
          (_, context) => {
            if (! utils.messageIsOnInboxServer(context.msg)) return false;
            if (! utils.isStaff(context.msg.member)) return false;
            return true;
          }
        ]
      });

      handlers[cmd.id] = async (msg, args) => {
        const thread = await threads.findOpenThreadByChannelId(msg.channel.id);
        handler(msg, args, thread);
      };
    };

    /**
     * Add a command that can only be invoked in a thread on the inbox server
     */
    const addInboxThreadCommand = (trigger, parameters, handler, commandConfig = {}) => {
      const aliases = aliasMap.has(trigger) ? [...aliasMap.get(trigger)] : [];
      if (commandConfig.aliases) aliases.push(...commandConfig.aliases);

      let thread;
      const cmd = manager.add(trigger, parameters, {
        ...commandConfig,
        aliases,
        preFilters: [
          async (_, context) => {
            if (! utils.messageIsOnInboxServer(context.msg)) return false;
            if (! utils.isStaff(context.msg.member)) return false;
            thread = await threads.findOpenThreadByChannelId(context.msg.channel.id);
            if (! thread) return false;
            return true;
          }
        ]
      });

      handlers[cmd.id] = async (msg, args) => {
        handler(msg, args, thread);
      };
    };

    const addAlias = (originalCmd, alias) => {
      if (! aliasMap.has(originalCmd)) {
        aliasMap.set(originalCmd, new Set());
      }

      aliasMap.get(originalCmd).add(alias);
    };

    return {
      manager,
      addGlobalCommand,
      addInboxServerCommand,
      addInboxThreadCommand,
      addAlias,
    };
  }
};