這是一篇從零開始製作一臺能夠運作簡單功能的 Discord Bot 基礎教學文章,將會使用 Python 程式語言撰寫 Discord Bot 回覆使用者訊息的功能。 :::success :book: **更多 Python Discord Bot 教學系列和程式範例** https://github.com/smallshawn95/Python-Discord-Bot-Teach.git ::: --- [TOC] --- ## 一、Discord 簡介: Discord 誕生於西元 2015 年,一開始是為了遊戲玩家而設計的免費網路即時通話軟體,經過多年來的發展,現今教育人士、學生、商業人士等等各式各樣的人都有在使用 Discord 作為辦公討論的工具。 Discord 功能十分多樣,不僅能夠直播、通話、建立各種新的頻道和伺服器、身分組管理系統等等,還有支持 Markdown 語法,能夠讓文字產生更多的變化和應用。 Discord 在 Windows、macOS、Linux、iOS、Android 和網頁瀏覽器等等常見平台都能運行。 ## 二、Discord Bot 簡介: Discord Bot 在 Discord 中扮演一個非常重要的角色,Bot 是機器人的意思,能夠自動化地幫助伺服器管理員執行繁忙瑣碎的任務,例如:歡迎新成員、自動分發身分組、點播音樂、管理伺服器秩序等等功能,最重要的是每位 Discord 使用者都能免費打造屬於自己獨一無二的機器人。 ## 三、Discord Bot 創建: 1. 先登入 Discord 帳號。 連結:https://discord.com/ ![Picture_1](https://i.imgur.com/cZY3wYb.png) 2. 前往 Discord Developers。 連結:https://discord.com/developers/applications ![Picture_2](https://i.imgur.com/eAaazuq.png) 3. 點擊 New Application,創建機器人的整合系統身分組名稱(未來可修改)。 ![Picture_3](https://i.imgur.com/Z1x1ucZ.png) ![Picture_4](https://i.imgur.com/iWpSBQN.png) 4. 點選左側功能欄中的 Bot 進入創建頁面。 ![Picture_5](https://i.imgur.com/G6gsxVh.png) 5. 點擊 Add Bot 創建機器人身分,接著點擊「Yes, do it!」,即可創建成功。 ![Picture_6](https://i.imgur.com/mPUSVZH.png) ![Picture_7](https://i.imgur.com/a1oQbxi.png) 6. 往下滑到 Privileged Gateway Intents 區塊,三個功能選項都點選並按下 Save Change,對未來製作機器人較方便操作。 ![Picture_8](https://i.imgur.com/SSNcPx3.png) 7. 點擊左側功能欄 OAuth2 會跑出兩個選項,點擊 URL Generator 選項。 ![Picture_9](https://i.imgur.com/3DBLP92.png) 8. 右方 SCOPES 區塊中選擇 bot,往下滑點選 Administrator,賦予機器人所有權限,而最下方的 GENERATED URL 就是邀請機器人到伺服器中的連結。 ![Picture_10](https://i.imgur.com/NMSOa0s.png) ![Picture_11](https://i.imgur.com/JmVJDTp.png) 9. 前往上方獲得的連結,進入邀請頁面後,點選要加入的伺服器並授權機器人。 ![Picture_12](https://i.imgur.com/GqlvyHv.png) ![Picture_13](https://i.imgur.com/Vz0hCuk.png) 10. 如果看到機器人出現在成員列表中以及歡迎訊息,代表機器人創建成功。 ![Picture_14](https://i.imgur.com/J0CcDFw.png) ![Picture_15](https://i.imgur.com/RlFsRTJ.png) ## 四、Discord Bot 撰寫: 執行 Discord Bot 指令大致分為三種方式,三種方式的程式碼撰寫都不太一樣,本次教學以關鍵字和前綴符號指令作為範例,更詳細使用方式可參考未來的教學。 * 關鍵字 - Hello, ping, say * 前綴符號指令 - $Hello, %ping, &say * 斜線指令 - /Hello, /ping, /say :::warning :bell: 如果還沒有撰寫 Python 程式的 IDE 的話,可以參考這篇 **[Vscode 撰寫 Python](https://hackmd.io/@smallshawn95/vscode_write_py)**。 ::: 1. 終端機輸入下方指令,安裝 [discord.py](https://discordpy.readthedocs.io/en/stable/) 模組。 ```python= pip install discord.py ``` ![Picture_16](https://i.imgur.com/dhnh5GK.png) 2. 回到 Bot 設置頁面,機器人名字下方有機器人的 TOKEN,如果忘記之前的 TOKEN 或者不小心外露出去讓別人知道,只要點擊 Reset Token 換新的一組 TOKEN 即可。 :::danger :warning: TOKEN 是控制機器人的鑰匙,擁有者就可以操控機器人,要嚴禁保管! ::: ![Picture_17](https://i.imgur.com/Z8gOccO.png) ![Picture_18](https://i.imgur.com/UCkINjA.png) 3. 撰寫一個簡單功能的程式並執行程式來啟動機器人。 * 關鍵字 ```python= # 導入Discord.py模組 import discord # client是跟discord連接,intents是要求機器人的權限 intents = discord.Intents.default() intents.message_content = True client = discord.Client(intents = intents) # 調用event函式庫 @client.event # 當機器人完成啟動 async def on_ready(): print(f"目前登入身份 --> {client.user}") @client.event # 當頻道有新訊息 async def on_message(message): # 排除機器人本身的訊息,避免無限循環 if message.author == client.user: return # 新訊息包含Hello,回覆Hello, world! if message.content == "Hello": await message.channel.send("Hello, world!") client.run("機器人的TOKEN") ``` * 前綴指令 ```python= # 導入Discord.py模組 import discord # 導入commands指令模組 from discord.ext import commands # intents是要求機器人的權限 intents = discord.Intents.all() # command_prefix是前綴符號,可以自由選擇($, #, &...) bot = commands.Bot(command_prefix = "%", intents = intents) @bot.event # 當機器人完成啟動 async def on_ready(): print(f"目前登入身份 --> {bot.user}") @bot.command() # 輸入%Hello呼叫指令 async def Hello(ctx): # 回覆Hello, world! await ctx.send("Hello, world!") bot.run("機器人的TOKEN") ``` 4. 如果終端機顯示「登入身分 --> 機器人名字」和看見 Discord 成員列表有顯示機器人上線,代表以上步驟都做對。 ![Picture_19](https://i.imgur.com/MMZKFFh.png) ![Picture_20](https://i.imgur.com/jEM3h3J.png) 5. 現在只要在頻道中使用關鍵字「Hello」或者「%Hello」,機器人就會回覆「Hello, world!」囉。 * 關鍵字 ![Picture_21](https://i.imgur.com/EC5iCom.png) * 前綴指令 ![Picture_22](https://i.imgur.com/fMe2Id8.png) --- 感謝各位看完整篇教學,希望大家都有成功製作出屬於自己的 Discord Bot,並且讓它能夠上線成功運作! 上面教學授權機器人的權限的部分,之所以都勾選,是為了要讓機器人運作的成功率提高,如果有時間或有興趣的人可以好好研究看看每個權限分別代表什麼。 此次教學中的兩個程式範例,如果合併起來會發現 Discord Bot 無法正常運作,那是因為關鍵字的 `on_message` 和前綴指令 `command` 互相抵觸到,要解決這個問題會使用到 Cog 工具,可參考這篇本作者撰寫的 [Python Discord Bot 進階教學 — Cog 篇](https://hackmd.io/@smallshawn95/python_discord_bot_cog)。 --- :::info 📢 **歡迎加入我的 Discord 伺服器** https://discord.gg/Jtd3eVrFJs ::: *Copyright © 2022 SmallShawn95. All rights reserved.*