【问题标题】:Use YouTube API in Discord Bot在 Discord Bot 中使用 YouTube API
【发布时间】:2020-12-08 05:41:03
【问题描述】:
我正在考虑一个 Discord Bot,如果您是使用 YouTube API 的 YouTube 频道的子用户,它会给您一个子角色,但我真的不知道如何在我的 Bot 中使用 YouTube API,所以我的问题是如何添加我的机器人的 YouTube 数据 API ?
【问题讨论】:
-
根据this article Youtube 订阅者数据无法这样获取。但是,在集成下的服务器设置中,可以选择同步订阅者并为其分配角色。值得研究。
标签:
youtube
youtube-api
bots
discord.py
【解决方案1】:
您实际上无法为服务器中的订阅者赋予角色,因为 discord 和 youtube 的数据库无论如何都没有连接,因此您无法获得作为特定 youtube 频道订阅者的 discord 用户。但是,您可以在机器人中使用 google-api-python-client 将其与 youtube 连接并获取有关特定频道的信息或搜索视频,您需要 youtube API key 才能这样做。
这是你如何编码它 -
from googleapiclient.discovery import build
@client.command()
async def ytsearch(ctx, query: str):
youtube = build("youtube", "v3", developerKey=YT_Key)
search_response = youtube.search().list(q=<query>, part="id,snippet", maxResults=5).execute()
await ctx.send(search_response) # It will send the data in a .json format.