aboutsummaryrefslogtreecommitdiff
path: root/src/commands/utilities/hash.ts
blob: 4b5b01c159d81375399077195071ec57f5bca8ee (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
import crypto from 'crypto';
import { Constants } from 'discord-akairo';
import got from 'got';
import { BushCommand, BushMessage } from '../../lib';

export default class HashCommand extends BushCommand {
	constructor() {
		super('hash', {
			aliases: ['hash'],
			category: 'utilities',
			description: {
				content: 'Gets the file hash of the given discord link',
				usage: 'hash <file url>',
				examples: ['hash https://cdn.discordapp.com/emojis/782630946435366942.png?v=1'] //nice
			},
			args: [
				{
					id: 'url',
					type: Constants.ArgumentTypes.URL,
					match: Constants.ArgumentMatches.PHRASE,
					prompt: {
						start: 'What url would you like to find the hash of?',
						retry: '{error} Enter a valid url.'
					}
				}
			],
			clientPermissions: ['SEND_MESSAGES']
		});
	}

	public async exec(message: BushMessage, { url }: { url: string }): Promise<void> {
		try {
			const req = await got.get(url);
			const rawHash = crypto.createHash('md5');
			rawHash.update(req.rawBody.toString('binary'));
			const hash = rawHash.digest('hex');
			await message.util.reply(`\`${hash}\``);
		} catch {
			await message.util.reply('Unable to calculate hash.');
		}
	}
}