aboutsummaryrefslogtreecommitdiff
path: root/src/commands/info/avatar.ts
blob: 3eae98c5da76e43f98b206970ac18cb6ecf577cb (plain)
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
import { Arg, BushCommand, clientSendAndPermCheck, colors, type CommandMessage, type OptArgType, type SlashMessage } from '#lib';
import { ApplicationCommandOptionType, EmbedBuilder, GuildMember, PermissionFlagsBits } from 'discord.js';

export default class AvatarCommand extends BushCommand {
	public constructor() {
		super('avatar', {
			aliases: ['avatar', 'av'],
			category: 'info',
			description: "A command to get a user's avatar",
			usage: ['avatar [user]'],
			examples: ['avatar', 'av IRONM00N'],
			args: [
				{
					id: 'user',
					description: 'The user you would like to find the avatar of.',
					type: Arg.union('member', 'globalUser'),
					readableType: 'member|user',
					prompt: 'Who would you like to see the avatar of?',
					retry: '{error} Choose a valid user.',
					optional: true,
					slashType: ApplicationCommandOptionType.User
				}
			],
			clientPermissions: (m) => clientSendAndPermCheck(m, [PermissionFlagsBits.EmbedLinks], true),
			userPermissions: [],
			slash: true
		});
	}

	public override async exec(message: CommandMessage | SlashMessage, args: { user: OptArgType<'member' | 'globalUser'> }) {
		const params: { size: 2048; extension: 'png'; dynamic: true } = { size: 2048, extension: 'png', dynamic: true };
		const defaultAvatar = `https://cdn.discordapp.com/embed/avatars/${Math.ceil(Math.random() * 6) - 1}.png`;

		const member =
			(args.user ?? message.member) instanceof GuildMember ? args.user ?? (message.member as GuildMember | null) : null;
		const user = args.user instanceof GuildMember ? args.user.user : args.user ?? message.author;

		const guildAvatar = member?.avatarURL(params);

		const embed = new EmbedBuilder().setTimestamp().setColor(colors.default).setTitle(`${user.tag}'s Avatar`);
		guildAvatar
			? embed.setImage(guildAvatar).setThumbnail(user.avatarURL(params) ?? defaultAvatar)
			: embed.setImage(user.avatarURL(params) ?? defaultAvatar);

		await message.util.reply({ embeds: [embed] });
	}
}