Commit bbfbd180 authored by Daniel Preston's avatar Daniel Preston
Browse files

Handle json decode error in reddit command

parent 28ac408b
Pipeline #374095903 passed with stages
in 2 minutes and 27 seconds
......@@ -7,6 +7,7 @@
- Change command list MD file location.
- Replace documentation files with new, reworked content.
- Buff raffle currency rewards and guarantee recipe items.
- Fixed rate limits causing `reddit` to break.
## 4.91.1820 Fubuki
......
beta: false
build_date: 1631823710
build_date: 1632180239
codename: Fubuki
version:
major: 4
minor: 94
patch: 1888
patch: 1891
......@@ -132,7 +132,7 @@ class SigmaCommand(object):
Adds a log line when a command is used.
:type message: discord.Message
:type args: list[str]
:type extime: int
:type extime: float
"""
crst = arrow.get(message.created_at).float_timestamp
exdiff = round(extime - crst, 3)
......@@ -234,7 +234,7 @@ class SigmaCommand(object):
Adds detailed statistics for command usage.
:param pld:
:type pld: sigma.core.mechanics.payload.CommandPayload
:type exec_timestamp: int
:type exec_timestamp: float
"""
cmd_stat = CommandStatistic(self.db, self, pld)
cmd_stat.exec_timestamp = exec_timestamp
......
......@@ -17,7 +17,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import json
from json.decoder import JSONDecodeError
import aiohttp
import discord
......@@ -38,7 +37,7 @@ async def catfact(_cmd, pld):
data = await data.read()
try:
fact = json.loads(data).get('fact')
except JSONDecodeError:
except json.JSONDecodeError:
fact = None
if fact:
response = discord.Embed(color=0xFFDC5D)
......
......@@ -71,7 +71,7 @@ async def wfpricecheck(_cmd, pld):
page_data = await data.read()
try:
data = json.loads(page_data)
except json.decoder.JSONDecodeError:
except json.JSONDecodeError:
data = None
if data:
if not data.get('error'):
......
......@@ -39,7 +39,7 @@ async def pythonpackage(_cmd, pld):
package_url = f'https://pypi.org/pypi/{lookup}/json'
try:
package_data = await aioget(package_url, True)
except json.decoder.JSONDecodeError:
except json.JSONDecodeError:
package_data = None
if package_data:
cdat = package_data.get('info')
......
......@@ -64,7 +64,10 @@ class RedditClient(object):
"""
async with aiohttp.ClientSession() as session:
async with session.get(url, headers=self.headers) as data_response:
data = json.loads(await data_response.read())
try:
data = json.loads(await data_response.read())
except json.JSONDecodeError:
data = {}
return data
async def get_subreddit(self, subreddit):
......@@ -74,7 +77,7 @@ class RedditClient(object):
"""
sub_about_url = f'{reddit_base}/{subreddit}/about.json'
sub_about_data = await self.__get_data(sub_about_url)
return RedditSub(sub_about_data)
return RedditSub(sub_about_data) if sub_about_data else None
async def get_posts(self, subreddit, listing):
"""
......
......@@ -88,29 +88,33 @@ async def reddit(cmd, pld):
subreddit = pld.args[0]
argument = pld.args[-1].lower()
subreddit = await reddit_client.get_subreddit(subreddit)
if not subreddit.private and not subreddit.banned:
# noinspection PyTypeChecker
post = await grab_post(subreddit, argument)
if subreddit.exists:
if post:
if not post.over_18 or pld.msg.channel.is_nsfw():
post_desc = f'Author: {post.author if post.author else "Anonymous"}'
post_desc += f' | Karma Score: {post.score}'
author = f'https://www.reddit.com{post.permalink}'
response = discord.Embed(color=0xcee3f8, timestamp=arrow.get(post.created_utc).datetime)
response.set_author(name=f'r/{subreddit.display_name}', url=author, icon_url=reddit_icon)
response.description = post.title
response.set_footer(text=post_desc)
add_post_image(post, response)
if subreddit:
if not subreddit.private and not subreddit.banned:
# noinspection PyTypeChecker
post = await grab_post(subreddit, argument)
if subreddit.exists:
if post:
if not post.over_18 or pld.msg.channel.is_nsfw():
post_desc = f'Author: {post.author if post.author else "Anonymous"}'
post_desc += f' | Karma Score: {post.score}'
author = f'https://www.reddit.com{post.permalink}'
response = discord.Embed(color=0xcee3f8, timestamp=arrow.get(post.created_utc).datetime)
response.set_author(name=f'r/{subreddit.display_name}', url=author, icon_url=reddit_icon)
response.description = post.title
response.set_footer(text=post_desc)
add_post_image(post, response)
else:
response = GenericResponse('NSFW Subreddits and posts are not allowed here.').error()
else:
response = GenericResponse('NSFW Subreddits and posts are not allowed here.').error()
response = GenericResponse('That subreddit has no posts.').error()
else:
response = GenericResponse('That subreddit has no posts.').error()
response = GenericResponse('No such subreddit.').error()
else:
response = GenericResponse('No such subreddit.').error()
reason = 'banned' if subreddit.banned else 'private'
response = GenericResponse(f'That subreddit is {reason}.').error()
else:
reason = 'banned' if subreddit.banned else 'private'
response = GenericResponse(f'That subreddit is {reason}.').error()
response = GenericResponse('Failed to retrieve subreddit.').error()
response.set_footer(text='This is most likely due to a rate limit')
else:
response = GenericResponse('Nothing inputted.').error()
await pld.msg.channel.send(embed=response)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment