aboutsummaryrefslogtreecommitdiff
path: root/src/utils/update-permissions-for-guild.ts
blob: 64110a76607077578b30eb2f12347c0ca3681227 (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
import {ApplicationCommandPermissionData, Guild} from 'discord.js';
import {prisma} from './db.js';

const COMMANDS_TO_LIMIT_TO_GUILD_OWNER = ['config'];

const updatePermissionsForGuild = async (guild: Guild) => {
  const settings = await prisma.setting.findUnique({
    where: {
      guildId: guild.id,
    },
  });

  if (!settings) {
    throw new Error('could not find settings for guild');
  }

  const permissions: ApplicationCommandPermissionData[] = [
    {
      id: guild.ownerId,
      type: 'USER',
      permission: true,
    },
    {
      id: guild.roles.everyone.id,
      type: 'ROLE',
      permission: false,
    },
  ];

  if (settings.invitedByUserId) {
    permissions.push({
      id: settings.invitedByUserId,
      type: 'USER',
      permission: true,
    });
  }

  const commands = await guild.commands.fetch();

  await guild.commands.permissions.set({fullPermissions: commands.map(command => ({
    id: command.id,
    permissions: COMMANDS_TO_LIMIT_TO_GUILD_OWNER.includes(command.name) ? permissions : [
      ...permissions,
      ...(settings.roleId ? [{
        id: settings.roleId,
        type: 'ROLE' as const,
        permission: true,
      }] : []),
    ],
  }))});
};

export default updatePermissionsForGuild;