class SearchResultsBrowser: def __init__(self, bot: Bot, ctx: Context, results: List[Doujin], **kwargs): """Class to create and run a browser from NHentai-API `results` - obtained from nhentai_api.search(query) `msg` - optional message that the bot owns to edit, otherwise created """ self.bot = bot self.ctx = ctx self.doujins = results self.index = 0 self.lolicon_allowed = kwargs.pop("lolicon_allowed", False) self.minimal_details = kwargs.pop("minimal_details", True) self.name = kwargs.pop("name", "Search Results") self.active_message: Message = kwargs.pop("msg", None) self.am_embed: Embed = Embed() self.is_zoomed = False self.language = kwargs.pop("user_language", "eng") async def update_browser(self, ctx): message_part = [] for ind, dj in enumerate(self.doujins): try: if ind == self.index and int(dj.id) in self.bot.user_data['UserData'][str(ctx.author.id)]['Lists']['Built-in']["Favorites|*n*|fav"]: symbol = 'š©' elif ind == self.index: symbol='š„' elif int(dj.id) in self.bot.user_data['UserData'][str(ctx.author.id)]['Lists']['Built-in']["Favorites|*n*|fav"]: symbol = 'š¦' else: symbol='ā¬' except KeyError: symbol='ā¬' tags = [tag.name for tag in dj.tags if tag.type == "tag"] if any([tag in restricted_tags for tag in tags]) and ctx.guild and not self.lolicon_allowed: message_part.append( f"{'**' if ind == self.index else ''}" f"`{symbol} {str(ind+1).ljust(2)}` | {localization[self.language]['search_doujins']['contains_restricted_tags']}" f"{'**' if ind == self.index else ''}") else: message_part.append( f"{'**' if ind == self.index else ''}" f"`{symbol} {str(ind+1).ljust(2)}` | " f"__`{str(dj.id).ljust(7)}`__ | " f"{language_to_flag(dj.languages)} | " f"{shorten(dj.title.pretty, width=40, placeholder='...')}" f"{'**' if ind == self.index else ''}") doujin = self.doujins[self.index] previous_emb = deepcopy(self.am_embed) #self.active_message.embeds[0] = self.am_embed self.am_embed = Embed( title=self.name, description=f"\n"+('\n'.join(message_part))+"\n\nāāāāāāāāāāāāāāāāāāāāāāāā") nhentai = NHentai() tags = [tag.name for tag in doujin.tags if tag.type == "tag"] if any([tag in restricted_tags for tag in tags]) and ctx.guild and not self.lolicon_allowed: self.am_embed.add_field( name=localization[self.language]['results_browser']['forbidden']['title'], inline=False, value=localization[self.language]['results_browser']['forbidden']['description'] ).set_footer( text=f"ā N/A" ) doujin.cover.src = str(self.bot.user.avatar.url) else: if self.minimal_details: self.am_embed.add_field( name=localization[self.language]['results_browser']['minimal_details'], inline=False, value= f"ID: `{doujin.id}`\n" f"{localization[self.language]['doujin_info']['fields']['title']}: {language_to_flag(doujin.languages)} `{shorten(doujin.title.pretty, width=256, placeholder='...')}`\n" f"{localization[self.language]['doujin_info']['fields']['artists']}: `{', '.join([tag.name for tag in doujin.artists]) if doujin.artists else localization[self.language]['doujin_info']['fields']['not_provided']}`\n" f"{localization[self.language]['doujin_info']['fields']['characters']}: `{', '.join([tag.name for tag in doujin.characters]) if doujin.characters else localization[self.language]['doujin_info']['fields']['original']}`\n" f"{localization[self.language]['doujin_info']['fields']['parodies']}: `{', '.join([tag.name for tag in doujin.parodies]) if doujin.parodies else localization[self.language]['doujin_info']['fields']['original']}`\n" f"{localization[self.language]['doujin_info']['fields']['tags']}:\n||`{shorten(str(', '.join([tag.name for tag in doujin.tags if tag.type == 'tag']) if [tag.name for tag in doujin.tags if tag.type == 'tag'] else localization[self.language]['doujin_info']['fields']['not_provided']), width=950, placeholder='...')}`||" ).set_footer( text=f"{localization[self.language]['doujin_info']['sfw']}") self.am_embed.set_author( name=f"NHentai", icon_url="https://cdn.discordapp.com/emojis/845298862184726538.png?v=1") self.am_embed.set_thumbnail(url=Embed.Empty) self.am_embed.set_image(url=Embed.Empty) else: self.am_embed.add_field( name=localization[self.language]['doujin_info']['fields']['title'], inline=False, value=f"`{shorten(doujin.title.pretty, width=256, placeholder='...')}`" ).add_field( inline=False, name=localization[self.language]['doujin_info']['fields']['id/pages'], value=f"`{doujin.id}` - `{doujin.total_pages}`" ).add_field( inline=False, name=localization[self.language]['doujin_info']['fields']['date_uploaded'], value=f"<t:{int(doujin.upload_at.timestamp())}>" ).add_field( inline=False, name=localization[self.language]['doujin_info']['fields']['languages'], value=f"{language_to_flag(doujin.languages)} `{', '.join([localization[self.language]['doujin_info']['fields']['language_names'][tag.name] for tag in doujin.languages]) if doujin.languages else localization[self.language]['doujin_info']['fields']['not_provided']}`" ).add_field( inline=False, name=localization[self.language]['doujin_info']['fields']['artists'], value=f"`{', '.join([tag.name for tag in doujin.artists]) if doujin.artists else localization[self.language]['doujin_info']['fields']['not_provided']}`" ).add_field( inline=False, name=localization[self.language]['doujin_info']['fields']['characters'], value=f"`{', '.join([tag.name for tag in doujin.characters]) if doujin.characters else localization[self.language]['doujin_info']['fields']['original']}`" ).add_field( inline=False, name=localization[self.language]['doujin_info']['fields']['parodies'], value=f"`{', '.join([tag.name for tag in doujin.parodies]) if doujin.parodies else localization[self.language]['doujin_info']['fields']['original']}`" ).set_footer( text=f"ā {doujin.total_favorites}" ) # Doujin count for tags tags_list = [] for tag in [tag for tag in doujin.tags if tag.type == "tag"]: count = tag.count parse_count = list(str(count)) if len(parse_count) < 4: tags_list.append(f"{localization[self.language]['fields']['tag_names'][tag.name] if tag.name in localization[self.language]['doujin_info']['fields']['tag_names'] else tag.name}[{count}]") elif len(parse_count) >= 4 and len(parse_count) <= 6: count = count/1000 tags_list.append(f"{localization[self.language]['fields']['tag_names'][tag.name] if tag.name in localization[self.language]['doujin_info']['fields']['tag_names'] else tag.name}[{round(count, 1)}k]") elif len(parse_count) > 7: count = count/1000000 tags_list.append(f"{localization[self.language]['fields']['tag_names'][tag.name] if tag.name in localization[self.language]['doujin_info']['fields']['tag_names'] else tag.name}[{round(count, 2)}m]") self.am_embed.add_field( inline=False, name=localization[self.language]["doujin_info"]["fields"]["tags"], value=f"```{shorten(str(', '.join(tags_list) if tags_list else localization[self.language]['doujin_info']['fields']['not_provided']), width=1018, placeholder='...')}```" ) self.am_embed.set_author( name=f"NHentai", url=f"https://nhentai.net/g/{doujin.id}/", icon_url="https://cdn.discordapp.com/emojis/845298862184726538.png?v=1") if self.is_zoomed: self.am_embed.set_image(url=doujin.cover.src) self.am_embed.set_thumbnail(url=Embed.Empty) elif not self.is_zoomed: self.am_embed.set_thumbnail(url=doujin.cover.src) self.am_embed.set_image(url=Embed.Empty) if not self.active_message: self.active_message = await ctx.send("...") class SRBControls(ui.View): def __init__(self, bot, ctx, parent): super().__init__(timeout=60) self.value = 0 self.bot = bot self.ctx = ctx self.parent = parent @ui.button(emoji=self.bot.get_emoji(853800909108936754), style=ButtonStyle.secondary, custom_id="up") async def up_button(self, button, interaction): if interaction.user.id == self.ctx.author.id: await interaction.response.defer() if self.parent.index > 0: self.parent.index -= 1 elif self.parent.index == 0: self.parent.index = len(self.parent.doujins)-1 self.stop() @ui.button(emoji=self.bot.get_emoji(853800909276315678), style=ButtonStyle.secondary, custom_id="down") async def down_button(self, button, interaction): if interaction.user.id == self.ctx.author.id: await interaction.response.defer() if self.parent.index < len(self.parent.doujins)-1: self.parent.index += 1 elif self.parent.index == len(self.parent.doujins)-1: self.parent.index = 0 self.stop() @ui.button(emoji=self.bot.get_emoji(853668227212902410), style=ButtonStyle.secondary, custom_id="select") async def select_button(self, button, interaction): if interaction.user.id == self.ctx.author.id: await interaction.response.defer() conf = await self.ctx.send(embed=Embed( description=localization[self.parent.language]['results_browser']['buttons']['select'])) while True: try: m = await self.bot.wait_for("message", timeout=15, bypass_cooldown=True, check=lambda m: m.author.id == self.ctx.author.id and m.channel.id == self.ctx.channel.id) except TimeoutError: await conf.delete() self.stop() return else: with suppress(Forbidden): await m.delete() if m.content == "n-cancel": await conf.delete() self.stop() return if is_int(m.content) and (int(m.content)-1) in range(0, len(self.parent.doujins)): await conf.delete() self.parent.index = int(m.content)-1 self.stop() return else: continue self.stop() # unreachable, but just to be consistant with design @ui.button(emoji=self.bot.get_emoji(853668227175546952), style=ButtonStyle.secondary, custom_id="stop") async def stop_button(self, button, interaction): if interaction.user.id == self.ctx.author.id: await interaction.response.defer() message_part = [] for ind, dj in enumerate(self.parent.doujins): tags = [tag.name for tag in dj.tags if tag.type == "tag"] if any([tag in restricted_tags for tag in tags]) and self.ctx.guild and not self.parent.lolicon_allowed: message_part.append(localization[self.parent.language]['search_doujins']['contains_restricted_tags']) else: message_part.append( f"__`{str(dj.id).ljust(7)}`__ | " f"{language_to_flag(dj.languages)} | " f"{shorten(dj.title.pretty, width=50, placeholder='...')}") self.parent.am_embed = Embed( title=self.parent.name, description=f"\n"+('\n'.join(message_part))) self.parent.am_embed.set_author( name="NHentai", url=f"https://nhentai.net/", icon_url="https://cdn.discordapp.com/emojis/845298862184726538.png?v=1") self.parent.am_embed.set_thumbnail(url=Embed.Empty) self.parent.am_embed.set_image(url=Embed.Empty) await self.parent.active_message.edit(embed=self.parent.am_embed, view=None) self.value = 1 self.stop() if not self.ctx.guild or (self.ctx.guild and not all([ self.ctx.guild.me.guild_permissions.manage_channels, self.ctx.guild.me.guild_permissions.manage_roles, self.ctx.guild.me.guild_permissions.manage_messages])): @ui.button(emoji=self.bot.get_emoji(853684136379416616), style=ButtonStyle.secondary, custom_id="read", disabled=True) async def read_button(self, button, interaction): return else: @ui.button(emoji=self.bot.get_emoji(853684136379416616), style=ButtonStyle.secondary, custom_id="read", disabled=self.minimal_details) async def read_button(self, button, interaction): if interaction.user.id == self.ctx.author.id: await interaction.response.defer() tags = [tag.name for tag in self.parent.doujins[self.parent.index].tags if tag.type == "tag"] if any([tag in restricted_tags for tag in tags]) and self.ctx.guild and not self.parent.lolicon_allowed: self.stop() return message_part = [] for ind, dj in enumerate(self.parent.doujins): tags = [tag.name for tag in dj.tags if tag.type == "tag"] if any([tag in restricted_tags for tag in tags]) and self.ctx.guild and not self.parent.lolicon_allowed: message_part.append(localization[self.parent.language]['search_doujins']['contains_restricted_tags']) else: message_part.append( f"{'**' if ind == self.parent.index else ''}" f"__`{str(dj.id).ljust(7)}`__ | " f"{language_to_flag(dj.languages)} | " f"{shorten(dj.title.pretty, width=50, placeholder='...')}" f"{'**' if ind == self.parent.index else ''}") self.parent.am_embed = Embed( title=self.parent.name, description=f"\n"+('\n'.join(message_part))) self.parent.am_embed.set_author( name="NHentai", url=f"https://nhentai.net/", icon_url="https://cdn.discordapp.com/emojis/845298862184726538.png?v=1") self.parent.am_embed.set_thumbnail(url=Embed.Empty) self.parent.am_embed.set_image(url=Embed.Empty) await self.parent.active_message.edit(embed=self.parent.am_embed, view=None) self.value = 1 self.stop() doujin = self.parent.doujins[self.parent.index] if str(doujin.id) in self.bot.user_data['UserData'][str(self.ctx.author.id)]['Lists']['Built-in']["Bookmarks|*n*|bm"]: page = self.bot.user_data['UserData'][str(self.ctx.author.id)]['Lists']['Built-in']["Bookmarks|*n*|bm"][str(doujin.id)] else: page = 0 session = ImagePageReader(self.bot, self.ctx, doujin.images, doujin.title.pretty, str(doujin.id), starting_page=page) response = await session.setup() if response: await session.start() else: await self.parent.active_message.edit(embed=self.parent.am_embed) @ui.button(emoji=self.bot.get_emoji(853684136433942560), style=ButtonStyle.secondary, custom_id="zoom", disabled=self.minimal_details) async def zoom_button(self, button, interaction): if interaction.user.id == self.ctx.author.id: await interaction.response.defer() self.parent.is_zoomed = not self.parent.is_zoomed self.stop() @ui.button(emoji=self.bot.get_emoji(853668227205038090), style=ButtonStyle.secondary, custom_id="readlater") async def readlater_button(self, button, interaction): if interaction.user.id == self.ctx.author.id: await interaction.response.defer() if len(self.bot.user_data["UserData"][str(self.ctx.author.id)]["Lists"]["Built-in"]["Read Later|*n*|rl"]) >= 25: await self.ctx.send( embed=Embed( color=0xff0000, description=localization[self.parent.language]['results_browser']['buttons']['read_later_full'] ), delete_after=5) self.stop() return if str(self.parent.doujins[self.parent.index].id) not in self.bot.user_data["UserData"][str(self.ctx.author.id)]["Lists"]["Built-in"]["Read Later|*n*|rl"]: self.bot.user_data["UserData"][str(self.ctx.author.id)]["Lists"]["Built-in"]["Read Later|*n*|rl"].append(str(self.parent.doujins[self.parent.index].id)) await self.ctx.send( embed=Embed( description=localization[self.parent.language]['results_browser']['buttons']['add_to_read_later'].format(code=self.parent.doujins[self.parent.index].id) ), delete_after=5) else: self.bot.user_data["UserData"][str(self.ctx.author.id)]["Lists"]["Built-in"]["Read Later|*n*|rl"].remove(str(self.parent.doujins[self.parent.index].id)) await self.ctx.send( embed=Embed( description=localization[self.parent.language]['results_browser']['buttons']['remove_from_read_later'].format(code=self.parent.doujins[self.parent.index].id) ), delete_after=5) self.stop() async def on_timeout(self): message_part = [] for ind, dj in enumerate(self.parent.doujins): tags = [tag.name for tag in dj.tags if tag.type == "tag"] if any([tag in restricted_tags for tag in tags]) and self.ctx.guild and not self.parent.lolicon_allowed: message_part.append(localization[self.parent.language]['search_doujins']['contains_restricted_tags']) else: message_part.append( f"__`{str(dj.id).ljust(7)}`__ | " f"{language_to_flag(dj.languages)} | " f"{shorten(dj.title.pretty, width=50, placeholder='...')}") self.parent.am_embed = Embed( title=self.parent.name, description=f"\n"+('\n'.join(message_part))) self.parent.am_embed.set_author( name="NHentai", url=f"https://nhentai.net/", icon_url="https://cdn.discordapp.com/emojis/845298862184726538.png?v=1") self.parent.am_embed.set_thumbnail(url=Embed.Empty) self.parent.am_embed.set_image(url=Embed.Empty) await self.parent.active_message.edit(embed=self.parent.am_embed, view=None) self.value = 1 self.stop() view = SRBControls(self.bot, self.ctx, self) view.add_item(ui.Button(label=localization[self.language]['results_browser']['buttons']['support_server'], style=ButtonStyle.link, url="https://discord.gg/DJ4wdsRYy2")) await self.active_message.edit(embed=self.am_embed, view=view) await view.wait() return view.value async def start(self, ctx): """Initial start of the result browser.""" view_exit_code = 0 while view_exit_code == 0: view_exit_code = await self.update_browser(self.ctx)
async def doujin_info(self, ctx, code="random", interface="new"): lolicon_allowed = False try: if ctx.guild.id in self.bot.user_data["UserData"][str( ctx.guild.owner_id)]["Settings"]["UnrestrictedServers"]: lolicon_allowed = True except KeyError: pass if not ctx.channel.is_nsfw(): await ctx.send(embed=Embed( description= ":x: This command cannot be used in a non-NSFW channel.")) return try: if code.lower() not in ["random", "r"]: code = int(code) code = str(code) except ValueError: await ctx.send(embed=Embed( description= ":x: You didn't type a proper ID. Hint: It has to be a number!" )) return nhentai_api = NHentai() edit = await ctx.send(embed=Embed( description="<a:nreader_loading:810936543401213953>")) if code.lower() not in ["random", "r"]: if code not in self.bot.doujin_cache: doujin = await nhentai_api.get_doujin(code) else: doujin = self.bot.doujin_cache[code] if not doujin: await edit.edit(embed=Embed( description= ":mag_right::x: I did not find a doujin with that ID.")) return else: self.bot.doujin_cache[code] = doujin if ("lolicon" in doujin.tags or "shotacon" in doujin.tags) and ctx.guild and not lolicon_allowed: await edit.edit(embed=Embed( description= ":warning::no_entry_sign: This doujin contains lolicon/shotacon content and cannot be displayed publically." )) if not self.bot.user_data["UserData"][str( ctx.author.id )]["Settings"]["NotificationsDue"]["LoliconViewingTip"]: with suppress(Forbidden): await ctx.author.send( localization["eng"]["notifications_due"] ["lolicon_viewing_tip"]) self.bot.user_data["UserData"][str( ctx.author.id)]["Settings"]["NotificationsDue"][ "LoliconViewingTip"] = True return else: while True: doujin = await nhentai_api.get_random() self.bot.doujin_cache[doujin.id] = doujin if ("lolicon" in doujin.tags or "shotacon" in doujin.tags) and ctx.guild and not lolicon_allowed: await sleep(0.5) continue else: break # Doujin count for tags tags_list = [] for tag in [tag for tag in doujin.tags if tag.type == "tag"]: count = tag.count parse_count = list(str(count)) if len(parse_count) < 4: tags_list.append(f"{tag.name}[{count}]") elif len(parse_count) >= 4 and len(parse_count) <= 6: count = count / 1000 tags_list.append(f"{tag.name}[{round(count, 1)}k]") elif len(parse_count) > 7: count = count / 1000000 tags_list.append(f"{tag.name}[{round(count, 2)}m]") if interface == "old": emb = Embed( description=f"Doujin ID: __`{doujin.id}`__\n" f"Languages: {language_to_flag(doujin.languages)} `{', '.join([tag.name for tag in doujin.languages]) if doujin.languages else 'Not provided'}`\n" f"Pages: `{len(doujin.images)}`\n" f"Artist(s): `{', '.join([tag.name for tag in doujin.artists]) if doujin.artists else 'Not provided'}`\n" f"Character(s): `{', '.join([tag.name for tag in doujin.characters]) if doujin.characters else 'Original'}`\n" f"Parody of: `{', '.join([tag.name for tag in doujin.parodies]) if doujin.parodies else 'Original'}`\n" f"Tags: ```{', '.join(tags_list) if doujin.tags else 'None provided'}```" ) else: emb = Embed() emb.add_field( inline=False, name="Title", value= f"`{shorten(doujin.title.pretty, width=256, placeholder='...') if doujin.title.pretty else 'Not provided'}`" ).add_field( inline=False, name="ID ć¼ Pages", value=f"`{doujin.id} ć¼ {len(doujin.images)}`" ).add_field( inline=False, name="Language(s)", value= f"{language_to_flag(doujin.languages)} `{', '.join([tag.name for tag in doujin.languages]) if doujin.languages else 'Not provided'}`" ).add_field( inline=False, name="Artist(s)", value= f"`{', '.join([tag.name for tag in doujin.artists]) if doujin.artists else 'Not provided'}`" ).add_field( inline=False, name="Character(s)", value= f"`{', '.join([tag.name for tag in doujin.characters]) if doujin.characters else 'Original'}`" ).add_field( inline=False, name="Parody Of", value= f"`{', '.join([tag.name for tag in doujin.parodies]) if doujin.parodies else 'Original'}`" ).add_field( inline=False, name="Tags", value= f"```{', '.join(tags_list) if doujin.tags else 'None provided'}```" ) emb.set_author( name= f"{shorten(doujin.title.pretty, width=120, placeholder='...') if doujin.title.pretty else 'Not provided'}", url=f"https://nhentai.net/g/{doujin.id}/", icon_url= "https://cdn.discordapp.com/emojis/845298862184726538.png?v=1") emb.set_thumbnail(url=doujin.images[0].src) print(f"[HRB] {ctx.author} ({ctx.author.id}) looked up `{doujin.id}`.") await edit.edit(content="", embed=emb) await edit.add_reaction("š") await edit.add_reaction("š") while True: try: reaction, user = await self.bot.wait_for("reaction_add", timeout=60, check=lambda r,u: r.message.id==edit.id and \ u.id==ctx.author.id and \ str(r.emoji) in ["š", "š"]) except TimeoutError: emb.set_footer(text="Provided by NHentai-API") emb.set_thumbnail(url=doujin.images[0].src) emb.set_image(url=Embed.Empty) await edit.edit(embed=emb) with suppress(Forbidden): await edit.clear_reactions() return except BotInteractionCooldown: continue else: if str(reaction.emoji) == "š": with suppress(Forbidden): await edit.clear_reactions() emb.set_footer(text="Provided by NHentai-API") emb.set_thumbnail(url=doujin.images[0].src) emb.set_image(url=Embed.Empty) await edit.edit(embed=emb) session = ImagePageReader( self.bot, ctx, doujin.images, f"{doujin.id} [*n*] {doujin.title.pretty if doujin.title.pretty else 'Not provided'}" ) response = await session.setup() if response: print( f"[HRB] {ctx.author} ({ctx.author.id}) started reading `{doujin.id}`." ) await session.start() else: emb.set_footer(text=Embed.Empty) await edit.edit(embed=emb) return elif str(reaction.emoji) == "š": if not emb.image: emb.set_image(url=emb.thumbnail.url) emb.set_thumbnail(url=Embed.Empty) # word = "Hide" elif not emb.thumbnail: emb.set_thumbnail(url=emb.image.url) emb.set_image(url=Embed.Empty) # word = "Minimize" await edit.remove_reaction("š", ctx.author) await edit.edit(content="", embed=emb) continue