Webhook

เราสามารถสร้างบอทที่ยิงข้อความเข้าไปใน 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 สามารถอ่านเพิ่มเติมได้ที่นี่

Last updated