/ src / modules / commands / toggleAutoRole.ts
toggleAutoRole.ts
 1  import { prepare, getLang, getUserConfig } from './../db';
 2  import { Command } from '../commandHandler';
 3  
 4  import { __, __h_dc } from '../messages';
 5  import { Colors, EmbedBuilder, SlashCommandBuilder } from 'discord.js';
 6  export default {
 7    data: new SlashCommandBuilder()
 8      .setName('toggleautorole')
 9      .setDescription('Enable/Disable automatic role assignment')
10      .setDescriptionLocalizations(__h_dc('Enable/Disable automatic role assignment'))
11      .addBooleanOption(option =>
12        option
13          .setName('enabled')
14          .setDescription('Enable/Disable automatic role assignment')
15          .setDescriptionLocalizations(__h_dc('Enable/Disable automatic role assignment'))
16          .setRequired(false)
17      ),
18  
19    execute: async interaction => {
20      const locale = getLang(interaction);
21  
22      const autoRole = interaction.options.get('enabled', false)?.value as boolean | undefined;
23      const userAutoRole = getUserConfig(interaction.user.id).autoRole;
24      if (autoRole === undefined) {
25        interaction.reply({
26          embeds: [
27            new EmbedBuilder()
28              .setTitle(__({ phrase: 'User Status', locale }))
29              .setDescription(
30                __(
31                  {
32                    phrase:
33                      'The bot is currently **%s** for this user.\n\nYou can change this with the command `/toggleAutoRole`.',
34                    locale
35                  },
36                  userAutoRole
37                    ? __({ phrase: 'enabled', locale })
38                    : __({ phrase: 'disabled', locale })
39                )
40              )
41              .setColor(userAutoRole ? Colors.Green : Colors.Red)
42          ]
43        });
44      } else {
45        prepare('UPDATE users SET autoRole = ? WHERE userIDHash = ?').run(
46          Number(autoRole),
47          interaction.user.id
48        );
49        interaction.reply({
50          embeds: [
51            new EmbedBuilder()
52              .setTitle(
53                __(
54                  { phrase: 'Automatic role assignment for your user is now **%s**.', locale },
55                  autoRole ? __({ phrase: 'enabled', locale }) : __({ phrase: 'disabled', locale })
56                )
57              )
58              .setDescription(
59                __({ phrase: 'You can change this with the command `/toggleAutoRole`.', locale })
60              )
61              .setColor(autoRole ? Colors.Green : Colors.Red)
62          ]
63        });
64      }
65    }
66  } as Command;