File size: 2,211 Bytes
b39afbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Copyright (c) 2023 MERCENARIES.AI PTE. LTD.
 * All rights reserved.
 */

const script = {
    name: 'settings',

    permission: async function (ctx, ability, payload) {
      const auth = ctx.app.integrations.get('auth')
      const isAdmin = await auth.isAdmin(ctx.user)
      if (!isAdmin) {
        await ctx.app.sendMessageToSession(ctx.sessionId, 'Admin permission needed to change server settings', 'text/plain')
        throw new Error('Admin permission needed to change server settings')
      }
    },
  
    exec: async function (ctx, payload) {
      const [command, ...args] = payload
      ctx.integration.debug('script', command, args)
    
      if (command === 'list') {
        const settings = ctx.app.settings.getAll()
        await ctx.app.sendMessageToSession(
            ctx.sessionId, 
            'Server settings:\n\n' + settings.map((setting) => '- ' + setting.key + ': ' + setting.value).join('\n'), 
            'text/plain'
        )
      } else if (command === 'set') {
        const [key, value] = args
        if (!key || !value) {
            await ctx.app.sendMessageToSession(
                ctx.sessionId, 
                'Usage: /settings set <key> <value>', 
                'text/plain'
            )
            return true
        }
        
        await ctx.app.settings.update(key, value)
        await ctx.app.sendMessageToSession(
            ctx.sessionId, 
            'Setting ' + key + ' set to ' + ctx.app.settings.get(key)?.value + '\n Some changes needs a server restart to take effect.', 
            'text/plain'
        )
      } else if (command === 'reset') {
        const [key] = args
        if (!key) {
            await ctx.app.sendMessageToSession(
                ctx.sessionId, 
                'Usage: /settings reset <key>', 
                'text/plain'
            )
            return true
        }
        await ctx.app.settings.reset(key)
        await ctx.app.sendMessageToSession(
            ctx.sessionId, 
            'Setting ' + key + ' reset\n You may need to restart the server for the change to take effect.', 
            'text/plain'
        )
      }

      return true  
    }

  }
  
  export default script