Discord-InterChat/InterChat

View on GitHub
src/interactions/InactiveConnect.ts

Summary

Maintainability
A
0 mins
Test Coverage
import { emojis } from '#utils/Constants.js';
import { RegisterInteractionHandler } from '#main/decorators/RegisterInteractionHandler.js';
import { fetchConnection, updateConnection } from '#utils/ConnectedListUtils.js';
import { CustomID } from '#utils/CustomID.js';
import { InfoEmbed } from '#utils/EmbedUtils.js';
import { t } from '#utils/Locale.js';
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, type ButtonInteraction } from 'discord.js';

type extraOpts = {
  disconnectEmoji?: string;
  connectEmoji?: string;
  userId?: string;
  /** set custom prefix for customId and handle it urself, eg: `epik_reconnect`  */
  customCustomId?: string;
};

/**
 * @param channelId The channel ID of the connection.
 */
export const buildConnectionButtons = (
  connected: boolean | undefined,
  channelId: string,
  opts: extraOpts = {},
) => {
  if (!opts?.disconnectEmoji || !opts.connectEmoji) {
    opts.disconnectEmoji = emojis.disconnect;
    opts.connectEmoji = emojis.connect;
  }

  return new ActionRowBuilder<ButtonBuilder>().addComponents([
    new ButtonBuilder()
      .setCustomId(
        new CustomID()
          .setIdentifier(opts.customCustomId ?? 'connection', 'toggle')
          .addArgs(channelId)
          .addArgs(opts?.userId ?? '')
          .toString(),
      )
      .setLabel(connected ? 'Disconnect' : 'Reconnect')
      .setStyle(connected ? ButtonStyle.Danger : ButtonStyle.Success)
      .setEmoji(connected ? opts.disconnectEmoji : opts.connectEmoji),
  ]);
};

export default class InactiveConnectInteraction {
  @RegisterInteractionHandler('inactiveConnect', 'toggle')
  async inactiveConnect(interaction: ButtonInteraction): Promise<void> {
    await interaction.deferUpdate();

    const customId = CustomID.parseCustomId(interaction.customId);
    const [channelId] = customId.args;

    const connection = await fetchConnection(channelId);
    if (!connection) {
      const locale = await interaction.client.userManager.getUserLocale(interaction.user.id);
      const notFoundEmbed = new InfoEmbed().setDescription(
        t('connection.channelNotFound', locale, { emoji: emojis.no }),
      );

      await interaction.followUp({ embeds: [notFoundEmbed], ephemeral: true });
      return;
    }

    await updateConnection({ channelId }, { connected: true });

    const embed = new InfoEmbed()
      .removeTitle()
      .setDescription(
        `### ${emojis.tick} Connection Resumed\nConnection has been resumed. Have fun chatting!`,
      );

    await interaction.editReply({ embeds: [embed], components: [] });
  }
}