Fix joke command, aggregate requires .next not .first, returns raw json

This commit is contained in:
Zeva Rose 2021-08-05 18:13:50 -06:00
parent a222146152
commit b034f07b07

View file

@ -39,27 +39,27 @@ class JokeCog(commands.Cog):
{"$match": {"score": {"$gt": threshold}}}, {"$match": {"score": {"$gt": threshold}}},
{"$sample": {"size": 1}}, {"$sample": {"size": 1}},
] ]
result = Joke.objects().aggregate(pipeline).first() result = Joke.objects().aggregate(pipeline).next()
while result.body in ["[removed]", "[deleted]"]: while result["body"] in ["[removed]", "[deleted]"]:
result = Joke.objects().aggregate(pipeline).first() result = Joke.objects().aggregate(pipeline).next()
if result is None: if result is None:
await ctx.send( await ctx.send(
"Humor module failed. Please try again later.", hidden=True "Humor module failed. Please try again later.", hidden=True
) )
return return
emotes = re.findall(r"(&#x[a-fA-F0-9]*;)", result.body) emotes = re.findall(r"(&#x[a-fA-F0-9]*;)", result["body"])
for match in emotes: for match in emotes:
result.body = result.body.replace(match, html.unescape(match)) result["body"] = result["body"].replace(match, html.unescape(match))
emotes = re.findall(r"(&#x[a-fA-F0-9]*;)", result.title) emotes = re.findall(r"(&#x[a-fA-F0-9]*;)", result["title"])
for match in emotes: for match in emotes:
result.title = result.title.replace( result["title"] = result["title"].replace(
match, html.unescape(match) match, html.unescape(match)
) )
body_chunks = [] body_chunks = []
body = "" body = ""
for word in result.body.split(" "): for word in result["body"].split(" "):
if len(body) + 1 + len(word) > 1024: if len(body) + 1 + len(word) > 1024:
body_chunks.append(Field("", body, False)) body_chunks.append(Field("", body, False))
body = "" body = ""
@ -71,7 +71,7 @@ class JokeCog(commands.Cog):
body += " " + word body += " " + word
desc = "" desc = ""
title = result.title title = result["title"]
if len(title) > 256: if len(title) > 256:
new_title = "" new_title = ""
limit = False limit = False
@ -88,18 +88,18 @@ class JokeCog(commands.Cog):
body_chunks.append(Field("", body, False)) body_chunks.append(Field("", body, False))
fields = body_chunks fields = body_chunks
fields.append(Field("Score", result.score)) fields.append(Field("Score", result["score"]))
# Field( # Field(
# "Created At", # "Created At",
# str(datetime.fromtimestamp(result["created_utc"])), # str(datetime.fromtimestamp(result["created_utc"])),
# ), # ),
fields.append(Field("ID", result.id)) fields.append(Field("ID", result["rid"]))
embed = build_embed( embed = build_embed(
title=title, title=title,
description=desc, description=desc,
fields=fields, fields=fields,
url=f"https://reddit.com/r/jokes/comments/{result.rid}", url=f"https://reddit.com/r/jokes/comments/{result['rid']}",
timestamp=datetime.fromtimestamp(result.created_utc), timestamp=datetime.fromtimestamp(result["created_utc"]),
) )
await ctx.send(embed=embed) await ctx.send(embed=embed)
except Exception: except Exception: