thieleju / statsbot

Discord Bot to gather data from public APIs using node.js and discord.js! Hacktoberfest Event Project
MIT License
22 stars 45 forks source link

add urban dictionary api #148

Closed appledora closed 1 year ago

appledora commented 1 year ago

Added new command /urban

Issue: #83

appledora commented 1 year ago

@thieleju I was planning to show the response using an embed using the EmbedBuilder but couldn't make it work. Tried something like below :

const embed = new EmbedBuilder()
    .setColor(0xEFFF00)
    .setTitle(myres.word)
.setDescription(myres.description);
 await interaction.editReply({ embeds: [embed] })

Pointers on this would be helpful!

thieleju commented 1 year ago

@appledora Great work! The code in your comment looks like it would work. Did you remember to import the EmbedBuilder Object? :)

const { SlashCommandBuilder, EmbedBuilder } = require("discord.js")

Also a good way to find out whats wrong is to log the error

.catch((error) => {
  console.log(error)
  interaction.reply(
    `No results found for **${term}** or API did not respond.`
  )
})

I'd build the embed like this, but feel free to come up with your own if you want :) image

Code spoiler ```js const { SlashCommandBuilder, EmbedBuilder } = require("discord.js") const axios = require("axios").default module.exports = { data: new SlashCommandBuilder() .setName("urban") .setDescription("Get the urban dictionary definition of a give term.") .addStringOption((option) => option .setName("term") .setDescription('The term to search for. For Example: "dog"') .setRequired(false) ), async execute(interaction) { let term = interaction.options.getString("term") await axios({ method: "get", url: term ? `https://api.urbandictionary.com/v0/define?term=${term}` : "https://api.urbandictionary.com/v0/random", responseType: "json", }) .then((res) => { let myres = res.data.list[0] const embed = new EmbedBuilder() .setColor(0x0099ff) .setTitle(`Urban Dictionary: ${myres.word}`) .addFields({ name: "Definition", value: myres.definition, }) .addFields({ name: "Example", value: myres.example, }) .addFields({ name: "Rating", value: `${myres.thumbs_up} thumbs up. ${myres.thumbs_down} thumbs down.`, }) .setFooter({ text: `Requested by ${interaction.user.tag}` }) .setTimestamp() interaction.reply({ embeds: [embed] }) }) .catch(() => { interaction.reply( `No results found for **${term}** or API did not respond.` ) }) }, } ```
appledora commented 1 year ago

@thieleju Updated the embed, had to use srtFields instead of addField and that did the trick :D