aboutsummaryrefslogtreecommitdiff
path: root/src/commands/info/color.ts
blob: 0e1be8166b38131c0c551cae01bdf2ba9f828ed3 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import {
	AllowedMentions,
	BushCommand,
	type ArgType,
	type BushArgumentTypeCaster,
	type BushGuildMember,
	type BushMessage,
	type BushRole,
	type BushSlashMessage
} from '#lib';
import assert from 'assert';
import { ApplicationCommandOptionType, Embed, Permissions, Role } from 'discord.js';
import tinycolor from 'tinycolor2';
assert(tinycolor);

const isValidTinyColor: BushArgumentTypeCaster<string | null> = (_message, phase) => {
	// if the phase is a number it converts it to hex incase it could be representing a color in decimal
	const newPhase = isNaN(phase as any) ? phase : `#${Number(phase).toString(16)}`;
	return tinycolor(newPhase).isValid() ? newPhase : null;
};

export default class ColorCommand extends BushCommand {
	public constructor() {
		super('color', {
			aliases: ['color'],
			category: 'info',
			description: 'Find the color of a hex code, user, or role.',
			usage: ['color <color|role|user>'],
			examples: ['color #0000FF'],
			args: [
				{
					id: 'color',
					description: 'The color string, role, or member to find the color of.',
					type: util.arg.union(isValidTinyColor as any, 'role', 'member'),
					readableType: 'color|role|member',
					match: 'restContent',
					prompt: 'What color code, role, or user would you like to find the color of?',
					retry: '{error} Choose a valid color, role, or member.',
					slashType: ApplicationCommandOptionType.String
				}
			],
			channel: 'guild',
			clientPermissions: (m) => util.clientSendAndPermCheck(m, [Permissions.FLAGS.EMBED_LINKS], true),
			userPermissions: []
		});
	}

	public removePrefixAndParenthesis(color: string): string {
		return color.substring(4, color.length - 5);
	}

	public override async exec(
		message: BushMessage | BushSlashMessage,
		args: { color: string | ArgType<'role'> | ArgType<'member'> }
	) {
		const _color = message.util.isSlashMessage(message)
			? ((await util.arg.cast(util.arg.union(isValidTinyColor, 'role', 'member'), message, args.color as string)) as
					| string
					| BushRole
					| BushGuildMember)
			: args.color;

		const color =
			typeof _color === 'string'
				? tinycolor(_color)
				: _color instanceof Role
				? tinycolor(_color.hexColor)
				: tinycolor(_color.displayHexColor);

		if (_color instanceof Role && _color.hexColor === '#000000') {
			return await message.util.reply({
				content: `${util.emojis.error} <@&${_color.id}> does not have a color.`,
				allowedMentions: AllowedMentions.none()
			});
		}

		const embed = new Embed()
			.addField({ name: '» Hexadecimal', value: color.toHexString() })
			.addField({ name: '» Decimal', value: `${parseInt(color.toHex(), 16)}` })
			.addField({ name: '» HSL', value: this.removePrefixAndParenthesis(color.toHslString()) })
			.addField({ name: '» RGB', value: this.removePrefixAndParenthesis(color.toRgbString()) })
			.setColor(parseInt(color.toHex(), 16));

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