summaryrefslogtreecommitdiff
path: root/src/commands/utility/average.ts
blob: 233236c557f8a1564db409b0d4b19f9a6d23116b (plain) (blame)
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
import { Command, CommandoMessage, CommandoClient } from 'discord.js-commando';
//@ts-ignore no types
import emoji from 'emoji-random'
import { formatDistance, formatRelative } from 'date-fns'
//TODO: check if this has types
//@ts-ignore no types
import { stripIndents } from 'common-tags'

module.exports = class AverageUtility extends Command {
    constructor(client: CommandoClient) {
        super(client, {
            name: 'average',
            aliases: [
                'average-number',
                'averagenumber',
                'average-num',
                'averagenum'
            ],
            group: 'utility',
            memberName: 'average',
            description: 'Gets the average of specified numbers.',
            examples: ['uwu!average 10 20 30 40 50'],
            throttling: {
                usages: 5,
                duration: 30
            },
            userPermissions: ['SEND_MESSAGES', 'READ_MESSAGE_HISTORY'],
            clientPermissions: ['SEND_MESSAGES', 'READ_MESSAGE_HISTORY'],
            args: [
                {
                    key: 'nNum',
                    prompt: 'What\'s another number you would like to average?',
                    type: 'float',
                    label: 'number',
                    infinite: true
                }
            ]
        });
    }
    async run(msg: CommandoMessage, { nNum }: any) { // this is really a string
        if (nNum.length < 2) msg.reply('Please provide **2** or more numbers.')

        const reducer = (accumulator: any, currentValue: any) => accumulator + currentValue
        return msg.reply(`The average of the specified numbers is ${nNum.reduce(reducer) / nNum.length}.` + ' ' + emoji.random())
    }
};