ravenbotv14/commands/untils/dictionary.js
2022-09-25 02:50:27 -04:00

102 lines
3.6 KiB
JavaScript

const {
EmbedBuilder
} = require('discord.js');
const {
AttachmentBuilder
} = require('discord.js');
var download = require('download-file')
const isNotDefined = require("is-not-defined");
let phonetic
var unirest = require('unirest');
let meaningsArray = []
module.exports = {
name: "dictionary",
description: "Returns information about a word",
options: [{
"name": "word",
"description": "The data you would like inside of the QR",
"required": true,
"type": 3 // 6 is type USER
}],
run: async (client, interaction) => {
let rand = Math.floor(Math.random() * 99999).toString();
let word = interaction.options._hoistedOptions[0].value
unirest
.get('https://api.dictionaryapi.dev/api/v2/entries/en/' + word)
.headers({
'Accept': 'application/json',
'Content-Type': 'application/json',
})
.then((response) => {
let data = response.body[0]
if (isNotDefined(data)){
return interaction.editReply("Sorry, nothing was found!")
}
if (isNotDefined(data.phonetics[1])){
phonetic = ""
} else {
phonetic = data.phonetics[1].text
}
let audio = data.phonetics[0].audio
if (!audio) {
const embed = new EmbedBuilder()
.setColor("#FF0000")
.setTitle("Results for " + word + ": " + phonetic)
.addFields(meaningsArray)
.setTimestamp()
.setFooter({
text: `Requested by ${interaction.user.tag}`,
iconURL: `${interaction.user.displayAvatarURL()}`
});
interaction.editReply({
embeds: [embed]
});
rand = Math.floor(Math.random() * 99999).toString();
} else {
// Options for Audio Download
var options = {
directory: "./audio/",
filename: rand + ".mp3"
}
download(audio, options, function(err) {
if (err) throw err
console.log("audio downloaded")
console.log(response.body)
data.meanings.forEach(wordData => {
meaningsArray.push({
"name": wordData.partOfSpeech,
"value": wordData.definitions[0].definition
})
});
const file = new AttachmentBuilder('./audio/' + rand + ".mp3");
const embed = new EmbedBuilder()
.setColor("#FF0000")
.setTitle("Results for " + word + ": " + phonetic)
.addFields(meaningsArray)
.setTimestamp()
.setFooter({
text: `Requested by ${interaction.user.tag}`,
iconURL: `${interaction.user.displayAvatarURL()}`
});
interaction.editReply({
embeds: [embed],
files: [file]
});
rand = Math.floor(Math.random() * 99999).toString();
})
}
})
},
};