aboutsummaryrefslogtreecommitdiff
path: root/src/commands/utilities/decode.ts
blob: a5a4c21437bcda74907bd2ba69c6c55e494422a4 (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
import { AllowedMentions, BushCommand, BushMessage } from '@lib';
import { AkairoMessage } from 'discord-akairo';
import { MessageEmbed } from 'discord.js';

const encodingTypesArray = ['ascii', 'utf8', 'utf-8', 'utf16le', 'ucs2', 'ucs-2', 'base64', 'latin1', 'binary', 'hex'];
const encodingTypesString = '`ascii`, `utf8`, `utf-8`, `utf16le`, `ucs2`, `ucs-2`, `base64`, `latin1`, `binary`, `hex`';

export default class DecodeCommand extends BushCommand {
	public constructor() {
		super('decode', {
			aliases: ['decode', 'encode'],
			category: 'utilities',
			description: {
				content: 'Decode / encode.',
				usage: 'decode <from> <to> <data>',
				examples: ['decode base64 ascii TmVyZApJbWFnaW5lIGRlY29kaW5nIHRoaXMgbG1hbw==']
			},
			args: [
				{
					id: 'from',
					customType: encodingTypesArray,
					prompt: {
						start: 'What is the encoding of the original data?',
						retry: `{error} Choose one of the following ${encodingTypesString} for the encoding of the original data.`
					}
				},
				{
					id: 'to',
					customType: encodingTypesArray,
					prompt: {
						start: 'What would you like the encoding of the resulting data to be?',
						retry: `{error} Choose one of the following ${encodingTypesString} for the encoding of the resulting data.`
					}
				},
				{
					id: 'data',
					type: 'string',
					match: 'restContent',
					prompt: {
						start: 'What would you to decode.',
						retry: '{error} Choose a valid string to decode.'
					}
				}
			],
			clientPermissions: ['SEND_MESSAGES'],
			slash: true,
			slashOptions: [
				{
					name: 'from',
					description: 'The type of data you are inputting.',
					type: 'STRING',
					choices: [
						{ name: 'ascii', value: 'ascii' },
						{ name: 'utf8', value: 'utf8' },
						{ name: 'utf-8', value: 'utf-8' },
						{ name: 'utf16le', value: 'utf16le' },
						{ name: 'ucs2', value: 'ucs2' },
						{ name: 'ucs-2', value: 'ucs-2' },
						{ name: 'base64', value: 'base64' },
						{ name: 'latin1', value: 'latin1' },
						{ name: 'binary', value: 'binary' },
						{ name: 'hex', value: 'hex' }
					],
					required: true
				},
				{
					name: 'to',
					description: 'The type of data you want the output to be.',
					type: 'STRING',
					choices: [
						{ name: 'ascii', value: 'ascii' },
						{ name: 'utf8', value: 'utf8' },
						{ name: 'utf-8', value: 'utf-8' },
						{ name: 'utf16le', value: 'utf16le' },
						{ name: 'ucs2', value: 'ucs2' },
						{ name: 'ucs-2', value: 'ucs-2' },
						{ name: 'base64', value: 'base64' },
						{ name: 'latin1', value: 'latin1' },
						{ name: 'binary', value: 'binary' },
						{ name: 'hex', value: 'hex' }
					],
					required: true
				},
				{
					name: 'data',
					description: 'What you would like to decode.',
					type: 'STRING',
					required: true
				}
			]
		});
	}

	public override async exec(
		message: BushMessage | AkairoMessage,
		{ from, to, data }: { from: BufferEncoding; to: BufferEncoding; data: string }
	): Promise<unknown> {
		// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
		const encodeOrDecode = util.capitalizeFirstLetter(message?.util?.parsed?.alias || 'decoded');
		const decodedEmbed = new MessageEmbed()
			.setTitle(`${encodeOrDecode} Information`)
			.addField('📥 Input', await util.inspectCleanRedactCodeblock(data));
		try {
			const decoded = Buffer.from(data, from).toString(to);
			decodedEmbed.setColor(util.colors.success).addField('📤 Output', await util.inspectCleanRedactCodeblock(decoded));
		} catch (error) {
			decodedEmbed
				.setColor(util.colors.error)
				.addField(`📤 Error ${encodeOrDecode.slice(1)}ing`, await util.inspectCleanRedactCodeblock(error?.stack ?? error));
		}
		return await message.util.reply({ embeds: [decodedEmbed], allowedMentions: AllowedMentions.none() });
	}
}