def list_reviews(self, author, subject): result = review_repo.get_subject(subject).first() if not result: result = review_repo.get_subject(f"tv-{subject}").first() if not result: return None reviews = review_repo.get_subject_reviews(result.shortcut) tier_cnt = reviews.count() name = review_repo.get_subject_details(result.shortcut) if tier_cnt == 0: if name: description = f"{name.name}\n*No reviews*" else: description = "*No reviews*" review = None page = "1/1" else: review = reviews[0].Review if name: description = f"{name.name}\n**Average tier:** {round(reviews[0].avg_tier)}" else: description = f"**Average tier:** {round(reviews[0].avg_tier)}" page = f"1/{tier_cnt}" return self.make_embed(author, review, result.shortcut, description, page)
def list_reviews(self, subject): result = review_repo.get_subject(subject).first() if not result: return None reviews = review_repo.get_subject_reviews(subject) tier_cnt = reviews.count() name = review_repo.get_subject_details(subject).name if tier_cnt == 0: description = f"{name}\n*No reviews*" review = None page = "1/1" else: review = reviews[0].Review description = f"{name}\n**Average tier:** {round(reviews[0].avg_tier)}" page = f"1/{tier_cnt}" return self.make_embed(review, subject, description, page)
async def handle_reaction(self, ctx): subject = ctx.message.embeds[0].title.split(" ", 1)[0].lower() footer = ctx.message.embeds[0].footer.text.split("|") # don't track old reviews as they are not compatible if len(footer) != 3: return review_id = footer[2][5:] pages = footer[1].split(":")[1].split("/") try: page = int(pages[0]) max_page = int(pages[1]) except ValueError: await ctx.message.edit(content=messages.reviews_page_e, embed=None) return except IndexError: # handle legacy embed reviews try: await ctx.member.send(messages.review_legacy_clicked) except discord.HTTPException as e: if e.code != 50007: raise return if ctx.emoji in ["◀", "▶", "⏪"]: next_page = utils.pagination_next(ctx.emoji, page, max_page) if next_page: review = review_repo.get_subject_reviews(subject) if review.count() >= next_page: review = review.all()[next_page - 1].Review next_page = f"{next_page}/{max_page}" embed = self.rev.update_embed(ctx.message.embeds[0], review, next_page) if embed.fields[3].name == "Text page": await ctx.message.add_reaction("🔼") await ctx.message.add_reaction("🔽") else: for emote in ctx.message.reactions: if emote.emoji == "🔼": await ctx.message.remove_reaction("🔼", self.bot.user) await ctx.message.remove_reaction("🔽", self.bot.user) break await ctx.message.edit(embed=embed) elif ctx.emoji in ["👍", "👎", "🛑"]: review = review_repo.get_review_by_id(review_id) member_id = str(ctx.member.id) if review and member_id != review.member_ID: if ctx.emoji == "👍": self.rev.add_vote(review_id, True, member_id) elif ctx.emoji == "👎": self.rev.add_vote(review_id, False, member_id) elif ctx.emoji == "🛑": review_repo.remove_vote(review_id, member_id) page = f"{page}/{max_page}" embed = self.rev.update_embed(ctx.message.embeds[0], review, page) await ctx.message.edit(embed=embed) elif ctx.emoji in ["🔼", "🔽"]: if ctx.message.embeds[0].fields[3].name == "Text page": review = review_repo.get_review_by_id(review_id) if review: pages = ctx.message.embeds[0].fields[3].value.split("/") text_page = int(pages[0]) max_text_page = int(pages[1]) next_text_page = utils.pagination_next(ctx.emoji, text_page, max_text_page) if next_text_page: page = f"{page}/{max_page}" embed = self.rev.update_embed( ctx.message.embeds[0], review, page, next_text_page ) await ctx.message.edit(embed=embed) if ctx.message.guild: # cannot remove reaction in DM await ctx.message.remove_reaction(ctx.emoji, ctx.member)
async def on_raw_reaction_add(self, payload): ctx = await utils.reaction_get_ctx(self.bot, payload) if ctx is None: return if ( ctx["message"].embeds and ctx["message"].embeds[0].title is not discord.Embed.Empty and "reviews" in ctx["message"].embeds[0].title ): subject = ctx["message"].embeds[0].title.split(" ", 1)[0].lower() footer = ctx["message"].embeds[0].footer.text.split("|") review_id = footer[2][5:] pages = footer[1].split(":")[1].split("/") try: page = int(pages[0]) max_page = int(pages[1]) except ValueError: await ctx["message"].edit(content=messages.reviews_page_e, embed=None) return if ctx["emoji"] in ["◀", "▶", "⏪"]: next_page = utils.pagination_next(ctx["emoji"], page, max_page) if next_page: review = review_repo.get_subject_reviews(subject) if review.count() >= next_page: review = review.all()[next_page - 1].Review next_page = f"{next_page}/{max_page}" embed = self.rev.update_embed(ctx["message"].embeds[0], review, next_page) if embed.fields[3].name == "Text page": await ctx["message"].add_reaction("🔼") await ctx["message"].add_reaction("🔽") else: for emote in ctx["message"].reactions: if emote.emoji == "🔼": await ctx["message"].remove_reaction("🔼", self.bot.user) await ctx["message"].remove_reaction("🔽", self.bot.user) break await ctx["message"].edit(embed=embed) elif ctx["emoji"] in ["👍", "👎", "🛑"]: review = review_repo.get_review_by_id(review_id) member_id = str(ctx["member"].id) if review and member_id != review.member_ID: if ctx["emoji"] == "👍": self.rev.add_vote(review_id, True, member_id) elif ctx["emoji"] == "👎": self.rev.add_vote(review_id, False, member_id) elif ctx["emoji"] == "🛑": review_repo.remove_vote(review_id, member_id) page = f"{page}/{max_page}" embed = self.rev.update_embed(ctx["message"].embeds[0], review, page) await ctx["message"].edit(embed=embed) elif ctx["emoji"] in ["🔼", "🔽"]: if ctx["message"].embeds[0].fields[3].name == "Text page": review = review_repo.get_review_by_id(review_id) if review: pages = ctx["message"].embeds[0].fields[3].value.split("/") text_page = int(pages[0]) max_text_page = int(pages[1]) next_text_page = utils.pagination_next(ctx["emoji"], text_page, max_text_page) if next_text_page: page = f"{page}/{max_page}" embed = self.rev.update_embed( ctx["message"].embeds[0], review, page, next_text_page ) await ctx["message"].edit(embed=embed) if ctx["message"].guild: # cannot remove reaction in DM await ctx["message"].remove_reaction(ctx["emoji"], ctx["member"])
async def on_raw_reaction_add(self, payload): ctx = await utils.reaction_get_ctx(self.bot, payload) if ctx is None: return if (ctx["message"].embeds and ctx["message"].embeds[0].title is not discord.Embed.Empty and re.match(".* reviews", ctx["message"].embeds[0].title)): subject = ctx["message"].embeds[0].title.split(" ", 1)[0].lower() footer = ctx["message"].embeds[0].footer.text.split("|")[0] pos = footer.find("/") try: page = int(footer[8:pos]) max_page = int(footer[(pos + 1):]) except ValueError: await ctx["message"].edit(content=messages.reviews_page_e, embed=None) return description = ctx["message"].embeds[0].description if ctx["emoji"] in ["◀", "▶", "⏪"]: next_page = utils.pagination_next(ctx["emoji"], page, max_page) if next_page: review = review_repo.get_subject_reviews(subject) if review.count() >= next_page: review = review.all()[next_page - 1].Review next_page = str(next_page) + "/" + str(max_page) embed = self.rev.make_embed(review, subject, description, next_page) if embed.fields[3].name == "Text page": await ctx["message"].add_reaction("🔼") await ctx["message"].add_reaction("🔽") else: for emote in ctx["message"].reactions: if emote.emoji == "🔼": await ctx["message"].remove_reaction( "🔼", self.bot.user) await ctx["message"].remove_reaction( "🔽", self.bot.user) break await ctx["message"].edit(embed=embed) elif ctx["emoji"] in ["👍", "👎", "🛑"]: review = review_repo.get_subject_reviews(subject)[page - 1].Review if str(ctx["member"].id) != review.member_ID: review_id = review.id if ctx["emoji"] == "👍": self.rev.add_vote(review_id, True, str(ctx["member"].id)) elif ctx["emoji"] == "👎": self.rev.add_vote(review_id, False, str(ctx["member"].id)) elif ctx["emoji"] == "🛑": review_repo.remove_vote(review_id, str(ctx["member"].id)) page = str(page) + "/" + str(max_page) embed = self.rev.make_embed(review, subject, description, page) await ctx["message"].edit(embed=embed) elif ctx["emoji"] in ["🔼", "🔽"]: if ctx["message"].embeds[0].fields[3].name == "Text page": review = review_repo.get_subject_reviews(subject) if review: review = review[page - 1].Review text_page = ctx["message"].embeds[0].fields[3].value pos = ctx["message"].embeds[0].fields[3].value.find( "/") max_text_page = int(text_page[(pos + 1):]) text_page = int(text_page[:pos]) next_text_page = utils.pagination_next( ctx["emoji"], text_page, max_text_page) if next_text_page: page = str(page) + "/" + str(max_page) embed = self.rev.make_embed( review, subject, description, page) embed = self.rev.change_text_page( review, embed, next_text_page, max_text_page) await ctx["message"].edit(embed=embed) if ctx["message"].guild: # cannot remove reaction in DM await ctx["message"].remove_reaction(ctx["emoji"], ctx["member"])