-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
87 lines (63 loc) · 2.96 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* --> Simplan bot for Discord <--
* @description: This bot create IFR flight plans with latest navigraph data
* @author: Boris Tronquoy
* @version: 20200401
*/
// ! this bot is intended to use in a French Discord, that's why all the content is only available in French
const Discord = require('discord.js');
const fetch = require('node-fetch');
global.Headers = fetch.Headers;
const client = new Discord.Client();
const config = require("./config.json");
const urlApi = "https://api.flightplandatabase.com/";
client.login(process.env.TOKEN);
client.on('ready', () => {
client.user.setActivity("préparer des plans de vol")
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', async msg=>{
// ignore messages without prefix command and messages redacted by bots
if (msg.content.indexOf(config.prefix) || (msg.author.bot)) return;
const args = msg.content.slice(config.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if (!args.length && command != "aide") {
return msg.reply(`Il manque des arguments! Tappez \`!simplan aide\` pour avoir de l'aide!`);
}
if (command == "aide") {
return msg.reply(`Ecrivez \`!simplan plan LFLL LFPT\` en remplacant *LFLL* par le code OACI de votre aéroport de départ et *LFPT* par le code OACI de votre aéroport de destination`);
}
let response;
let fplan;
// try to send some data (post)
if ((command == "plan") && args.length == 2) {
let body = {
"fromICAO": args[0],
"toICAO": args[1]
}
// ------------- flight plan is created here but we need to fetch it after ---------------- //
let flightPlanAsk = await fetch("https://api.flightplandatabase.com/auto/generate", {
method: 'POST',
body: JSON.stringify(body),
headers: {'Content-Type': 'application/json', 'Authorization': `Basic ${process.env.API_KEY_FPDATABASE}`}
})
.then(res => res.json())
.then(json => response = json)
.catch((err) => { console.log(err); });
// ------------- flight plan is now fetched here --------- //
let flightPlanGenerated = await fetch("https://api.flightplandatabase.com/plan/" + response.id)
.then(res => res.json())
.then(json => fplan = json)
.catch((err) => { console.log(err); });
let route = " ";
// decode the route
fplan.route.nodes.forEach(function (arrayItem) {
var x = arrayItem.ident;
route += x + " ";
});
// global message for user
let planMsg = `Voici votre **plan de vol généré**\n*Attention: Ce plan de vol est utilisable uniquement à des fin de simulations*\n-----------------\n**Départ:** ${body.fromICAO} (${response.fromName})\n**Arrivée:** ${body.toICAO} (${response.toName})\n**Altitude de croisière:** ${response.maxAltitude} ft\n**Route:** ${route}\n`
planMsg += `\n\nGénéré via flightplandatabase.com -> https://flightplandatabase.com/plan/${response.id}\nBot par Boris Tronquoy (btrn.fr)`;
return msg.reply(planMsg);
}
})