Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Discord Bot Junior Tutorial@HackerSir 11th

Discord Bot Junior Tutorial@HackerSir 11th

Avatar for YUKAI

YUKAI

May 30, 2025
Tweet

More Decks by YUKAI

Other Decks in Education

Transcript

  1. 1. 安裝 Python 3.10 2. 配置 3.10 的虛擬環境 3. 安裝

    discord 套件 4. 有空就往後翻把 ffmpeg 和 yt-dlp 套件安裝好 ※ 如果你把 discord bot 申請好了那就更棒了 Before Class
  2. 1. 打開 VSCode 2. 開啟一個資料夾 3. Ctrl+Shift+P 開啟命令列 4. 輸入

    >Python 5. 點擊 Python: Create Environment 配置 3.10 的虛擬環境
  3. Step 4:邀請機器人進 Discord • 將 SCOPES 的 BOT 勾選 •

    將 BOT PERMISSIONS 的 Administrator 勾選
  4. 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) # 啟用機器人(這行放在程式碼最後) 實例化一個機器人
  5. 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):當準備好的時候執行以下程式
  6. 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:等待程式執行完,繼續下一的程式
  7. 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 參考文件
  8. 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) 複讀機
  9. 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)]) ㄟ晚餐吃什麼
  10. 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
  11. 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)]) 注意:事件與指令同時使用
  12. 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 播放音樂前,檢查發話的人是否在語音頻道內
  13. 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
  14. 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 網址的參數
  15. 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 網址的音源
  16. 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 輸出
  17. 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 設定音源並播放
  18. 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() 暫停即可
  19. 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() 終止即可
  20. 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() 恢復播放即可