ravenbotv14/commands/untils/dictionary.js
2022-09-25 06:45:06 -04:00

124 lines
4.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) {
data.meanings.forEach(wordData => {
if (wordData.definitions[0].example) {
meaningsArray.push({
"name": wordData.partOfSpeech,
"value": wordData.definitions[0].definition + "\nExample: " + wordData.definitions[0].example
})
} else {
meaningsArray.push({
"name": wordData.partOfSpeech,
"value": wordData.definitions[0].definition
})
}
});
const embed = new EmbedBuilder()
.setColor("#FF0000")
.setTitle(word + ": " + phonetic)
.addFields(meaningsArray)
.setTimestamp()
.setFooter({
text: `Requested by ${interaction.user.tag}`,
iconURL: `${interaction.user.displayAvatarURL()}`
});
interaction.editReply({
embeds: [embed]
});
meaningsArray = []
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(data.meanings)
data.meanings.forEach(wordData => {
if (wordData.definitions[0].example) {
meaningsArray.push({
"name": wordData.partOfSpeech,
"value": wordData.definitions[0].definition + "\nExample: " + wordData.definitions[0].example
})
} else {
meaningsArray.push({
"name": wordData.partOfSpeech,
"value": wordData.definitions[0].definition
})
}
});
const file = new AttachmentBuilder('./audio/' + rand + ".mp3");
const embed = new EmbedBuilder()
.setColor("#FF0000")
.setTitle(word + ": " + phonetic)
.addFields(meaningsArray)
.setTimestamp()
.setFooter({
text: `Requested by ${interaction.user.tag}`,
iconURL: `${interaction.user.displayAvatarURL()}`
});
interaction.editReply({
embeds: [embed],
files: [file]
});
meaningsArray = []
rand = Math.floor(Math.random() * 99999).toString();
})
}
})
},
};