summaryrefslogtreecommitdiff
path: root/src/commands/utility/romannumeral.ts
blob: 39b9320f298154ef8376d7fab0a3bd61be8fb4ba (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
47
48
49
50
51
52
53
54
55
56
57
import { Command, CommandoMessage, CommandoClient } from 'discord.js-commando';
//@ts-ignore no types
import emoji from 'emoji-random'
//@ts-ignore no types
import romanize from 'romanize'

module.exports = class RomanNumeralUtility extends Command {
    constructor(client: CommandoClient) {
        super(client, {
            name: 'romannumeral',
            aliases: [
                'roman-numeral',
                'romannumerals',
                'roman-numerals'
            ],
            group: 'utility',
            memberName: 'romannumeral',
            description: 'Converts a number to a roman numeral.',
            examples: ['uwu!romannumeral 12'],
            throttling: {
                usages: 5,
                duration: 30
            },
            userPermissions: ['SEND_MESSAGES', 'READ_MESSAGE_HISTORY'],
            clientPermissions: ['SEND_MESSAGES', 'READ_MESSAGE_HISTORY'],
            args: [
                {
                    key: 'nNum',
                    prompt: 'What number would you like to translate',
                    type: 'integer',
                    min: 1
                }
            ]
        });
    }
    run(msg: CommandoMessage, { nNum }: any) {
        if (nNum === parseInt(nNum, 10)) {
            msg.reply(romanize(nNum))
        }

        const back = (value: string) => {
            let res = 0

            const decimal = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
            const roman = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]
            for (let i = 0; i <= decimal.length; i++) {
                while (value.indexOf(roman[i]) === 0) {
                    res += decimal[i]
                    value = value.replace(roman[i], '')
                }
            }
            return res
        }

        return msg.reply(back(nNum) + ' ' + emoji.random())
    }
};