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
  • Fork Github Repo
  • สร้าง .env file
Edit on GitHub
  1. Discord Bot Hands-on

Workshop

Last updated 5 months ago

Fork Github Repo

ก่อนอื่นมา setup project กัน โดย fork repo นี้จาก Github มาก่อน

จากนั้นเปิด CodeSpaces ในนั้นรองรับ node 20 แล้วด้วยนะ เราใช้ library Discord version ล่าสุดเลย ที่รองรับ node 18

หน้าตาของ Codesapces

อธิบายโค้ดกันก่อน

// server.js
require('dotenv').config();

// Import dependency
const { Client, Events, GatewayIntentBits } = require('discord.js');

// Create a new client instance
const client = new Client({ intents: [GatewayIntentBits.Guilds] });

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

// Log in to Discord with your client's token
client.login(process.env.token);
  • Import library discord.js เรียก Client, Events, GatewayIntentBits สามอันนี้เป็น default ที่เราต้องเรียกใช้

  • สร้าง client instance ที่เป็นตัว intent เป็น GatewayIntentBits.Guilds เป็นตัวที่จำเป็นในการทำงาน

  • เมื่อ client พร้อมแล้ว ให้ run client.once()

  • และอย่าลืม logic ก่อนใช้งาน

สร้าง .env file

ต่อมาสร้างไฟล์ .env แล้วใส่ตามนี้

// .env
token=...
clientId=...
guildId=...
  • token ไปที่ Bot แล้วกดปุ่ม Reset Token แล้วเราจะได้ token มาใช้

  • clientId เอามาจาก APPLICATION ID ที่ได้หลังจากแอพเสร็จ

  • guildId คือ server id ที่เราต้องการเทส วิธีการเอาไปใช้ ไปที่ server setting -> Widget แล้วทำการ copy ออกมา

สุดท้ายพิมพ์ command นี้เพื่อ install package ต่าง ๆ เข้ามาในโปรเจกต์

npm install

🤖
GitHub - mikkipastel/discord-bot-workshop: part of my first workshop "Discord Bot from my Experience"GitHub
Logo