Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Discord Bot Junior Tutorial@HackerSir 11th
Search
YUKAI
May 30, 2025
Education
44
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
73
Docker Escape@HackerSir StudyGroup
yukai0xe
0
89
How to use Docker@HackerSir 11th
yukai0xe
0
70
OAuth2.0 Easy Talk@HackerSir StudyGroup
yukai0xe
0
79
XSS Everywhere@HackerSir 11th
yukai0xe
0
82
不要小看我的情報網_上吧網路爬蟲!!@HackerSir 11th
yukai0xe
0
70
Other Decks in Education
See All in Education
吉岡研究室紹介(2026年度)
kentaroy47
0
260
Sekolah Sabat - Triwulan 4 2026 - Pelajaran 2
hiola
0
9.8k
DecisionesEjecutivasCrisis
mariooc
0
420
子どものためのプログラミング道場『CoderDojo』〜法人提携例〜 / Partnership with CoderDojo Japan
coderdojojapan
PRO
4
19k
観察、仮説、実行、検証、計画、提案を一年で3000回トレーニングする方法/3000 Thinking Loops in 365 Days
moriyuya
5
1.4k
Science Tokyo University for International Research Excellence scheme_April 2026
sciencetokyo
PRO
0
420
マークシート試験のTeX言語を用いた自動採点について
doratex
0
5.4k
BRIEF OUTLINE OF IMPORTANT PLANT CONSTITUENTS
pawan_pharm
0
180
Web Architectures - Lecture 2 - Web Technologies (1019888BNR)
signer
PRO
0
3.7k
Adobe Express
matleenalaakso
2
8.4k
1007
cbtlibrary
0
120
医療事務養成の専門学校でのAI教育 非エンジニアの学生にAIを1年間使ってもらって、感じたこと
naochim2026
0
140
Featured
See All Featured
Bootstrapping a Software Product
garrettdimon
PRO
306
120k
Heart Work Chapter 1 - Part 1
lfama
PRO
10
37k
The #1 spot is gone: here's how to win anyway
tamaranovitovic
4
1.2k
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
910
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
2
930
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
550
Six Lessons from altMBA
skipperchong
29
4.5k
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
400
svc-hook: hooking system calls on ARM64 by binary rewriting
retrage
2
610
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
68
58k
The Mindset for Success: Future Career Progression
greggifford
PRO
0
530
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
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