MikkiCoding
MikkiPastel
  • Welcome!
  • 👩‍🏫👩‍💻 Introduction of Programming
    • การทำงานของคอมพิวเตอร์เบื้องต้น
    • การแปลภาษาของโปรแกรม
    • Flowchart
    • Preprocesser Directory
    • Basic Coding
  • 🐍Python for Beginner
    • แนะนำภาษา python
    • ติดตั้งโปรแกรม python ลงบน computer
    • การพิมพ์คำสั่งขั้นต้น
    • Data type และการประกาศตัวแปร
    • Comparison
    • Condition Statement
    • Loop Statement
    • การใช้ break, continue และ pass ใน statement
    • Function
    • Data structure
    • การทำงานร่วมกันกับไฟล์
    • Exception handling
    • Document และแหล่งเรียนรู้อื่นๆ
  • 🤖Discord Bot Hands-on
    • Introduction
    • Discord Bot & Example
    • Create Your First Discord Bot
      • Create Your Discord Server
      • Create Discord Bot Account
      • Bring Discord Bot to Server
    • Workshop
      • Create First Slash Command
      • Create Slash Command with Parameter
      • Return Embeds and Button
      • Webhook
    • Reference
  • 💁‍♀️Android Developer Trip
    • Android Logcat
  • 📑Content
    • Content Roadmap Rule
    • 2024
      • Content Roadmap Sprint 01/2024 (15 - 26 January)
      • Content Roadmap Sprint 02/2024 (29 January - 9 February)
    • 2023
      • Content Roadmap Sprint 01/2023 (21 May - 2 June)
      • Content Roadmap Sprint 02/2023 (5 - 16 June)
      • Content Roadmap Sprint 03/2023 (19 - 30 June)
      • Content Roadmap Sprint 04/2023 (3 - 14 July)
      • Content Roadmap Sprint 05/2023 (17 - 25 July)
      • Content Roadmap Sprint 06/2023 (31 July - 11 August)
      • Content Roadmap Sprint 07/2023 (14 - 25 August)
      • Content Roadmap Sprint 08/2023 (28 August - 8 September)
      • Content Roadmap Sprint 09/2023 (11 - 22 September)
      • Content Roadmap Sprint 10/2023 (25 September - 6 October)
      • Content Roadmap Sprint 11/2023 (9 - 20 October)
      • Content Roadmap Sprint 12/2023 (6 - 17 November)
      • Content Roadmap Sprint 13/2023 (20 November - 1 December)
      • Content Roadmap Sprint 14/2023 (6 - 22 December)
    • 2022
Powered by GitBook
On this page
Edit on GitHub
  1. Discord Bot Hands-on
  2. Workshop

Create Slash Command with Parameter

Last updated 5 months ago

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

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

// 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

🤖