-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (51 loc) · 1.47 KB
/
index.js
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
require('dotenv').config();
const Discord = require('discord.js');
const moment = require('moment');
const { rastro } = require('rastrojs');
const bot = new Discord.Client();
moment.locale('pt-BR');
bot.on('ready', () => {
console.log('bot is ready!');
});
bot.on('message', async (message) => {
const { content } = message;
if (content.startsWith('!rastreia')) {
const trackingCodes = content.split(' ')[1].split(',');
const results = await trackCodes(trackingCodes);
results.forEach((track) => {
if (track.lastUpdate) {
message.reply(`
Código: ${track.code}
Último local: ${track.lastUpdate.locale}
Status: ${track.lastUpdate.status}
Atualizado em: ${moment(
convertTZ(track.lastUpdate.trackedAt, 'America/Fortaleza')
).format('LLLL')}
`);
} else {
message.reply(`
Código: ${track.code}
Sem informações de rastreio`);
}
});
}
});
bot.login(process.env.TOKEN);
async function trackCodes(trackingCodes = []) {
const tracks = await rastro.track(trackingCodes);
const parsedTracks = tracks.map(parseTracks);
return parsedTracks;
}
function parseTracks({ code, tracks }) {
return {
code,
lastUpdate: tracks ? tracks[tracks.length - 1] : null,
};
}
function convertTZ(date, tzString) {
return new Date(
(typeof date === 'string' ? new Date(date) : date).toLocaleString('en-US', {
timeZone: tzString,
})
);
}