ravenbotv14/commands/untils/dictionary.js

109 lines
4.0 KiB
JavaScript
Raw Normal View History

2022-09-25 02:27:34 -04:00
const {
EmbedBuilder
} = require('discord.js');
const {
AttachmentBuilder
} = require('discord.js');
var download = require('download-file')
2022-09-25 02:50:27 -04:00
const isNotDefined = require("is-not-defined");
let phonetic
2022-09-25 02:27:34 -04:00
var unirest = require('unirest');
let meaningsArray = []
2022-09-25 02:50:27 -04:00
2022-09-25 02:27:34 -04:00
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) => {
2022-09-25 02:51:42 -04:00
let data = response.body[0]
if (isNotDefined(data)) {
return interaction.editReply("Sorry, nothing was found!")
2022-09-25 02:50:27 -04:00
}
2022-09-25 02:27:34 -04:00
2022-09-25 02:51:42 -04:00
if (isNotDefined(data.phonetics[1])) {
phonetic = ""
2022-09-25 02:50:27 -04:00
} else {
2022-09-25 02:51:42 -04:00
phonetic = data.phonetics[1].text
2022-09-25 02:50:27 -04:00
}
2022-09-25 02:51:42 -04:00
2022-09-25 02:50:27 -04:00
let audio = data.phonetics[0].audio
2022-09-25 02:27:34 -04:00
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]
});
2022-09-25 02:58:27 -04:00
meaningsArray = []
2022-09-25 02:27:34 -04:00
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 => {
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
})
}
2022-09-25 02:27:34 -04:00
});
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]
});
2022-09-25 02:58:27 -04:00
meaningsArray = []
2022-09-25 02:27:34 -04:00
rand = Math.floor(Math.random() * 99999).toString();
})
}
})
},
};