---
tags: 資訊之芽
---
<style type="text/css">
@keyframes polish {
0% {
color: #7a7a7a;
}
100% {
color: #3a3a3a;
}
}
.warning {
color: #ff0000;
}
#nav-bar {
width: 100%;
height: 5vh;
background-color: #f7f7f7;
display: flex;
border-radius: 5px;
}
#nav-item {
color: #3a3a3a;
font-weight: bold;
font-size: 2vh;
line-height: 5vh;
width: 25%;
text-align: center;
}
#inner-link {
text-decoration: none;
color: #7a7a7a;
}
#inner-link:hover {
animation: polish .1s linear forwards;
}
.weak_hint {
color: #7a7a7a;
}
.weak_hint:hover {
animation: polish .1s linear forwards;
}
.logo {
display: none;
}
@media (min-width: 1200px) {
.logo {
display: block;
position: fixed;
right: 10vw;
bottom: 20px;
}
}
</style>
# 2025 資訊之芽 py 班 第二次大作業 - Discord Bot
Reference: 2024 資訊之芽 py 班 第二次大作業
## Links
- [作業程式模板連結](https://github.com/racercode/discord-bot-tutorial)
- [分組表單連結](https://forms.gle/PMtPSYBkxVbHBpVx6)
- [作業繳交表單連結](https://forms.gle/fhwNMzyTEjMHQhW39)
- [GEMINI API 文檔](https://ai.google.dev/api?lang=python)
- [GEMINI APIKEY 申請](https://ai.google.dev/gemini-api/docs/api-key)
- [Google cloud Project](https://cloud.google.com/cloud-console?utm_source=google&utm_medium=cpc&utm_campaign=japac-TW-all-zh-dr-BKWS-all-lv-trial-PHR-dr-1710102&utm_content=text-ad-none-none-DEV_c-CRE_622018104153-ADGP_Hybrid+%7C+BKWS+-+BRO+%7C+Txt+-Management+Tools-Cloud+Console-cloud+console-main-KWID_43700076521325397-kwd-2007301366065&userloc_9197990-network_g&utm_term=KW_console+cloud+google+coom&gad_source=1&gclid=Cj0KCQjwoNzABhDbARIsALfY8VPIxHvf8qZ83qi-ngh2JSzSjK3BP2R0z8638MJ6E89odiJnu8Hhl90aAgKdEALw_wcB&gclsrc=aw.ds&hl=zh_tw)
- [Discord Bot 申請連結](https://discord.com/developers/applications)
---
## 作業目標
* 跟夥伴合作 !
* 實作一個 Discord Bot
* 使用者自建 Todo List
* Gemini 聊天機器人
* 自訂功能一
* 自訂功能二
* 發揮創意設計一些有趣的機器人 !
* 完成 Report 並在最後一周分享你的作品 !
## 作業要求
包含但不限於文字輸入和點擊按鈕,完成以下功能:
### 1. Todo List (20%)
幫一個有基本架構的 TodoList 加上功能
* 新增一個待辨事項 (4%)
* 刪除一個待辨事項 (4%)
* 清空所有待辨事項 (4%)
* 排序並輸出所有待辨事項 (4%)
* 可依照任何你喜歡的排序 ex: 日期、重要度、字典序等
* 美觀的介面 (使用 slash command 或 Embed 或 Button) (4%)
----
### 2. Gemini 聊天機器人 (20%)
幫一個有基本架構的 Gemini Bot 加上功能
1. Gemini 能夠單次回應使用者 (5%)
2. Gemini 能夠以聊天模式回應使用者 (有記錄功能) (5%)
3. Gemini 聊天模式擁有 System instruction (5%)
4. 美觀的介面 (使用 embed 或 button) (5%)
----
### 3. 自訂功能一 (20%)
* 指令一 (5%)
* 指令二 (5%)
* 指令三 (5%)
* 美觀的介面 (使用 slash command 或 Embed 或 Button) (5%)
----
### 4. 自訂功能二 (20%)
* 指令一 (5%)
* 指令二 (5%)
* 指令三 (5%)
* 美觀的介面 (使用 slash command 或 Embed 或 Button) (5%)
----
### Bonus - Music Bot (20%)
幫一個有基本架構的music bot加上功能
1. 讓使用者能夠透過樂曲名稱搜尋音樂 (5%)
2. 管理待播放清單 (10%)
* 加入歌曲
* 插歌
* 移除指定歌曲
* 列出清單
* 清空清單
3. 美觀的介面 (使用 slash command 或 Embed 或 Button) (5%)
----
### Coding Style (0%)
Coding Style 不計分,但有好的 Coding Style 可以讓你的組員更容易讀懂你的程式
* 優質易懂的變數名稱
* 詳細的註解
* 用函數or其他方法包裝功能
* PEP8
* Error Handling
----
### Report (10%)
* 解釋你的專案架構(5%)
(有哪些檔案、各代表什麼)
* 解釋你如何實作的 (5%)
(尤其是自訂功能的部分)
:::warning
請額外標註
* 每位成員負責的部分
* 每位組員的貢獻度 (全部組員合計貢獻度=10)
若貢獻度太低會斟酌扣分
:::
----
### 上臺分享 (10%)
* 每個組員都要上台
* 講師們評分(10%)
----
### 例子 - 玩 1A2B
* 開始玩
* 猜4個數字
* 結束時輸出猜測次數與排名
----
### 例子 - 玩終極密碼
* 開始玩
* 猜一個數字
* 結束時輸出猜測次數與排名
----
### 例子 - 玩成語接龍
* 從一個成語開始玩
* 電腦回答成語,使用者接續
* 使用者放棄
----
## 開始前的準備
### 申請 API KEY
- [GEMINI APIKEY 申請](https://ai.google.dev/gemini-api/docs/api-key)
- 先在 Google cloud 控制台建立一個新的 cloud project
- 申請 APIKEY 時選擇該 cloud project
:::warning
Gemini API KEY 會需要 Google 帳戶符合 18 歲以上。
:::
- [Discord Bot APIKEY 申請](https://discord.com/developers/applications)
----
### 作業模板
[Github Repo](https://github.com/racercode/discord-bot-tutorial)
可以直接把專案下載下來,也可以用上禮拜教過的 git 先 fork 這個專案再把它 clone 下來
----
- 安裝 discord Python 套件
- `python -m pip install discord --user` 或 `python3 -m pip install discord --user`
- 安裝 gemini Python 套件
- `python -m pip install google-genai --user` 或 `python3 -m pip install google-genai --user`
----
開啟開發者模式
Discord -> settings -> advanced -> developer mode


----
### 檔案內容
這份 Discord Bot 利用到了 [Cog](https://discordpy.readthedocs.io/en/stable/ext/commands/cogs.html) 來完成。
- `bot.py` 中包含了初始化所有 command 的檔案,並且包含 `load`, `reload`, `unload` command,也是程式的進入點。
- `core.py` 包含了所有 Bot 的 instance. 所有會用到 Bot 的檔案都會繼承這個 class
- `cmds/task.py` 是 TodoList 的模板。
- `cmds/chat.py` 是 Gemini 機器人 的模板。
- `cmds/feature1.py` 是自訂功能 1 的模板。
- `cmds/feature2.py` 是自訂功能 2 的模板。
- `cmds/music.py` 是 Bonus 的 music bot 的模板。
- `.default.env` 是環境變數的模板,請你將其改名為 `.env` 使用。可以自行添加其他所需環境變數。
- `.gitignore` 決定了需要不上傳到 Git Repo 的檔案。
----
## 繳交方式
### 分組
- [分組表單](https://forms.gle/PMtPSYBkxVbHBpVx6)
- 請在 5/18 23:59 前填寫完畢
- **1~3 人一組**
----
### DeadLine : 6/22 23:59
- [繳交連結](https://forms.gle/fhwNMzyTEjMHQhW39)
- 大家加油 ~
----
## Example
### Todo List Example
:::info
以下的作法只是範例,不用完全相同
但藍框框內的需求若沒有做到會斟酌扣分
:::
- `$AddTodoList <task_name> <description> <date> `
將 item(待辨事項) 加入 todo list
:::info
如果相同名字的待辦事項已經存在,需要提示錯誤訊息
:::
- `$RemoveTodoList <task_name>`
將 item(待辨事項) 移除 todo list
:::info
如果使用者想要移除掉不存在的待辦事項,需要提示錯誤訊息
:::
- `$ShowTodoList`
顯示 TodoList,並依照自己喜好輸出
- `$ClearTodoList`
清空 todo list
### Gemini 聊天機器人 Example
- `$Response <prompt>`
讓 Gemini 回應 `<prompt>`
- `$NewChat <system_instruction>`
建立新的對話,並且 `<system_instruction>` 代表先告訴 Bot 一些背景知識,如 `你是一個財務機器人`, `密碼是 12345678` 等等,請自由發揮創意。
:::info
* 要能夠記住過往的對話紀錄
* system_instruction 要有發揮效用
* 測試方式:
```
(system_instruction: 你是學生)
User: 早安
Bot: <Bot 的回應...>
User: 我剛才說什麼 ?
Bot: <剛才說早安...>
User: 你是誰
Bot: <學生...>
```
:::
- `$Chat <prompt>`
### 成果範例




