toowake

dictionary.js

Aug 7th, 2023
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
  2.  
  3. module.exports = {
  4. data: new SlashCommandBuilder()
  5. .setName('dictionary')
  6. .setDescription('search a word in the dictionary')
  7. .addStringOption(option => option.setName('word').setDescription('the word you want to search').setRequired(true)),
  8. async execute(interaction) {
  9.  
  10. const word = interaction.options.getString('word');
  11.  
  12. let data = await fetch(`https://api.dictionaryapi.dev/api/v2/entries/en/${word}`)
  13.  
  14. if (data.statusText == 'Not Found') {
  15. return interaction.reply({ content: 'that word does not exist', ephemeral: true});
  16. }
  17.  
  18. let info = await data.json();
  19. let result = info[0];
  20.  
  21. let embedInfo = await result.meanings.map((data, index) => {
  22.  
  23. let definition = data.definitions[0].definition || "No definition found";
  24. let example = data.definitions[0].example || "No example found";
  25.  
  26.  
  27. return {
  28. name: data.partOfSpeech.toUpperCase(),
  29. value: `\`\`\` Description: ${definition} \n Example: ${example} \`\`\``,
  30. };
  31.  
  32.  
  33. });
  34.  
  35. const embed = new EmbedBuilder()
  36. .setColor('Blue')
  37. .setTitle(`Definition of | **${result.word}**`)
  38. .addFields(embedInfo)
  39.  
  40. await interaction.reply({ embeds: [embed] });
  41. }
  42. }
Add Comment
Please, Sign In to add comment