aboutsummaryrefslogtreecommitdiff
path: root/src/commands/moulberry-bush/neuRepo.ts
blob: 4baae7dba612e6fb89a9af6f1516cfae33606ac5 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { BushCommand, clientSendAndPermCheck, type ArgType, type CommandMessage, type SlashMessage } from '#lib';
import canvas from '@napi-rs/canvas';
import {
	ApplicationCommandOptionType,
	AttachmentBuilder,
	AutocompleteInteraction,
	CacheType,
	PermissionFlagsBits
} from 'discord.js';
import { join } from 'path';
import tinycolor from 'tinycolor2';
import { formattingInfo, RawNeuItem } from '../../../lib/utils/Minecraft.js';

export default class NeuRepoCommand extends BushCommand {
	public static items: { name: string; id: string }[] = [];

	public constructor() {
		super('neuRepo', {
			aliases: ['neu-repo', 'repo-item', 'neu-item', 'item-repo'],
			category: "Moulberry's Bush",
			description: 'Get information about an item from the NEU item repo.',
			usage: ['neu-repo <item>'],
			examples: ['neu-repo BARRIER'],
			args: [
				{
					id: 'item',
					description: 'The item id that you would like to find neu item information about.',
					type: 'string',
					prompt: 'What SkyBlock item would you like to get information about?',
					retry: '{error} Pick a valid SkyBlock item ID. Try using the slash command for a better experience.',
					slashType: ApplicationCommandOptionType.String,
					autocomplete: true
				}
				/* {
					id: 'dangerous',
					description: 'Whether or not to use the dangerous branch.',
					prompt: 'Would you like to use the dangerous branch instead of the master branch?',
					match: 'flag',
					flag: ['--dangerous', '-d'],
					default: false,
					optional: true,
					slashType: ApplicationCommandOptionType.Boolean
				} */
			],
			slash: false,
			clientPermissions: (m) => clientSendAndPermCheck(m, [PermissionFlagsBits.EmbedLinks], true),
			userPermissions: [],
			ownerOnly: true,
			hidden: true
		});
	}

	public override async exec(
		message: CommandMessage | SlashMessage,
		args: { item: ArgType<'string'> /* dangerous: ArgType<'flag'> */ }
	) {
		const itemPath = join(import.meta.url, '..', '..', '..', '..', '..', 'neu-item-repo-dangerous', 'items', `${args.item}.json`);
		const item = (await import(itemPath, { assert: { type: 'json' } })).default as RawNeuItem;

		const toolTip = this.toolTip(item);

		return message.util.reply({
			files: [new AttachmentBuilder(toolTip, { name: `${item.internalname}.png`, description: item.displayname })]
		});
	}

	public toolTip(item: RawNeuItem): Buffer {
		const background = '#100010';

		const width = 250;
		const height = 250;
		const scale = 10;

		const itemRender = canvas.createCanvas(width, height),
			ctx = itemRender.getContext('2d');

		ctx.globalAlpha = 0.94;
		ctx.fillStyle = background;

		// top outside
		ctx.fillRect(scale, 0, width - 2 * scale, scale);

		// bottom outside
		ctx.fillRect(scale, height - scale, width - 2 * scale, scale);

		// left outside
		ctx.fillRect(0, scale, scale, height - 2 * scale);

		// right outside
		ctx.fillRect(width - scale, scale, scale, height - 2 * scale);

		// middle
		ctx.fillRect(2 * scale, 2 * scale, width - 4 * scale, height - 4 * scale);

		ctx.globalAlpha = 0.78;

		const borderColorStart = parseInt(new tinycolor(this.getPrimaryColour(item.displayname)).toHex(), 16);
		const borderColorEnd = ((borderColorStart & 0xfefefe) >> 1) | (borderColorStart & 0xff000000);

		const borderColorStartStr = `#${borderColorStart.toString(16)}`;
		const borderColorEndStr = `#${borderColorEnd.toString(16)}`;

		ctx.fillStyle = borderColorStartStr;

		// top highlight
		ctx.fillRect(scale, scale, width - 2 * scale, scale);

		// left highlight
		ctx.fillRect(scale, 2 * scale, scale, height - 3 * scale);

		// bottom highlight
		{
			const x = 2 * scale,
				y = height - 2 * scale,
				w = width - 3 * scale,
				h = scale;
			const gradient = ctx.createLinearGradient(x, y, x + w, y + h);
			gradient.addColorStop(0, borderColorStartStr);
			gradient.addColorStop(1, borderColorEndStr);
			ctx.fillStyle = gradient;

			ctx.fillRect(x, y, w, h);
		}

		// right highlight
		{
			const x = width - 2 * scale,
				y = 2 * scale,
				w = scale,
				h = height - 4 * scale;
			const gradient = ctx.createLinearGradient(x, y, x + w, y + h);
			gradient.addColorStop(0, borderColorStartStr);
			gradient.addColorStop(1, borderColorEndStr);
			ctx.fillStyle = gradient;

			ctx.fillRect(x, y, w, h);
		}

		item.displayname.split('');

		return itemRender.toBuffer('image/png');
	}

	// stolen from NEU and modified
	public getPrimaryColourCode(displayname: string): code {
		let lastColourCode = -99;
		let currentColour = 0;
		const mostCommon = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
		for (let i = 0; i < displayname.length; i++) {
			const c = displayname.charAt(i);
			if (c === '\u00A7') {
				lastColourCode = i;
			} else if (lastColourCode === i - 1) {
				const colIndex = '0123456789abcdef'.indexOf(c);
				if (colIndex >= 0) {
					currentColour = colIndex;
				} else {
					currentColour = 0;
				}
			} else if ('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.indexOf(c) >= 0) {
				if (currentColour > 0) {
					mostCommon[currentColour] = mostCommon[currentColour]++;
				}
			}
		}
		let mostCommonCount = 0;
		for (let index = 0; index < mostCommon.length; index++) {
			if (mostCommon[index] > mostCommonCount) {
				mostCommonCount = mostCommon[index];
				currentColour = index;
			}
		}

		return <code>'0123456789abcdef'.charAt(currentColour);
	}

	// stolen from NEU and modified
	public getPrimaryColour(displayname: string) {
		const code = this.getPrimaryColourCode(displayname);
		return formattingInfo[${code}`].foregroundDarker;
	}

	public override async autocomplete(interaction: AutocompleteInteraction<CacheType>) {
		return interaction.respond([{ name: 'Blazetekk™ Ham Radio', value: 'BLAZETEKK_HAM_RADIO' }]);
	}
}

type code = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | 'a' | 'b' | 'c' | 'd' | 'e' | 'f';