mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-21 18:27:59 -05:00
Audio Fixes (#4492)
* handles #4491 * add typing indicators to audio playlists commands like discussed with aika. * recheck perms upon change of token to avoid needing a reload. * Ensure the player lock is always released... on rewrite to this as a callback to the task. * ffs * resolves#4495 * missed one * aaaaaaaaa * fix https://canary.discord.com/channels/133049272517001216/387398816317440000/766711707921678396 * some tweaks * Clear errors to users around YouTube Quota
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import json
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
from typing import TYPE_CHECKING, Mapping, Optional, Union
|
||||
|
||||
@@ -8,6 +9,7 @@ import aiohttp
|
||||
from redbot.core import Config
|
||||
from redbot.core.bot import Red
|
||||
from redbot.core.commands import Cog
|
||||
from redbot.core.i18n import Translator
|
||||
|
||||
from ..errors import YouTubeApiError
|
||||
|
||||
@@ -15,7 +17,7 @@ if TYPE_CHECKING:
|
||||
from .. import Audio
|
||||
|
||||
log = logging.getLogger("red.cogs.Audio.api.YouTube")
|
||||
|
||||
_ = Translator("Audio", Path(__file__))
|
||||
SEARCH_ENDPOINT = "https://www.googleapis.com/youtube/v3/search"
|
||||
|
||||
|
||||
@@ -32,7 +34,7 @@ class YouTubeWrapper:
|
||||
self._token: Mapping[str, str] = {}
|
||||
self.cog = cog
|
||||
|
||||
def update_token(self, new_token: Mapping[str, str]):
|
||||
async def update_token(self, new_token: Mapping[str, str]):
|
||||
self._token = new_token
|
||||
|
||||
async def _get_api_key(
|
||||
@@ -54,11 +56,28 @@ class YouTubeWrapper:
|
||||
"type": "video",
|
||||
}
|
||||
async with self.session.request("GET", SEARCH_ENDPOINT, params=params) as r:
|
||||
if r.status in [400, 404]:
|
||||
if r.status == 400:
|
||||
if r.reason == "Bad Request":
|
||||
raise YouTubeApiError(
|
||||
_(
|
||||
"Your YouTube Data API token is invalid.\n"
|
||||
"Check the YouTube API key again and follow the instructions "
|
||||
"at `{prefix}audioset youtubeapi`."
|
||||
)
|
||||
)
|
||||
return None
|
||||
elif r.status in [403, 429]:
|
||||
if r.reason == "quotaExceeded":
|
||||
raise YouTubeApiError("Your YouTube Data API quota has been reached.")
|
||||
elif r.status == 404:
|
||||
return None
|
||||
elif r.status == 403:
|
||||
if r.reason in ["Forbidden", "quotaExceeded"]:
|
||||
raise YouTubeApiError(
|
||||
_(
|
||||
"YouTube API error code: 403\nYour YouTube API key may have "
|
||||
"reached the account's query limit for today. Please check "
|
||||
"<https://developers.google.com/youtube/v3/getting-started#quota> "
|
||||
"for more information."
|
||||
)
|
||||
)
|
||||
return None
|
||||
else:
|
||||
search_response = await r.json(loads=json.loads)
|
||||
|
||||
Reference in New Issue
Block a user