# Python Discord 機器人 --- ## 1. 介紹 ---- ### 1.1 什麼是 Discord 機器人? Discord 機器人是一個自動執行任務的程式,可以在 Discord 平台上提供各種功能,例如管理伺服器、播放音樂或回應指令。 ---- ### 1.2 為什麼使用 Python? Python 是一種簡單易學、功能豐富的程式語言,Discord.py 庫提供了一個強大的框架,使得使用 Python 開發 Discord 機器人變得輕鬆。 --- ## 2. 準備工作 ---- ### 2.1 安裝 Discord.py 使用 `pip` 安裝 Discord.py 庫,確保你的 Python 環境正確設置。 ```bash pip install discord.py ``` ---- ### 2.2 接著到DC developer登入並創建機器人(https://discord.com/developers/applications) 在 Discord 開發者入口網站建立一個應用程式(new application),獲取機器人令牌(Bot Token)。 ---- ### 2.3 完成權限設定 利用網址邀請至伺服器 ![image alt](https://cdn.discordapp.com/attachments/678292614398869513/1044951686344605726/image.png) --- ## 3. 基本指令 ---- ### 3.1 設定機器人連接到 Discord 伺服器 使用機器人令牌初始化 Discord 客戶端,並將機器人添加到伺服器。 ```python import discord client = discord.Client() @client.event async def on_ready(): print(f'We have logged in as {client.user}') client.run('YOUR_BOT_TOKEN') ``` ---- ### 3.2 撰寫和執行基本指令 創建指令處理函式,並在機器人接收到指令時執行相應的動作。 ```python @client.event async def on_message(message): if message.author == client.user: return if message.content.startswith('!hello'): await message.channel.send('Hello!') ``` --- # 如何知道其他機器人的製作發法 ---- ## 1.去看官方API看看各種機器人的用法 https://discordpy.readthedocs.io/en/stable/api.html ---- ## 2.去看影片或爬論壇,很多人都會分享如何寫出一種機器人的功能 ### 記得找比較新的影片或論壇,bot的語法會更新 https://github.com/ https://www.reddit.com/user/criddit_/ https://www.youtube.com/ ----
{"contributors":"[{\"id\":\"7137adef-c60a-40a1-8949-9e15468f84eb\",\"add\":1482,\"del\":0}]","title":"Python Discord 機器人","description":"Discord 機器人是一個自動執行任務的程式,可以在 Discord 平台上提供各種功能,例如管理伺服器、播放音樂或回應指令。"}
    406 views
   owned this note