Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Discord Bot Junior Tutorial@HackerSir 11th
Search
YUKAI
May 30, 2025
Education
43
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Discord Bot Junior Tutorial@HackerSir 11th
YUKAI
May 30, 2025
More Decks by YUKAI
See All by YUKAI
WebAssembly 入門:在 Browser 上跑 C Code 是怎麼回事@HackerSir StudyGroup
yukai0xe
0
66
Docker Escape@HackerSir StudyGroup
yukai0xe
0
86
How to use Docker@HackerSir 11th
yukai0xe
0
69
OAuth2.0 Easy Talk@HackerSir StudyGroup
yukai0xe
0
65
XSS Everywhere@HackerSir 11th
yukai0xe
0
71
不要小看我的情報網_上吧網路爬蟲!!@HackerSir 11th
yukai0xe
0
63
Other Decks in Education
See All in Education
[2026前期火5] 論理学(京都大学文学部 前期 第5回)「 ならばの問題演習・proof net・かつの規則」
yatabe
0
420
2026年度春学期 統計学 第13回 不確かな測定の不確かさを測る ― 不偏分散とt分布 (2026. 6. 25)
akiraasano
PRO
1
180
AI-Based Speaking Assessment of a Short-Term Study Abroad Program
uranoken
0
480
遊ぶかね欲しさの犯行(ルビ:労働)です
shirayanagiryuji
0
260
Carbohydrates: Classification, Structure & Chemical Properties
pawan5505
0
190
学生のうちに考えておきたい信頼の話
suisan
1
770
[2026前期火5] 論理学(京都大学文学部 前期 第9回)「正規化の停止性——ヒドラゲームによる証明」
yatabe
0
270
Throw Yourself In! - How I've learned English and What I'm Facing
georgeorge
1
260
第2部-高校生とAI活用/high-school2026-2
okana2ki
0
150
チームの鏡になるー自分の癖を知ると、チームのパターンが見えてくる@スクフェス仙台
saorimurooka
0
170
25-26 CBT Library top items
cbtlibrary
0
200
Lectura 1 (PIT : Python Basico)
robintux
0
1.1k
Featured
See All Featured
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
3k
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.7k
Principles of Awesome APIs and How to Build Them.
keavy
128
18k
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
11k
Bootstrapping a Software Product
garrettdimon
PRO
306
120k
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
650
How to Ace a Technical Interview
jacobian
281
24k
How GitHub (no longer) Works
holman
316
150k
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
510
Become a Pro
speakerdeck
PRO
31
6.2k
4 Signs Your Business is Dying
shpigford
187
23k
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.5k
Transcript
Discord Bot 給我安排安排 講者:YuKAi
1. 安裝 Python 3.10 2. 配置 3.10 的虛擬環境 3. 安裝
discord 套件 4. 有空就往後翻把 ffmpeg 和 yt-dlp 套件安裝好 ※ 如果你把 discord bot 申請好了那就更棒了 Before Class
• 下載連結 安裝 Python 3.10
1. 打開 VSCode 2. 開啟一個資料夾 3. Ctrl+Shift+P 開啟命令列 4. 輸入
>Python 5. 點擊 Python: Create Environment 配置 3.10 的虛擬環境
1. 選擇 Venv 2. 選擇 Python 版本(3.10.0) 配置 3.10 的虛擬環境
1. 成功建立後會有一個 .venv 資料夾 2. 接著打開 VSCode Terminal (快捷鍵 Ctrl
+ Shift + `) 配置 3.10 的虛擬環境
• 輸入 .\.venv\Scripts\activate • 這樣就會切換到 3.10 version 的虛擬環境(前面有 .venv 標記)
配置 3.10 的虛擬環境
Discord Bot 申請 01 ㄟ晚餐吃什麼 02 ㄟ要聽什麼 03 Outline
Discord Bot 申請 01
• 點擊 New Application • 輸入你的 Application 名稱 Step 1:前往
Discord Developers
• 點擊左側欄位的 Bot • 畫面會變成像右邊那樣 Step 2:新增一個機器人
• 往下滑,將 Privileged Gateway Intents 的三個設定都打開 Step 3:開啟權限和設定
Step 4:邀請機器人進 Discord • 點擊左側欄位的 OAuth2 • 畫面會變成像右邊那樣
Step 4:邀請機器人進 Discord • 將 SCOPES 的 BOT 勾選 •
將 BOT PERMISSIONS 的 Administrator 勾選
Step 4:邀請機器人進 Discord • 滑到最下面,複製邀請的 URL
Step 4:邀請機器人進 Discord • 選擇伺服器將機器人加進 Discord • 授權就勾選就好
成功畫面 • 會有機器人加入訊息,旁邊可以看到機器人是離線狀態
ㄟ 晚餐要吃什麼 02
點 Reset Token,複製 Token 加入程式 加入 Bot Token
• 在 terminal 輸入 • 這樣才能成功引入 discord 套件 安裝套件 discord
pip install discord
1. 2. 3. 4. 5. 6. 7. 8. 9. 10.
11. 12. 13. 14. import discord # 引入 discord library from discord.ext.commands import Bot # 引入 Bot intents = discord.Intents.default() # 設定機器人權限 intents.message_content = True # 機器人可以接收訊息 # 實例化一個 Discord Bot bot = Bot(intents = intents, # 權限配置 description="HackerSir Bot", # 機器人的說明 command_prefix="!") # 指令前綴 TOKEN = "your-bot-token" bot.run(TOKEN) # 啟用機器人(這行放在程式碼最後) 實例化一個機器人
1. 2. 3. 4. 5. 6. 7. 8. CHANNEL_ID =
"your-channel-id" @bot.event async def on_ready(): channel = await bot.fetch_channel(CHANNEL_ID) await channel.send("Meow~OuO") 機器人上線發個聲 機器人事件(on_ready):當準備好的時候執行以下程式
1. 2. 3. 4. 5. 6. 7. 8. CHANNEL_ID =
"your-channel-id" @bot.event async def on_ready(): channel = await bot.fetch_channel(CHANNEL_ID) await channel.send("Meow~OuO") 機器人上線發個聲 @bot.event:機器人事件 async await:等待程式執行完,繼續下一的程式
1. 2. 3. 4. 5. 6. 7. 8. @bot.event async
def on_message(message: discord.Message): if message.author == bot.user: # 如果發話的人是機器人就結束函式 return if message.content == "Hello": await message.channel.send("Hello, world!") 機器人回話 機器人事件(on_message):當收到訊息時執行以下程式 discord.Message 參考文件
1. 2. 3. 4. 5. 6. 7. 8. 9. 10.
11. 12. 13. 14. @bot.event async def on_message(message: discord.Message): if message.author == bot.user: # 如果發話的人是機器人就結束函式 return if message.content == "Hello": await message.channel.send("Hello, world!") else: await message.channel.send(message.content) 複讀機
1. 2. 3. 4. 5. 6. 7. 8. 9. 10.
11. 12. 13. 14. @bot.event async def on_message(message: discord.Message): if message.author == bot.user: return if message.content == "ㄟ晚餐吃什麼": dinner = ["老麥", "肯德基", "小品屋", "好而大", "好大一盤", "不要吃啊"] await message.channel.send(dinner[random.randint(0, len(dinner) - 1)]) ㄟ晚餐吃什麼
• on_typing:當使用者開始打字時 • on_connect:當機器人連接時 • on_disconnect:當機器人斷開時 • on_member_join:當成員加入時 • 更多事件
其他事件
ㄟ要聽什麼 03 圖源
• 在 terminal 輸入 • What is yt-dlp 安裝套件 yt-dlp
pip install yt-dlp
下載 ffmpeg • 下載連結
複製 ffmpeg 路徑 1. 下載完解壓縮 2. 一路點到 bin 的資料夾 3.
點擊上面的路徑欄 4. 複製路徑
設定環境變數 1. 搜尋環境變數 2. 點擊進階 3. 點擊環境變數
設定環境變數 1. 點選系統變數內 Path 2. 按下編輯
設定環境變數 1. 將剛剛複製的路徑貼上 2. 最後按下確定
設定環境變數 1. 打開 cmd 2. 輸入 ffmpeg, 如右圖就是設定成功
1. 2. 3. 4. 5. 6. 7. 8. from discord.ext.commands
import Context @bot.command(name="dinner") async def dinner(ctx: Context): dinner = ["老麥", "肯德基", "小品屋", "好而大", "好大一盤", "不要吃啊"] await ctx.send(dinner[random.randint(0, len(dinner) - 1)]) 怎麼使用指令 @bot.command:使用機器人指令 通常裡面的第一個參數會是 Context
1. 2. 3. 4. 5. 6. 7. 8. 9. 10.
11. 12. 13. 14. from discord.ext.commands import Context @bot.event async def on_message(message: discord.Message): ... await bot.process_commands(message) # 要加這行,機器人才會當作指令處理 @bot.command(name="dinner") async def dinner(ctx: Context): dinner = ["老麥", "肯德基", "小品屋", "好而大", "好大一盤", "不要吃啊"] await ctx.send(dinner[random.randint(0, len(dinner) - 1)]) 注意:事件與指令同時使用
效果比較
1. 播放音樂(play) 2. 暫停音樂(pause) 3. 終止音樂(stop) 4. 恢復播放(resume) 練習更多指令
播放音樂 1. 確認使用者是否在 voice channel 2. 解析 yt 網址的 音源
3. ffmpeg 設定音源並播放
1. 2. 3. 4. 5. 6. 7. 8. from discord.ext.commands
import Context @bot.command(name="play") async def play(ctx: Context, *, link): if ctx.author.voice is None: await ctx.send("你不在語音頻道內!") return 確認使用者是否在 voice channel 播放音樂前,檢查發話的人是否在語音頻道內
1. 2. 3. 4. 5. 6. 7. 8. 9. 10.
11. 12. 13. 14. from discord.ext.commands import Context voice_clients = {} @bot.command(name="play") async def play(ctx: Context, *, link): ... try: # 如果發話的人在語音頻道內就連接發話人的頻道 voice_client = await ctx.author.voice.channel.connect() voice_clients[ctx.guild.id] = voice_client except Exception as e: print(e) 確認使用者是否在 voice channel
1. 2. 3. 4. 5. 6. 7. 8. import yt_dlp
yt_dl_options = {"format": "bestaudio/best"} ytdl = yt_dlp.YoutubeDL(yt_dl_options) 解析 yt 網址的音源 設定解析 youtube 網址的參數
1. 2. 3. 4. 5. 6. 7. 8. 9. 10.
11. 12. 13. 14. @bot.command(name="play") async def play(ctx: Context, *, link): ... try: data = ytdl.extract_info(link, download=False) # ytdl 解析連結 print(data) except Exception as e: print(e) 解析 yt 網址的音源
1. 2. 3. 4. 5. 6. 7. 8. 9. 10.
11. 12. 13. 14. import json @bot.command(name="play") async def play(ctx: Context, *, link): ... try: data = ytdl.extract_info(link, download=False) # ytdl 解析連結 with open('output.json', 'w') as f: json.dump(data, f, indent=2) except Exception as e: print(e) Data 太多 → json 輸出
Data 太多 → json 輸出
1. 2. 3. 4. 5. 6. 7. 8. 9. 10.
11. 12. 13. 14. @bot.command(name="play") async def play(ctx: Context, *, link): ... try: ... song = data['url'] ffmpeg_options = {'before_options': '-reconnect 1 -reconnect_streamed 1 - reconnect_delay_max 5','options': '-vn -filter:a "volume=0.25"'} song = discord.FFmpegOpusAudio(song, **ffmpeg_options) voice_clients[ctx.guild.id].play(song) except Exception as e: print(e) FFmpeg 設定音源並播放
效果呈現
1. 2. 3. 4. 5. 6. 7. 8. @bot.command(name="pause") async
def pause(ctx: Context): try: voice_clients[ctx.guild.id].pause() except Exception as e: print(e) 暫停音樂 用 pause() 暫停即可
1. 2. 3. 4. 5. 6. 7. 8. @bot.command(name="stop") async
def stop(ctx: Context): try: voice_clients[ctx.guild.id].stop() except Exception as e: print(e) 終止音樂 用 stop() 終止即可
1. 2. 3. 4. 5. 6. 7. 8. @bot.command(name="resume") async
def resume(ctx: Context): try: voice_clients[ctx.guild.id].resume() except Exception as e: print(e) 恢復播放 用 resume() 恢復播放即可
完整程式碼連結 完整程式碼
None