Guest User

Checking for Existing Options

a guest
Jan 5th, 2024
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module.exports = {
  2.     data: new SlashCommandBuilder()
  3.         // ... previous code for our SlashCommandBuilder.
  4.  
  5.     async execute(interaction) {
  6.         // For simplicity, we'll declare our individual options.
  7.         const authorName = interaction.options.getString('author-name')
  8.         const authorIconURL = interaction.options.getString('author-icon-url')
  9.         const authorUrl = interaction.options.getString('author-url')
  10.  
  11.         // We will now delcare our new EmbedBuilder, allowing us to add options later.
  12.         const embed = new EmbedBuilder()
  13.  
  14.         // Now we need to add our authors. The easiest way to do this would be using
  15.         // conditional (ternary) operators. Discord expects to receiver a string,
  16.         // null, or undefined. With this knowledge, we can use an operator to pass
  17.         // undefined if our option doesn't exist.
  18.        
  19.         // Let's not forget that we also need to make sure the author name exists.
  20.         // Without our author name, we cannot use the other options since the name
  21.         // is absolutely required if we're including an author in our embed.
  22.        
  23.         // With all of that in mind, let's implement it...
  24.         if (authorName) {
  25.             embed.setAuthor({
  26.                 name: authorName,
  27.                 iconURL: authorIconURL || undefined,
  28.                 url: authorUrl || undefined
  29.             })
  30.         }
  31.  
  32.         // You can do this for the rest of your options as well. If the string
  33.         // option is required, you won't need to since it's guaranteed.
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment