aboutsummaryrefslogtreecommitdiff
path: root/src/commands/utilities/wolframAlpha.ts
blob: 4a357b8674fc3a8be90143918238e0626a45a75f (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
import { AllowedMentions, BushCommand, BushMessage, BushSlashMessage } from '@lib';
import { CommandInteraction, MessageEmbed } from 'discord.js';
import WolframAlphaAPI from 'wolfram-alpha-api';

export default class WolframAlphaCommand extends BushCommand {
	public constructor() {
		super('wolframAlpha', {
			aliases: ['wolfram-alpha', 'wolfram', 'alpha', 'wolf', 'wa'],
			category: 'utilities',
			description: {
				content: 'Queries Wolfram|Alpha for a result.',
				usage: 'wolfram-alpha <expression>',
				examples: ['wolfram-alpha what is the population of france']
			},
			args: [
				{
					id: 'expression',
					type: 'string',
					match: 'rest',
					prompt: {
						start: 'What would you like to look up?',
						retry: '{error} Pick something to look up.',
						optional: false
					}
				}
			],
			slash: true,
			slashOptions: [
				{
					name: 'expression',
					description: 'What would you like to look up?',
					type: 'STRING',
					required: true
				}
			],
			clientPermissions: ['SEND_MESSAGES'],
			userPermissions: ['SEND_MESSAGES']
		});
	}
	public override async exec(message: BushMessage | BushSlashMessage, args: { expression: string }): Promise<unknown> {
		if (message.util.isSlash) await (message.interaction as CommandInteraction).deferReply();

		const waApi = WolframAlphaAPI(client.config.credentials.wolframAlphaAppId);

		const decodedEmbed = new MessageEmbed().addField('📥 Input', await util.inspectCleanRedactCodeblock(args.expression));
		try {
			const calculated = await waApi.getShort(args.expression);
			decodedEmbed
				.setTitle(`${util.emojis.successFull} Successfully Queried Expression`)
				.setColor(util.colors.success)
				.addField('📤 Output', await util.inspectCleanRedactCodeblock(calculated.toString()));
		} catch (error) {
			decodedEmbed
				.setTitle(`${util.emojis.errorFull} Unable to Query Expression`)
				.setColor(util.colors.error)
				.addField(`📤 Error`, await util.inspectCleanRedactCodeblock(`${error.name}: ${error.message}`, 'js'));
		}
		return await message.util.reply({ embeds: [decodedEmbed], allowedMentions: AllowedMentions.none() });
	}
}