/ src / modules / commands / setStatusRole.ts
setStatusRole.ts
  1  import { DBStatusRole, prepare, getLang } from './../db';
  2  import { Command } from '../commandHandler';
  3  
  4  import { __, __h_dc, getEnumKey } from '../messages';
  5  import { ActivityType, Colors, EmbedBuilder, SlashCommandBuilder } from 'discord.js';
  6  export default {
  7    data: new SlashCommandBuilder()
  8      .setName('setstatusrole')
  9      .setDescription('role to assign on LISTENING/WATCHING/etc.')
 10      .setDescriptionLocalizations(__h_dc('role to assign on LISTENING/WATCHING/etc.'))
 11      .addStringOption(option =>
 12        option
 13          .setName('event')
 14          .setDescription('the event the user must have to receive the role')
 15          .setDescriptionLocalizations(__h_dc('the event the user must have to receive the role'))
 16          .setChoices(
 17            { name: 'Playing', name_localizations: __h_dc('Playing'), value: '0' }, // value 0
 18            { name: 'Streaming', name_localizations: __h_dc('Streaming'), value: '1' }, // value 1
 19            { name: 'Listening', name_localizations: __h_dc('Listening'), value: '2' }, // value 2
 20            { name: 'Watching', name_localizations: __h_dc('Watching'), value: '3' }, // value 3
 21            { name: 'Custom', name_localizations: __h_dc('Custom'), value: '4' }, // value 4
 22            { name: 'Competing', name_localizations: __h_dc('Competing'), value: '5' } // value 5
 23          )
 24          .setRequired(true)
 25      )
 26      .addRoleOption(option =>
 27        option
 28          .setName('role')
 29          .setDescription('the role to assign. To remove the role, omit this option')
 30          .setDescriptionLocalizations(
 31            __h_dc('the role to assign. To remove the role, omit this option')
 32          )
 33      ),
 34  
 35    execute: async interaction => {
 36      const locale = getLang(interaction);
 37      const type = Number(interaction.options.get('event')?.value) as number;
 38      const typeString = getEnumKey(ActivityType, type);
 39      const role = interaction.options.get('role')?.role;
 40      const currentStatusRole =
 41        prepare('SELECT * FROM statusRoles WHERE guildID = ? and type = ?')
 42          .get(interaction.guildId, type) as DBStatusRole | undefined;
 43  
 44      if (role) {
 45        if (currentStatusRole && role.id === currentStatusRole.roleID) {
 46          interaction.reply({
 47            embeds: [
 48              new EmbedBuilder()
 49                .setDescription(
 50                  __(
 51                    {
 52                      phrase: 'the status role for **%s** already is <@&%s>!',
 53                      locale
 54                    },
 55                    typeString,
 56                    role.id
 57                  )
 58                )
 59                .setColor(Colors.Green)
 60            ],
 61            ephemeral: true
 62          });
 63        } else {
 64          if (currentStatusRole) {
 65            prepare('UPDATE statusRoles SET roleID = ? WHERE guildID = ? AND type = ? ').run(
 66              role.id,
 67              interaction.guildId,
 68              type
 69            );
 70          } else {
 71            prepare('INSERT INTO statusRoles (guildID, type, roleID) VALUES (?, ?, ?)').run(
 72              interaction.guildId,
 73              type,
 74              role.id
 75            );
 76          }
 77          interaction.reply({
 78            embeds: [
 79              new EmbedBuilder()
 80                .setDescription(
 81                  __(
 82                    {
 83                      phrase: 'The status role for **%s** is now <@&%s>!',
 84                      locale
 85                    },
 86                    typeString,
 87                    role.id
 88                  )
 89                )
 90                .setColor(Colors.Green)
 91            ],
 92            ephemeral: true
 93          });
 94        }
 95      } else {
 96        if (currentStatusRole) {
 97          prepare('DELETE FROM statusRoles WHERE guildID = ? AND type = ?').run(
 98            interaction.guildId,
 99            type
100          );
101          interaction.reply({
102            embeds: [
103              new EmbedBuilder()
104                .setDescription(
105                  __({ phrase: 'The status role for **%s** has been deleted.', locale }, typeString)
106                )
107                .setColor(Colors.Red)
108            ],
109            ephemeral: true
110          });
111        } else {
112          interaction.reply({
113            embeds: [
114              new EmbedBuilder()
115                .setDescription(
116                  __({ phrase: 'There is no status role set for **%s**', locale }, typeString)
117                )
118                .setColor(Colors.Red)
119            ],
120            ephemeral: true
121          });
122        }
123      }
124    }
125  } as Command;