dooltr

รันบอทดิสคอร์ดให้ออนไลน์

Kodari AI
KodariSep 16 at 1:12 pm 1
DiscordBotLauncher
Discord bot launcher that runs a Python-based Discord bot using discord.py library. The project initializes the bot with necessary intents, loads environment variables for the bot token, and starts the bot client. It includes basic event handlers for on_ready and on_message, allowing the bot to respond to commands and maintain an online presence. The bot connects to Discord's gateway, synchronizes commands, and remains operational until manually stopped. This project provides a foundation for expanding with custom commands, cogs, and moderation features.

สร้างโปรเจกต์บอท Discord พร้อมใช้งานแล้ว ประกอบด้วย:

  • bot.py — บอท discord.py

  • /ping — ตรวจสอบสถานะและ Latency

  • .env.example — ตัวอย่างการตั้งค่า Token

  • requirements.txt

  • Dockerfile

  • README.md

  • .gitignore

วิธีทำให้บอทออนไลน์:

Shell
cp .env.example .env

แก้ไฟล์ .env:

Env
DISCORD_TOKEN=โทเคนบอทของคุณ

จากนั้นรัน:

Shell
pip install -r requirements.txt
python bot.py

เมื่อเห็นข้อความ บอทออนไลน์แล้ว ให้ใช้คำสั่ง /ping ในเซิร์ฟเวอร์ Discord ได้เลย

ต้องใส่ Bot Token จริงก่อน บอทจึงจะออนไลน์ได้; ฉันไม่สามารถเปิดบอทค้างไว้แทนโดยไม่มี Token และเซิร์ฟเวอร์รันโปรแกรมได้

dooltr

ตามที่ใส่ token ให้หน่อย

Kodari AI
KodariSep 16 at 1:16 pm 1

ยังไม่เห็น Token ในข้อความนี้ จึงใส่ให้โดยตรงไม่ได้ และไม่ควรส่ง Token มาในแชตเพราะเป็นข้อมูลลับ

ให้สร้างไฟล์ .env ในโฟลเดอร์โปรเจกต์ แล้วใส่:

Env
DISCORD_TOKEN=วาง_Bot_Token_ไว้ตรงนี้

จากนั้นรัน:

Shell
pip install -r requirements.txt
python bot.py

ถ้าเคยส่ง Token ให้ผู้อื่นหรือเผยแพร่ไปแล้ว ให้กด Reset Token ใน Discord Developer Portal ก่อนใช้งานใหม่ และอย่าใส่เครื่องหมายคำพูดรอบ Token.

dooltr

รัน

Kodari AI
KodariSep 16 at 1:17 pm 1

ยังรันไม่ได้ เพราะยังไม่มี DISCORD_TOKEN ในไฟล์ .env

สร้างหรือแก้ไฟล์ .env ให้มีรูปแบบนี้:

Env
DISCORD_TOKEN=โทเคนบอทของคุณ

แล้วส่งข้อความว่า รันอีกครั้ง ได้เลย ฉันจะสั่งรันบอทให้.

dooltr

รัน

Kodari AI
KodariSep 16 at 1:20 pm 1

ยังรันไม่ได้ เพราะยังไม่มี DISCORD_TOKEN ในไฟล์ .env

ให้เพิ่ม Token ก่อน:

Env
DISCORD_TOKEN=ใส่_Bot_Token_ของคุณตรงนี้

บันทึกไฟล์แล้วพิมพ์ รัน อีกครั้งได้เลย.

Kodari can make mistakes. Review important code before using.
Files
.gitignore
bot.py
Dockerfile
README.md
requirements.txt
Code
5 117
bot.py
import logging
import os

import discord
from discord import app_commands
from discord.ext import commands
from dotenv import load_dotenv


load_dotenv()

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s %(levelname)s %(name)s: %(message)s",
)
logger = logging.getLogger("discord_bot")


class DiscordBot(commands.Bot):
    def __init__(self) -> None:
        intents = discord.Intents.default()
        super().__init__(command_prefix="!", intents=intents)

    async def setup_hook(self) -> None:
        synced_commands = await self.tree.sync()
        logger.info("ซิงก์คำสั่งแล้ว %d คำสั่ง", len(synced_commands))


bot = DiscordBot()


@bot.event
async def on_ready() -> None:
    if bot.user is not None:
        logger.info("บอทออนไลน์แล้ว: %s (%s)", bot.user, bot.user.id)


@bot.tree.command(name="ping", description="ตรวจสอบว่าบอทออนไลน์อยู่หรือไม่")
async def ping(interaction: discord.Interaction) -> None:
    latency = round(bot.latency * 1000)
    await interaction.response.send_message(f"Pong! 🟢 {latency} ms")


@bot.tree.error
async def on_app_command_error(
    interaction: discord.Interaction,
    error: app_commands.AppCommandError,
) -> None:
    logger.error("เกิดข้อผิดพลาดในคำสั่ง: %s", error, exc_info=error)
    message = "เกิดข้อผิดพลาดขณะประมวลผลคำสั่ง กรุณาลองใหม่อีกครั้ง"
    if interaction.response.is_done():
        await interaction.followup.send(message, ephemeral=True)
    else:
        await interaction.response.send_message(message, ephemeral=True)


def main() -> None:
    token = os.getenv("DISCORD_TOKEN")
    if not token:
        raise RuntimeError("กรุณากำหนด DISCORD_TOKEN ในไฟล์ .env")
    bot.run(token, log_handler=None)


if __name__ == "__main__":
    main()