這是一篇從零開始製作一臺能夠運作簡單功能的 Discord Bot 基礎教學文章,將會使用 Python 程式語言撰寫 Discord Bot 回覆使用者訊息的功能。
更新文章內容
- 2024/11/19
- 更多 Python Discord Bot 教學系列和程式範例的 Github 網址更新。
- 由於 Discord Developers 更新介面,更新 Discord Bot 創建內容中的邀請步驟圖片。
一、Discord 簡介:
Discord 誕生於西元 2015 年,一開始是為了遊戲玩家而設計的免費網路即時通話軟體,經過多年來的發展,現今教育人士、學生、商業人士等等各式各樣的人都有在使用 Discord 作為辦公討論的工具。
Discord 功能十分多樣,不僅能夠直播、通話、建立各種新的頻道和伺服器、身分組管理系統等等,還有支持 Markdown 語法,能夠讓文字產生更多的變化和應用。
Discord 在 Windows、macOS、Linux、iOS、Android 和網頁瀏覽器等等常見平台都能運行。
二、Discord Bot 簡介:
Discord Bot 在 Discord 中扮演一個非常重要的角色,Bot 是機器人的意思,能夠自動化地幫助伺服器管理員執行繁忙瑣碎的任務,例如:歡迎新成員、自動分發身分組、點播音樂、管理伺服器秩序等等功能,最重要的是每位 Discord 使用者都能免費打造屬於自己獨一無二的機器人。
三、Discord Bot 創建:
-
先登入 Discord 帳號。
連結:https://discord.com/
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
-
前往 Discord Developers。
連結:https://discord.com/developers/applications
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
-
點擊 New Application,創建機器人的整合系統身分組名稱(未來可修改)。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
-
點選左側功能欄中的 Bot 進入創建頁面。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
-
點擊 Add Bot 創建機器人身分,接著點擊「Yes, do it!」,即可創建成功。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
-
往下滑到 Privileged Gateway Intents 區塊,三個功能選項都點選並按下 Save Change,對未來製作機器人較方便操作。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
-
點擊左側功能欄的 Installation,往下滑到 Default Install Settings 區塊。
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
-
點擊 Guild Install 區塊中 SCOPES 的下拉選項,新增 bot 選項。接著會跑出 PERMISSIONS 的下拉選項,新增 Administrator 選項。
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
-
複製 Install Link 區塊中的邀請連結,進入邀請頁面後,選擇新增至伺服器這個選項,點選要加入的伺服器並授權機器人。
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
-
如果看到機器人出現在成員列表中以及歡迎訊息,代表機器人創建成功。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
四、Discord Bot 撰寫:
執行 Discord Bot 指令大致分為三種方式,三種方式的程式碼撰寫都不太一樣,本次教學以關鍵字和前綴符號指令作為範例,更詳細使用方式可參考未來的教學。
- 關鍵字 - Hello, ping, say
- 前綴符號指令 - $Hello, %ping, &say
- 斜線指令 - /Hello, /ping, /say
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
如果還沒有撰寫 Python 程式的 IDE 的話,可以參考這篇 Vscode 撰寫 Python。
-
終端機輸入下方指令,安裝 discord.py 模組。
pip install discord.py
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
-
回到 Bot 設置頁面,機器人名字下方有機器人的 TOKEN,如果忘記之前的 TOKEN 或者不小心外露出去讓別人知道,只要點擊 Reset Token 換新的一組 TOKEN 即可。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
TOKEN 是控制機器人的鑰匙,擁有者就可以操控機器人,要嚴禁保管!
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
-
撰寫一個簡單功能的程式並執行程式來啟動機器人。
import discord
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents = intents)
@client.event
async def on_ready():
print(f"目前登入身份 --> {client.user}")
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content == "Hello":
await message.channel.send("Hello, world!")
client.run("機器人的TOKEN")
import discord
from discord.ext import commands
intents = discord.Intents.all()
bot = commands.Bot(command_prefix = "%", intents = intents)
@bot.event
async def on_ready():
print(f"目前登入身份 --> {bot.user}")
@bot.command()
async def Hello(ctx):
await ctx.send("Hello, world!")
bot.run("機器人的TOKEN")
-
如果終端機顯示「登入身分 –> 機器人名字」和看見 Discord 成員列表有顯示機器人上線,代表以上步驟都做對。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
-
現在只要在頻道中使用關鍵字「Hello」或者「%Hello」,機器人就會回覆「Hello, world!」囉。
-
關鍵字
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
-
前綴指令
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
感謝各位看完整篇教學,希望大家都有成功製作出屬於自己的 Discord Bot,並且讓它能夠上線成功運作!
上面教學授權機器人的權限的部分,之所以都勾選,是為了要讓機器人運作的成功率提高,如果有時間或有興趣的人可以好好研究看看每個權限分別代表什麼。
此次教學中的兩個程式範例,如果合併起來會發現 Discord Bot 無法正常運作,那是因為關鍵字的 on_message
和前綴指令 command
互相抵觸到,要解決這個問題會使用到 Cog 工具,可參考這篇本作者撰寫的 Python Discord Bot 進階教學 — Cog 篇。
Copyright © 2022 SmallShawn95. All rights reserved.