You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何借助YouTube API实现Discord机器人推送新视频嵌入消息?

嘿,我来帮你搞定YouTube API这块儿的核心操作!既然你已经准备好Discord消息的嵌入内容了,那咱们就聚焦在如何监控你的YouTube频道新视频并触发推送这部分,一步步来:

第一步:搞定YouTube API的基础配置

首先得拿到合法的API权限,这是一切的前提:

  • 去Google Cloud控制台创建一个新项目,搜索「YouTube Data API v3」并启用它。
  • 创建一个API密钥,记得给这个密钥加个使用限制(比如只允许调用YouTube Data API),防止被滥用浪费配额。
第二步:监控新视频的两种方案

方案1:轮询上传列表(新手友好,快速上手)

这种方式就是定期去查你频道的「上传」播放列表,对比最新视频ID,发现新的就推送。原理简单,不需要额外服务器资源,适合新手。

下面是用Python的discord.py和Google官方API客户端实现的示例代码,你可以直接替换自己的配置:

import discord
from googleapiclient.discovery import build
import asyncio

# 替换成你自己的配置
DISCORD_BOT_TOKEN = "你的Discord机器人令牌"
YOUTUBE_API_KEY = "你的YouTube API密钥"
YOUTUBE_CHANNEL_ID = "你的YouTube频道ID"
TARGET_DISCORD_CHANNEL = 1234567890  # 要推送消息的Discord频道ID

# 初始化客户端
youtube_client = build('youtube', 'v3', developerKey=YOUTUBE_API_KEY)
discord_client = discord.Client(intents=discord.Intents.default())

# 存储上次记录的最新视频ID,避免重复推送
latest_video_id = None

async def check_for_new_videos():
    global latest_video_id
    await discord_client.wait_until_ready()
    target_channel = discord_client.get_channel(TARGET_DISCORD_CHANNEL)
    
    while not discord_client.is_closed():
        # 第一步:获取频道的「上传」播放列表ID
        channel_info = youtube_client.channels().list(
            part='contentDetails',
            id=YOUTUBE_CHANNEL_ID
        ).execute()
        uploads_playlist_id = channel_info['items'][0]['contentDetails']['relatedPlaylists']['uploads']
        
        # 第二步:拉取播放列表的最新一条视频
        latest_video = youtube_client.playlistItems().list(
            part='snippet',
            playlistId=uploads_playlist_id,
            maxResults=1
        ).execute()
        
        if latest_video['items']:
            current_video_id = latest_video['items'][0]['snippet']['resourceId']['videoId']
            # 如果是新视频,就推送你准备好的嵌入内容
            if current_video_id != latest_video_id:
                video_link = f"https://www.youtube.com/watch?v={current_video_id}"
                # 这里替换成你已经准备好的Embed内容
                push_embed = discord.Embed(
                    title=latest_video['items'][0]['snippet']['title'],
                    url=video_link,
                    description=latest_video['items'][0]['snippet']['description'],
                    color=discord.Color.blue()
                )
                push_embed.set_thumbnail(url=latest_video['items'][0]['snippet']['thumbnails']['high']['url'])
                await target_channel.send(embed=push_embed)
                latest_video_id = current_video_id
        
        # 每隔10分钟检查一次(可以根据需求调整,比如5分钟=300秒)
        await asyncio.sleep(600)

@discord_client.event
async def on_ready():
    print(f"机器人已登录:{discord_client.user}")
    # 启动定时检查任务
    discord_client.loop.create_task(check_for_new_videos())

discord_client.run(DISCORD_BOT_TOKEN)

方案2:Webhook实时通知(高效进阶)

如果觉得轮询不够实时,可以用YouTube的PubSubHubbub服务订阅频道更新——当你发布新视频时,Google会主动把通知发送到你的公网服务器端点,触发机器人推送。不过这个需要你有一个公网可访问的服务器来接收Webhook请求,新手可以先从轮询方案入手,熟悉之后再尝试这种更高效的方式。

第三步:几个关键注意点
  • API配额限制:YouTube Data API有免费额度,每天10000单位配额。上面的轮询方案每次调用只消耗1单位,完全够用,不用担心超配额。
  • Discord机器人权限:确保你的机器人在目标频道拥有发送消息和嵌入链接的权限,不然会发不出嵌入内容。
  • 频道ID获取:如果不知道自己的YouTube频道ID,可以在频道页面右键查看源代码,搜索channelId;或者用API的Channels.list接口,通过你的频道用户名来获取ID。

内容的提问来源于stack exchange,提问作者Skyton

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:45:11