1 |
rakinar2 |
577 |
import { Message, CommandInteraction } from "discord.js"; |
2 |
|
|
import DiscordClient from "../../client/Client"; |
3 |
|
|
import CommandOptions from "../../types/CommandOptions"; |
4 |
|
|
import InteractionOptions from "../../types/InteractionOptions"; |
5 |
|
|
import { emoji } from "../../utils/Emoji"; |
6 |
|
|
import BaseCommand from "../../utils/structures/BaseCommand"; |
7 |
|
|
import crypto, { BinaryToTextEncoding } from 'crypto'; |
8 |
|
|
|
9 |
|
|
export default class HashCommand extends BaseCommand { |
10 |
|
|
supportsInteractions = true; |
11 |
|
|
validAlgorithms = ['sha1', 'sha256', 'sha512', 'md5']; |
12 |
|
|
validDigestModes = ['hex', 'base64', 'base64url']; |
13 |
|
|
|
14 |
|
|
constructor() { |
15 |
|
|
super('hash', 'utils', []); |
16 |
|
|
} |
17 |
|
|
|
18 |
|
|
async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) { |
19 |
|
|
if (!options.isInteraction && options.args[1] === undefined) { |
20 |
|
|
await msg.reply({ content: `${emoji('error')} You must specify the hash algorithm and the content to hash!`, ephemeral: true }); |
21 |
|
|
return; |
22 |
|
|
} |
23 |
|
|
|
24 |
|
|
if (msg instanceof CommandInteraction) |
25 |
|
|
await msg.deferReply({ ephemeral: true }); |
26 |
|
|
|
27 |
|
|
const algo = (options.isInteraction ? options.options.getString("algorithm") : options.args.shift())!; |
28 |
|
|
const digest: BinaryToTextEncoding = options.isInteraction ? (options.options.getString("digest") ?? 'hex') as BinaryToTextEncoding : 'hex'; |
29 |
|
|
|
30 |
|
|
if (!this.validAlgorithms.includes(algo)) { |
31 |
|
|
await this.deferReply(msg, { |
32 |
|
|
content: `${emoji('error')} Invalid algorithm given. Must be one of ${this.validAlgorithms.join(', ')}.` |
33 |
|
|
}); |
34 |
|
|
|
35 |
|
|
return; |
36 |
|
|
} |
37 |
|
|
|
38 |
|
|
if (!this.validDigestModes.includes(digest)) { |
39 |
|
|
await this.deferReply(msg, { |
40 |
|
|
content: `${emoji('error')} Invalid digest mode given. Must be one of ${this.validDigestModes.join(', ')}.` |
41 |
|
|
}); |
42 |
|
|
|
43 |
|
|
return; |
44 |
|
|
} |
45 |
|
|
|
46 |
|
|
const content = options.isInteraction ? options.options.getString("content") : options.args.join(' '); |
47 |
|
|
|
48 |
|
|
const hashsum = crypto.createHash(algo); |
49 |
|
|
hashsum.update(content!); |
50 |
|
|
const hashedData = hashsum.digest(digest); |
51 |
|
|
|
52 |
|
|
await this.deferReply(msg, { |
53 |
|
|
content: `**Hash (${algo}):**\n\`\`\`${hashedData}\`\`\`` |
54 |
|
|
}); |
55 |
|
|
} |
56 |
|
|
} |