# 第三堂社課
## 開始製作discord bot
---
記得點名
還有繳社費

---
## 前置作業
### 0. 先加入這個測試用伺服器
https://discord.gg/X4CwSV9J

### 1.申請discord帳號
https://discord.com/
### 2.進入discord developer
https://discord.com/developers/docs/intro

### 3.建立機器人
點選-->Application-->New Application


輸入你的bot名稱 Creat

### 4.將機器人邀請到伺服器
到側邊面板-->Bot往下滑
把Authorization Flow裡的選項都關掉
記得要按save change

再往下滑
把Privileged Gateway Intents裡的選項都打開
一樣要save change




到側邊面板-->OAuth2-->URL Generator

SCOPES選bot和applications.commands就好

bot permissions選Administrator

最後滑到最下面
複製這串網站貼到瀏覽器上

打開頁面後會有這個畫面
選擇機器人要加入的伺服器後按continue

授權確認後就加入你的伺服器了


>##### 可以到伺服器成員列表確認是否有成功加入
### 5.安裝Python或ide
ide可以用VScode或pycharm
可以回去看上一次的講義
:::warning
Python記得要裝3.10.11版的,之後的版本有bug
:::
### 6.取得token
:::info
token就是機器人的密碼
有token才能控制機器人
:::
>往下滑可以看到token這個欄位

>按下去就能得到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()
>##### 機器人開機
#### 啟動程式後你的機器人就上線了


### 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)
```
#### 結果

---
## 本週作業 : 用dcbot寫出簡易計算機
>不懂的東西請善用goole
>答案我上傳了

## 參考
大補帖
https://hackmd.io/@gtcoding/discord#PYTHON-and-Discord-Bot-Tutorial
https://hackmd.io/@smallshawn95/python_discord_bot_base