Skip to content

Commit dc976ca

Browse files
author
Simon Berger
committed
web failsafe
1 parent e1ef0b3 commit dc976ca

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

gisi/cogs/web.py

+21-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import logging
22
from io import BytesIO
33

4+
from aiohttp import ClientConnectorError
5+
from aiohttp.http_exceptions import InvalidURLError
46
from discord import File
57
from discord.ext.commands import command
68

@@ -16,18 +18,25 @@ def __init__(self, bot):
1618
@command()
1719
async def show(self, ctx, url: UrlConverter):
1820
async with ctx.typing():
19-
async with self.bot.webdriver as driver:
20-
await ctx.message.edit(content=f"heading to {url}...")
21-
await driver.get(url)
22-
await ctx.message.edit(content=f"taking screenshot...")
23-
im = await driver.get_screenshot()
24-
title = driver.title
25-
imdata = BytesIO()
26-
im.save(imdata, "png")
27-
imdata.seek(0)
28-
file = File(imdata, f"{title}.png")
29-
await ctx.send(f"{title} (<{url}>):", file=file)
30-
await ctx.message.delete()
21+
await ctx.message.edit(content=f"checking {url}...")
22+
try:
23+
async with self.bot.aiosession.head(url, allow_redirects=True) as resp:
24+
resp.raise_for_status()
25+
except (InvalidURLError, ClientConnectorError):
26+
await ctx.message.edit(content=f"<{url}> **isn't a valid url**")
27+
return
28+
29+
async with self.bot.webdriver as driver:
30+
await ctx.message.edit(content=f"heading to {url}...")
31+
await driver.get(url)
32+
await ctx.message.edit(content=f"taking screenshot...")
33+
im = await driver.get_screenshot()
34+
title = driver.title
35+
imdata = BytesIO()
36+
im.save(imdata, "png")
37+
imdata.seek(0)
38+
file = File(imdata, f"{title}.png")
39+
await ctx.message.edit(content=f"{title} (<{url}>):", file=file)
3140

3241

3342
def setup(bot):

scripts/run.bat

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ cd ..
22
call venv\scripts\activate
33
:run
44
echo STARTING
5-
run.py
5+
python run.py
66
goto run
77
pause

0 commit comments

Comments
 (0)