# 第三堂社課 ## 開始製作discord bot --- 記得點名 還有繳社費 ![](https://hackmd.io/_uploads/ByDFY5OGa.png =38%x) --- ## 前置作業 ### 0. 先加入這個測試用伺服器 https://discord.gg/X4CwSV9J ![](https://hackmd.io/_uploads/ryxJDqOzT.png) ### 1.申請discord帳號 https://discord.com/ ### 2.進入discord developer https://discord.com/developers/docs/intro ![](https://hackmd.io/_uploads/ry_ByFrD3.jpg =50%x) ### 3.建立機器人 點選-->Application-->New Application ![](https://hackmd.io/_uploads/H1XlbYrvn.jpg =50%x) ![](https://hackmd.io/_uploads/SJ92xYBD2.jpg =50%x) 輸入你的bot名稱 Creat ![](https://hackmd.io/_uploads/B1KkMFBPn.jpg =50%x) ### 4.將機器人邀請到伺服器 到側邊面板-->Bot往下滑 把Authorization Flow裡的選項都關掉 記得要按save change ![](https://hackmd.io/_uploads/HyEoG9Hv2.jpg =50%x) 再往下滑 把Privileged Gateway Intents裡的選項都打開 一樣要save change ![](https://hackmd.io/_uploads/BJDPrqHwh.jpg =50%x) ![](https://hackmd.io/_uploads/BJ4Km9Hwn.jpg =50%x) ![](https://hackmd.io/_uploads/B1Ypr9rv3.jpg =50%x) ![](https://hackmd.io/_uploads/HJvLrqrvh.jpg =50%x) 到側邊面板-->OAuth2-->URL Generator ![](https://hackmd.io/_uploads/H1iTI9Bwh.jpg =50%x) SCOPES選bot和applications.commands就好 ![](https://hackmd.io/_uploads/HkXUYcHv2.jpg =50%x) bot permissions選Administrator ![](https://hackmd.io/_uploads/SJV3Y5Bw3.jpg =50%x) 最後滑到最下面 複製這串網站貼到瀏覽器上 ![](https://hackmd.io/_uploads/r1iE5cBwh.jpg =50%x) 打開頁面後會有這個畫面 選擇機器人要加入的伺服器後按continue ![](https://hackmd.io/_uploads/S1aKocHvh.jpg =50%x) 授權確認後就加入你的伺服器了 ![](https://hackmd.io/_uploads/H17b2cBw2.jpg =50%x) ![](https://hackmd.io/_uploads/ByQJp5rDh.jpg =50%x) >##### 可以到伺服器成員列表確認是否有成功加入 ### 5.安裝Python或ide ide可以用VScode或pycharm 可以回去看上一次的講義 :::warning Python記得要裝3.10.11版的,之後的版本有bug ::: ### 6.取得token :::info token就是機器人的密碼 有token才能控制機器人 ::: >往下滑可以看到token這個欄位 ![](https://hackmd.io/_uploads/SJbhXYSvh.jpg =50%x) >按下去就能得到token >先複製起來 >如果之後忘記了就只能再reset :::danger 請保管好token 任何人有你的token就能操縱你的機器人 ::: --- ## 開始寫機器人 ### 1. nextcord.py 和 discord.py nextcord.py和discord.py都是dc bot會需要的函式庫 但是discord.py似乎已經沒有更新了 所以由nextcord來接續開發 我也推薦使用nextcord :::spoiler 使用方式 >在terminal輸入pip install nextcord.py >接著在使用前import >```python= >import nextcord >from nextcord.ext import commands >``` >>commands是等一下建立指令會用到的東西 ::: ### 2.讓你的機器人上線 ```python= import nextcord from nextcord.ext import commands # intents是要求機器人的權限 intents = nextcord.Intents.all() bot = commands.Bot(intents=intents) #你的token TOKEN='Your token' #bot開機 @bot.event async def on_ready(): print(f"目前登入身份:{bot.user}") #執行 bot.run(TOKEN) ``` #### intents >##### 機器人的權限 #### TOKEN >##### 你的token >記得用' '包住 #### async >##### 異步執行 > >因為一般程式都是由上而下執行 >用這東西可以讓程式同時做很多事 #### on_ready() >##### 機器人開機 #### 啟動程式後你的機器人就上線了 ![](https://hackmd.io/_uploads/r1byl6rP3.png) ![](https://hackmd.io/_uploads/SJ-yxTHvn.png) ### 3.基礎指令 #### 指令種類 >Discord Bot 指令大致分為三種方式: >* 關鍵字 - Hello, ping, say >* 前綴符號指令 - $Hello, %ping, &say >* 斜線指令 - /Hello, /ping, /say > >三種方式的程式碼撰寫都不太一樣,下面是使用前綴符號指令作為範例 #### 範例 ```python= import nextcord from nextcord.ext import commands # intents是要求機器人的權限 intents = nextcord.Intents.all() # command_prefix是前綴符號,可自由選擇($, #, &...) bot = commands.Bot(command_prefix = "%", intents = intents) TOKEN='你的token' @bot.event async def on_ready(): print(f"目前登入身份:{bot.user}") #建立指令 @bot.command() async def Hello(ctx): # 回覆Hello, world! await ctx.send("Hello, world!") bot.run(TOKEN) ``` #### 結果 ![](https://hackmd.io/_uploads/S1wPUaBv2.png =90%x) --- ## 本週作業 : 用dcbot寫出簡易計算機 >不懂的東西請善用goole >答案我上傳了 ![](https://hackmd.io/_uploads/rJtCXqdGa.png) ## 參考 大補帖 https://hackmd.io/@gtcoding/discord#PYTHON-and-Discord-Bot-Tutorial https://hackmd.io/@smallshawn95/python_discord_bot_base