aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/commands/admin/reload.js
blob: 206e2cab79758949a5911c97dc7c036e33d6c55e (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
/*
  Description: Clears and resets the command modules, outputting any errors
*/

// module main
export async function run(core, server, socket, data) {
  // increase rate limit chance and ignore if not admin
  if (socket.uType !== 'admin') {
    return server.police.frisk(socket.address, 20);
  }

  // do command reload and store results
  let loadResult = core.dynamicImports.reloadDirCache();
  loadResult += core.commands.loadCommands();

  // clear and rebuild all module hooks
  server.loadHooks();

  // build reply based on reload results
  if (loadResult === '') {
    loadResult = `Reloaded ${core.commands.commands.length} commands, 0 errors`;
  } else {
    loadResult = `Reloaded ${core.commands.commands.length} commands, error(s):
      ${loadResult}`;
  }

  if (typeof data.reason !== 'undefined') {
    loadResult += `\nReason: ${data.reason}`;
  }

  // reply with results
  server.reply({
    cmd: 'info',
    text: loadResult,
  }, socket);

  // notify mods of reload #transparency
  server.broadcast({
    cmd: 'info',
    text: loadResult,
  }, { uType: 'mod' });

  return true;
}

export const info = {
  name: 'reload',
  description: '(Re)loads any new commands into memory, outputs errors if any',
  usage: `
    API: { cmd: 'reload', reason: '<optional reason append>' }`,
};