All files / discord/src/utils interaction.ts

0% Statements 0/10
0% Branches 0/8
0% Functions 0/3
0% Lines 0/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                       
import { FlarieContext, FlarieDMContext, FlarieInteraction, FlarieServerContext } from '@flarie/core';
import { CacheType, Interaction } from 'discord.js';
import { toDiscordReplyPayload } from './payloads';
 
export function getContext(interaction: Interaction<CacheType>): FlarieContext {
  if (interaction.guild && interaction.channel) {
    return new FlarieServerContext({
      server: {
        id: interaction.guild.id,
        name: interaction.guild.name,
      },
      channel: {
        id: interaction.channel.id,
        // @ts-expect-error this needs to be resolved at some point
        name: interaction.channel.name,
      },
      user: {
        id: interaction.user.id,
        username: interaction.user.username,
        displayName: interaction.user.discriminator,
        bot: interaction.user.bot,
      },
    });
  }
 
  if (interaction.channelId) {
    return new FlarieDMContext({
      channel: {
        id: interaction.channelId,
        name: interaction.user.username,
      },
      user: {
        id: interaction.user.id,
        username: interaction.user.username,
        displayName: interaction.user.discriminator,
        bot: interaction.user.bot,
      },
    });
  }
 
  throw new Error('Unknown interaction context.');
}
 
export function toFlarieInteraction(interaction: Interaction<CacheType>): FlarieInteraction {
  return {
    async reply(message) {
      if (!interaction.isRepliable()) {
        throw new Error('Interaction is not repliable');
      }
 
      await interaction.reply(toDiscordReplyPayload(message));
 
      this.replied = true;
    },
 
    replied: false,
    context: getContext(interaction),
  };
}