// The Discord instance const Discord = require("discord.js"); // Our configurations const config = require("./config.json"); // Build LUIS's base url const baseURL = `${config.LUIS.URL}luis/prediction/v3.0/apps/${config.LUIS.APP_ID}/slots/production`; // Axios instance, for the requests const axios = require('axios'); let myAxiosInstance = axios.create({ baseURL: baseURL }); // Creating the Discord client const client = new Discord.Client(); client.on('ready', () => { console.info(`Beep boop, this is ${client.user.tag}! Boop Beep!`); }); // The message listener client.on('message', (message) => { // If the message was sent by the bot itself, ignore it. Otherwise, the bot will be in loop. if (message.author == client.user) { return; } // If the message has any content (panaroia check) if (message && message.content && message.content != '') { // Calls LUIS myAxiosInstance.get("/predict", { "params": { "subscription-key": config.LUIS.PRIMARY_KEY, "query": message.content } }).then(response => { // Handle the food request let responseMessage = handleFoodRequest(response.data); message.reply(responseMessage); }).catch(error => { // We don't want our bot to stop if something goes wrong console.log(error); }); } }); // Login in the client client.login(config.BOT_TOKEN); function handleFoodRequest(requestData) { let message = "I couldn't understand your request. Can you be more specific?"; let prediction = requestData.prediction; try { if (prediction) { switch(prediction.topIntent){ // Notice how my intent and entities have spaces. // Spaces on JSON properties are quite ugly but... bear with me on this one case "Ask for food": let food = prediction.entities["Ask food action"][0].food[0]; let modifier = ''; if (prediction.entities["Ask food action"][0]['food modifier']) { modifier = prediction.entities["Ask food action"][0]['food modifier'][0]; } message = `Sure! Here's a ${modifier} ${food}!`; break; } } } catch (error) { console.log(error); } return message; // // // // // //