aboutsummaryrefslogtreecommitdiff
path: root/src/commands/utilities/wolframAlpha.ts
blob: 0e1cb1495742354ab4ecf303fa166b347b54c8a7 (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
import { AllowedMentions, BushCommand, type BushMessage, type BushSlashMessage } from '#lib';
import { initializeClass as WolframAlphaAPI } from '@notenoughupdates/wolfram-alpha-api';
import assert from 'assert';
import { ApplicationCommandOptionType, EmbedBuilder, type MessageOptions } from 'discord.js';

assert(WolframAlphaAPI);

export default class WolframAlphaCommand extends BushCommand {
	public constructor() {
		super('wolframAlpha', {
			aliases: ['wolfram-alpha', 'wolfram', 'alpha', 'wolf', 'wa'],
			category: 'utilities',
			description: 'Queries Wolfram|Alpha for a result.',
			usage: ['wolfram-alpha <expression>'],
			examples: ['wolfram-alpha what is the population of france'],
			args: [
				{
					id: 'expression',
					description: 'The expression to query the Wolfram|Alpha api for.',
					type: 'string',
					match: 'rest',
					prompt: 'What would you like to look up?',
					retry: '{error} Pick something to look up.',
					slashType: ApplicationCommandOptionType.String
				},
				{
					id: 'image',
					description: 'Whether to use the Simple API instead of the Short Answers API.',
					match: 'flag',
					flag: '--image',
					prompt: 'Would you like to use the Simple API instead of the Short Answers API?',
					slashType: ApplicationCommandOptionType.Boolean,
					optional: true
				}
			],
			slash: true,
			clientPermissions: (m) => util.clientSendAndPermCheck(m),
			userPermissions: []
		});
	}

	public override async exec(message: BushMessage | BushSlashMessage, args: { expression: string; image: boolean }) {
		if (message.util.isSlashMessage(message)) await message.interaction.deferReply();

		args.image && void message.util.reply({ content: `${util.emojis.loading} Loading...`, embeds: [] });
		const waApi = WolframAlphaAPI(client.config.credentials.wolframAlphaAppId);

		const decodedEmbed = new EmbedBuilder().addFields([
			{
				name: '📥 Input',
				value: await util.inspectCleanRedactCodeblock(args.expression)
			}
		]);
		const sendOptions: MessageOptions = { content: null, allowedMentions: AllowedMentions.none() };
		try {
			const calculated = await (args.image
				? waApi.getSimple({ i: args.expression, timeout: 1, background: '2C2F33', foreground: 'white' })
				: waApi.getShort(args.expression));
			decodedEmbed.setTitle(`${util.emojis.successFull} Successfully Queried Expression`).setColor(util.colors.success);

			if (args.image) {
				decodedEmbed.setImage(await util.uploadImageToImgur(calculated.split(',')[1]));
				decodedEmbed.addFields([{ name: '📤 Output', value: '​' }]);
			} else {
				decodedEmbed.addFields([{ name: '📤 Output', value: await util.inspectCleanRedactCodeblock(calculated.toString()) }]);
			}
		} catch (error) {
			decodedEmbed
				.setTitle(`${util.emojis.errorFull} Unable to Query Expression`)
				.setColor(util.colors.error)
				.addFields([
					{ name: `📤 Error`, value: await util.inspectCleanRedactCodeblock(`${error.name}: ${error.message}`, 'js') }
				]);
		}
		sendOptions.embeds = [decodedEmbed];

		return await message.util.reply(sendOptions);
	}
}