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

Webhook

Last updated 5 months ago

เราสามารถสร้างบอทที่ยิงข้อความเข้าไปใน text channel บน server ได้นะ โดยใช้ webhook

ไปที่ Server Settings -> Integations -> Create Webhook

จากนั้นเข้าไปที่ webhook ที่สร้าง โดยกดที่ตัว webhook เราสามารถใส่ชื่อ รูป เพื่อให้เราจำได้ แล้วก็ channel ที่เราต้องการ แล้วก็ Copy Webhook URL เพื่อเอาไปใช้ต่อ โดยหน้าตัว url นี้ จะเป็น https://discord.com/api/webhooks/{id}/{token}

กลับไปที่ server.js แล้วเพิ่มโค้ดเหล่านี้

// server.js
const { ..., WebhookClient } = require('discord.js');

const webhookClient = new WebhookClient(
    { url: 'https://discord.com/api/webhooks/{id}/{token}'}
);
const webhookClient = new WebhookClient({ id: 'id', token: 'token' });

client.once(Events.ClientReady, readyClient => {
   console.log(`Ready! Logged in as ${readyClient.user.tag}`);

   // send message webhook
   try {
       webhookClient.send({
           username: 'BB-8',
           avatarURL: 'https://i.imgur.com/PHnLYAm.png',
           content: 'Webhook test',
       })
   } catch (error) {
       common.error('Error trying to send: ', error);
   }
});
  • เพิ่ม webhookClient มาด้วย ตอน import library มา

  • webhookClient สามารถสร้างเพิ่มได้ 2 วิธีด้วยกัน

ใช้ url โดยตรง

const webhookClient = new WebhookClient(
    { url: 'https://discord.com/api/webhooks/{id}/{token}'}
);

ใช้ id และ token จาก url จาก format นี้ https://discord.com/api/webhooks/{id}/{token}

const webhookClient = new WebhookClient({ id: 'id', token: 'token' });
  • ที่ client.once() ใส่การส่งข้อความที่ให้ webhook ส่งข้างใน

เมื่อ code ทำงานใหม่ บอทจะส่งข้อความนี้มาที่ text channel ที่เราต้องการ

เราสามารถส่งข้อความแบบ Embed ได้ด้วยนะ

// server.js
client.once(Events.ClientReady, readyClient => {
   console.log(`Ready! Logged in as ${readyClient.user.tag}`);

   // send message webhook
   try {
       const embed = new EmbedBuilder()
           .setTitle('Some Title')
           .setColor(0x00FFFF);

       webhookClient.send({
           username: 'BB-8',
           avatarURL: 'https://i.imgur.com/PHnLYAm.png',
           content: 'Webhook test',
           embeds: [embed]
       })
   } catch (error) {
       common.error('Error trying to send: ', error);
   }
});

ผลที่ได้

เกี่ยวกับ webhook สามารถอ่านเพิ่มเติมได้

🤖
ที่นี่