# Create Slash Command with Parameter

ใน Github repo นี้มีไฟล์ json ที่ชื่อว่า `animal.json` เตรียมไว้ให้ เราจะนำไฟล์นี้มาเล่นกัน

<figure><img src="https://lh7-rt.googleusercontent.com/slidesz/AGV_vUd7om9QP6vO6Rj2xgtB8tJmbLj-TIon0f_66M7m-26ERVcRWlSWaqzzlALOvVMmL4pslfITLcnGsjFoP8CD1AxMeSFeyh5YLaw_100qGBVju4eOpgAINy0_8ruOwrcciJwc-VvxTQ=s2048?key=zouhIV8Fq_fCPxgqccmT5w" alt=""><figcaption></figcaption></figure>

กลับไปที่ `server.js` เรามาเพิ่มคำสั่งใหม่กัน

```javascript
// server.js
// Import dependency
const { ..., SlashCommandBuilder } = require('@discordjs/builders');
const { REST } = require('@discordjs/rest');

// Create slash command
const commands = [
   new SlashCommandBuilder()
       .setName('ping')
       .setDescription('Replies with Pong!'),
   // NEW: new command
   new SlashCommandBuilder()
       .setName('animal')
       .setDescription('Encyclopedia animal')
       .addStringOption(option =>
           option.setName('animal')
               .setDescription('role category')
               .setRequired(true)
               .addChoices(
                   { name: 'Hippopotamus', value: 'Hippopotamus'},
                   { name: 'Tiger', value: 'Tiger'},
                   { name: 'Asian Elephant', value: 'Asian Elephant'},
                   { name: 'Capybara', value: 'Capybara'},
               )
       )
 ].map(command => command.toJSON());
 
// NEW: Load json static beta
const animals = require('./data/animal.json');

// Register command
const rest = new REST({ version: '10' }).setToken(process.env.token);
rest.put(Routes.applicationGuildCommands(
    process.env.clientId, 
    process.env.guildId
), { body: commands })
    .then(() => console.log('Successfully registered application commands.'))
    .catch(console.error);

// Reply command
client.on('interactionCreate', async interaction => {
   if (!interaction.isChatInputCommand()) return;
   const { commandName } = interaction;

   if (commandName === 'ping') {
       const timeTaken = Date.now() - interaction.createdTimestamp;
       await interaction.reply(`Pong! This message had a latency of ${timeTaken}ms.`);
   } else if (commandName === 'animal') {
       // NEW: add new command
       const animalInput = interaction.options.getString('animal');
       const animalOutput = animals.find(animal => animal.name_en == animalInput);
       await interaction.reply(animalOutput.name_th);
    }

});

```

* เราเพิ่มคำสั่งที่ชื่อว่า animal และต้องกรอก parameter ด้วย 4 ตัวเลือก คือ Hippopotamus, Tiger, Asian Elephant และ Capybara
* เอา `animal.json` เข้ามาเพื่อเรียกใช้ข้อมูล
* เพิ่ม condition การจัดการคำสั่ง animal โดยเราดึง parameter ที่ user ใส่เข้ามา จากนั้นดึงข้อมูลจาก `animal.json` แล้วมาเทียบชื่อ จากนั้นให้ bot reply เป็นชื่อสัตว์ภาษาไทยออกมา

เมื่อ code ทำงานใหม่ กลับไปดูที่ Discord พบว่าบอทของเราเพิ่มคำสั่ง animal มาแล้วพร้อมตัวเลือกทั้ง 4

<figure><img src="/files/ZQqvTQ1r5Pqoa0EIxMhY" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mikkicoding.mikkipastel.com/discord-bot-hands-on/workshop/create-slash-command-with-parameter.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
