1
1
import logging
2
2
from io import BytesIO
3
3
4
+ from aiohttp import ClientConnectorError
5
+ from aiohttp .http_exceptions import InvalidURLError
4
6
from discord import File
5
7
from discord .ext .commands import command
6
8
@@ -16,18 +18,25 @@ def __init__(self, bot):
16
18
@command ()
17
19
async def show (self , ctx , url : UrlConverter ):
18
20
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 )
31
40
32
41
33
42
def setup (bot ):
0 commit comments