Skip to content

Commit

Permalink
Handle invalid twitch oauth token
Browse files Browse the repository at this point in the history
  • Loading branch information
tisboyo committed Oct 24, 2020
1 parent e71ac17 commit f535569
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"python.linting.pylintEnabled": false,
"python.linting.flake8Enabled": true,
"python.linting.enabled": true,
"python.formatting.blackPath": "${env:HOME}/.local/bin/black"
"python.formatting.blackPath": "${workspaceFolder}/.venv/bin/black"
}
},
},
}
}
38 changes: 35 additions & 3 deletions cogs/twitch.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import discord
from discord.ext import commands

from keys import error_channel_webhook
from keys import twitch as twitch_settings
from util.database import Database
from util.permissions import Permissions
Expand Down Expand Up @@ -287,8 +288,16 @@ async def get_twitch_status():
logger.info("Waiting for on_ready")
await asyncio.sleep(1)

first_loop = True

while True:
try:
# Skips the sleep cycle for the first loop when the bot runs.
if first_loop:
first_loop = False
else:
await asyncio.sleep(300) # 300 = 5 Minutes

logger.debug("Starting Twitch status retrieval")
streams_params = {"user_login": list(Twitch.streamers.keys())}

Expand All @@ -298,8 +307,33 @@ async def get_twitch_status():
async with aiohttp.ClientSession() as cs:
async with cs.get(streams_url, params=streams_params, headers=Twitch.headers) as r:
streams_data = await r.json()
streams_data = streams_data["data"]

try:
streams_data = streams_data["data"]
except KeyError:
if streams_data.get("error", False):
if streams_data["message"] == "Invalid OAuth token":
error_message = "<@219518082266300417> Invalid Twitch Oauth token!!"
else:
error_message = f"<@219518082266300417> Twitch: {streams_data['message']}"

logger.warning(f"{streams_data['status']} : {streams_data['message']}")
else:
logger.warning("Unknown KeyError")
error_message = "<@219518082266300417> Unknown KeyError in cogs.twitch.get_twitch_status"

# Send error to discord channel
if error_channel_webhook is not None:
async with aiohttp.ClientSession() as session:
# Not catching the response, because if it errors it doesn't matter
await session.post(
error_channel_webhook,
json={"content": error_message},
)
else:
logger.warning("error_channel_webhook is not set")

continue
# If we haven't gotten the profile pictures in the last hour, grab them
if Twitch.profile_update > (datetime.now() + timedelta(hours=1)):
await Twitch.get_twitch_profiles()
Expand Down Expand Up @@ -343,8 +377,6 @@ async def get_twitch_status():
# Save what time the next run will be, used in the list command
Twitch.next_live_query = datetime.now(tz=timezone.utc) + timedelta(seconds=300)

await asyncio.sleep(300) # 300 = 5 Minutes

except aiohttp.client_exceptions.ClientConnectionError:
logger.warning("Twitch connection error.")
await asyncio.sleep(300)
Expand Down
2 changes: 2 additions & 0 deletions keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,5 @@
# OAuth Key
key=os.getenv("twitch_key"),
)

error_channel_webhook = os.getenv("error_webhook")

0 comments on commit f535569

Please sign in to comment.