KevinNovak / Discord-Bot-TypeScript-Template

Discord bot - A discord.js bot template written with TypeScript.
MIT License
477 stars 86 forks source link

create sendEphemeral() function #60

Closed Kaisarion closed 1 year ago

Kaisarion commented 1 year ago

Allows the user to send a command ephemerally. Note that for this to work correctly, the command deferType must be set to public deferType = CommandDeferType.HIDDEN; at the top of command settings.

For example, if I wanted to send a hidden interaction with this utility, I'd do this in my command:

import { ChatInputCommandInteraction, PermissionsString, UserResolvable } from 'discord.js';
import { RateLimiter } from 'discord.js-rate-limiter';

import { Command, CommandDeferType } from '@/commands';
import { Language } from '@/models/enum-helpers';
import { EventData } from '@/models/internal-models';
import { Lang } from '@/services';
import { InteractionUtils } from '@/utils';

export class CreditsCommand implements Command {
  public names = [Lang.getRef('chatCommands.credits', Language.Default)];
  public cooldown = new RateLimiter(1, 5000);
  public deferType = CommandDeferType.HIDDEN; // Notice how this is HIDDEN, not the default PUBLIC.
  public requireClientPerms: PermissionsString[] = [];

  public async execute(intr: ChatInputCommandInteraction, data: EventData): Promise<void> {

    await InteractionUtils.sendEphemeral( // Note that this is not .send, but .sendEphemeral, and works just the same but hidden.
      intr,
      await Lang.getEmbed(intr.guild.id, 'displayEmbeds.credits', data.lang)
    );
  }
}