· 6 years ago · Apr 27, 2020, 01:46 PM
1const axios = require('axios');
2const Discord = require('discord.js');
3const colorConvert = require('color-convert');
4
5async function requestMotData(registration) {
6 const apiUrl = `https://beta.check-mot.service.gov.uk/trade/vehicles/mot-tests/?registration=${registration}`;
7
8 // Add API key for DVLA
9 // TODO: make this an env var
10 const requestParams = {
11 headers: {'x-api-key': 'lol get your own fuckboy'}
12 };
13
14 return axios.get(apiUrl, requestParams)
15}
16
17async function processDiscordEmbed(response, message, args) {
18 const motInfo = response.data[0];
19
20 // If there are no MOT tests in the response, the car probably hasn't been MOT tested yet
21 if (motInfo.motTests == undefined) {
22 message.channel.send("This car has not been MOT tested yet.");
23 return;
24 }
25
26 // Date is formatted YYYY.MM.DD - just chop off whatever we don't need
27 const registrationYear = motInfo.firstUsedDate.substring(0,4);
28
29 // Map the colour keyword to a hex colour. Otherwise, just accept white.
30 let embedColour;
31 try {
32 embedColour = colorConvert.keyword.hex(motInfo.primaryColour.toLowerCase());
33 }
34 catch {
35 embedColour = 'FFFFFF';
36 }
37
38 if (embedColour == 'FFFFFF') {
39 embedColour = 'FEFEFE';
40 }
41
42 let motData;
43 // Second argument: the year the test took place in.
44 // Third argument: the index of the test,
45 if (args[1] != undefined) {
46 // Get all tests for the given year.
47 motData = motInfo.motTests.filter(motTest => motTest.completedDate.startsWith(args[1]));
48
49 if(motData.length > 1) {
50 if(args[2] != undefined) {
51 motData = motData[args[2] - 1];
52 } else {
53 message.channel.send(`${motData.length} tests were found. Please re-run this command, adding the third argument to specify which test you want to see.`);
54 return;
55 }
56 }
57 }
58 else {
59 // Select the newest test, if no year was specified.
60 motData = motInfo.motTests[0];
61 }
62
63 if(motData.length == 1) {
64 motData = motData[0];
65 } else if (motData.length == 0) {
66 message.channel.send("Couldn't find that MOT test. Please make sure you specified a valid number.");
67 return;
68 }
69
70 const embed = new Discord.MessageEmbed()
71 .setColor(`#${embedColour}`)
72 .setTitle(`${registrationYear} ${motInfo.make} ${motInfo.model}`)
73 .addFields(
74 { name: 'MOT Result', value: motData.testResult, inline: true},
75 { name: 'MOT Date', value: motData.completedDate.substring(0,10), inline: true},
76 { name: 'MOT Expiry', value: motData.expiryDate ? motData.expiryDate : "N/A", inline: true},
77 { name: 'MOT Mileage', value: (`${motData.odometerValue}${motData.odometerUnit}`), inline: true},
78 { name: 'MOT Number', value: motInfo.motTests[0].motTestNumber, inline: true},
79 { name: 'Car Colour', value: motInfo.primaryColour, inline: true},
80 );
81
82 message.channel.send(embed);
83}
84
85module.exports = {
86 name: 'mot',
87 description: 'Get MOT info from DVLA API',
88 execute(message, args) {
89
90 if(args.length == 0) {
91 message.channel.send("No registration number was specified.");
92 }
93
94 const registration = args[0];
95
96 requestMotData(registration)
97 .then(response => processDiscordEmbed(response, message, args))
98 .catch(console.error);
99 }
100};