async def template(self, ctx, *args): # Argument Parsing parser = GlimmerArgumentParser(ctx) parser.add_argument("-p", "--page", type=int, default=1) parser.add_argument("-f", "--faction", default=None, action=FactionAction) try: args = parser.parse_args(args) except TypeError: return log.debug(f"[uuid:{ctx.uuid}] Parsed arguments: {args}") gid = ctx.guild.id if args.faction is not None: gid = args.faction.id templates = ctx.session.query(TemplateDb).filter_by( guild_id=gid).order_by(TemplateDb.name).all() if len(templates) < 1: raise NoTemplatesError() template_menu = menus.MenuPages(source=TemplateSource(templates), clear_reactions_after=True, timeout=300.0) template_menu.current_page = max( min(args.page - 1, template_menu.source.get_max_pages()), 0) try: await template_menu.start(ctx, wait=True) template_menu.source.embed.set_footer(text=ctx.s("bot.timeout")) await template_menu.message.edit(embed=template_menu.source.embed) except discord.NotFound: await ctx.send(ctx.s("bot.menu_deleted"))
async def check(self, ctx, *args): # Argument Parsing parser = GlimmerArgumentParser(ctx) parser.add_argument("-e", "--onlyErrors", action='store_true') parser.add_argument("-f", "--faction", default=None, action=FactionAction) parser.add_argument("-s", "--sort", default="name_az", choices=[ "name_az", "name_za", "errors_az", "errors_za", "percent_az", "percent_za"]) parser.add_argument("-p", "--page", default=1, type=int) try: a = parser.parse_args(args) except TypeError: return log.debug(f"[uuid:{ctx.uuid}] Parsed arguments: {a}") if a.faction: templates = ctx.session.query(Template).filter_by(guild_id=a.faction.id).all() else: templates = ctx.session.query(Template).filter_by(guild_id=ctx.guild.id).all() if len(templates) < 1: ctx.command.reset_cooldown(ctx) raise NoTemplatesError(False) msg = None # Calc info + send temp msg for canvas, canvas_ts in itertools.groupby(templates, lambda tx: tx.canvas): ct = list(canvas_ts) msg = await self.check_canvas(ctx, ct, canvas, msg=msg) # Delete temp msg and send final report await msg.delete() ts = [t for t in templates if t.errors != 0] if a.onlyErrors else templates if a.sort == "name_az" or a.sort == "name_za": ts = sorted(ts, key=lambda t: t.name, reverse=(a.sort == "name_za")) elif a.sort == "errors_az" or a.sort == "errors_za": ts = sorted(ts, key=lambda t: t.errors, reverse=(a.sort == "errors_za")) elif a.sort == "percent_az" or a.sort == "percent_za": ts = sorted(ts, key=lambda t: (t.size - t.errors) / t.size, reverse=(a.sort == "percent_za")) ts = sorted(ts, key=lambda t: t.canvas) check_menu = menus.MenuPages( source=CheckSource(ts), clear_reactions_after=True, timeout=300.0) check_menu.current_page = max(min(a.page - 1, check_menu.source.get_max_pages()), 0) try: await check_menu.start(ctx, wait=True) check_menu.source.embed.set_footer(text=ctx.s("bot.timeout")) await check_menu.message.edit(embed=check_menu.source.embed) except discord.NotFound: await ctx.send(ctx.s("bot.menu_deleted"))
async def template_check_pixelplanet(self, ctx): ts = [ x for x in sql.template_get_all_by_guild_id(ctx.guild.id) if x.canvas == 'pixelplanet' ] if len(ts) <= 0: ctx.command.parent.reset_cooldown(ctx) raise NoTemplatesError(True) ts = sorted(ts, key=lambda tx: tx.name) msg = await _check_canvas(ctx, ts, "pixelplanet") await msg.delete() await _build_template_report(ctx, ts)
async def template(self, ctx, *args): gid = ctx.guild.id iter_args = iter(args) page = next(iter_args, 1) if page == "-f": fac = next(iter_args, None) if fac is None: await ctx.send(ctx.s("error.missing_arg_faction")) return faction = sql.guild_get_by_faction_name_or_alias(fac) if not faction: raise FactionNotFoundError gid = faction.id page = next(iter_args, 1) try: page = int(page) except ValueError: page = 1 ts = sql.template_get_all_by_guild_id(gid) if len(ts) < 1: raise NoTemplatesError() pages = 1 + len(ts) // 10 page = min(max(page, 1), pages) w1 = max( max(map(lambda tx: len(tx.name), ts)) + 2, len(ctx.s("bot.name"))) msg = [ "**{}** - {} {}/{}".format(ctx.s("template.list_header"), ctx.s("bot.page"), page, pages), "```xl", "{0:<{w1}} {1:<14} {2}".format(ctx.s("bot.name"), ctx.s("bot.canvas"), ctx.s("bot.coordinates"), w1=w1) ] for t in ts[(page - 1) * 10:page * 10]: coords = "({}, {})".format(t.x, t.y) name = '"{}"'.format(t.name) canvas_name = canvases.pretty_print[t.canvas] msg.append("{0:<{w1}} {1:<14} {2}".format(name, canvas_name, coords, w1=w1)) msg.append("") msg.append("// " + ctx.s("template.list_footer_1").format(ctx.gprefix)) msg.append("// " + ctx.s("template.list_footer_2").format(ctx.gprefix)) msg.append("```") await ctx.send('\n'.join(msg))
async def template_check(self, ctx): if not ctx.invoked_subcommand or ctx.invoked_subcommand.name == "check": ts = sql.template_get_all_by_guild_id(ctx.guild.id) if len(ts) < 1: ctx.command.parent.reset_cooldown(ctx) raise NoTemplatesError(False) msg = None ts = sorted(ts, key=lambda tx: tx.name) ts = sorted(ts, key=lambda tx: tx.canvas) for canvas, canvas_ts in itertools.groupby(ts, lambda tx: tx.canvas): ct = list(canvas_ts) msg = await _check_canvas(ctx, ct, canvas, msg=msg) await msg.delete() await _build_template_report(ctx, ts)
async def template(self, ctx, *args): # Argument Parsing parser = GlimmerArgumentParser(ctx) parser.add_argument("-p", "--page", type=int, default=1) parser.add_argument("-f", "--faction", default=None, action=FactionAction) try: args = vars(parser.parse_args(args)) except TypeError: return page = args["page"] faction = args["faction"] gid = ctx.guild.id if faction != None: gid = faction.id templates = sql.template_get_all_by_guild_id(gid) if len(templates) < 1: raise NoTemplatesError() # Find number of pages given there are 25 templates per page. pages = int(math.ceil(len(templates) / 25)) # Makes sure page is in the range (1 <= page <= pages). page = min(max(page, 0), pages) page_index = page - 1 embed = Template.build_table(ctx, page_index, pages, templates) message = await ctx.send(embed=embed) await message.add_reaction('◀') await message.add_reaction('▶') def is_valid(reaction, user): return reaction.message.id == message.id and ( reaction.emoji == '◀' or reaction.emoji == '▶') and user.id != discord.ClientUser.id _5_minutes_in_future = (datetime.datetime.today() + datetime.timedelta(minutes=5.0)) try: while _5_minutes_in_future > datetime.datetime.today(): add_future = asyncio.ensure_future( self.bot.wait_for("reaction_add", timeout=300.0, check=is_valid)) remove_future = asyncio.ensure_future( self.bot.wait_for("reaction_remove", timeout=300.0, check=is_valid)) reaction, _user = None, None while True: if remove_future.done() == True: reaction, _user = remove_future.result() break if add_future.done() == True: reaction, _user = add_future.result() break await asyncio.sleep(0.1) if reaction.emoji == '◀': if page_index != 0: #not on first page, scroll left page_index -= 1 embed = Template.build_table(ctx, page_index, pages, templates) await message.edit(embed=embed) elif reaction.emoji == '▶': if page_index != pages - 1: #not on last page, scroll right page_index += 1 embed = Template.build_table(ctx, page_index, pages, templates) await message.edit(embed=embed) except asyncio.TimeoutError: pass await message.edit(content=ctx.s("bot.timeout"), embed=embed)
async def check(self, ctx, *args): # Argument Parsing parser = GlimmerArgumentParser(ctx) parser.add_argument("-e", "--onlyErrors", action='store_true') parser.add_argument("-f", "--faction", default=None, action=FactionAction) parser.add_argument("-s", "--sort", default="name_az", choices=[ "name_az", "name_za", "errors_az", "errors_za", "percent_az", "percent_za" ]) try: a = vars(parser.parse_args(args)) except TypeError: return only_errors = a["onlyErrors"] faction = a["faction"] sort = a["sort"] if faction: templates = sql.template_get_all_by_guild_id(faction.id) else: templates = sql.template_get_all_by_guild_id(ctx.guild.id) if len(templates) < 1: ctx.command.parent.reset_cooldown(ctx) raise NoTemplatesError(False) msg = None # Calc info + send temp msg for canvas, canvas_ts in itertools.groupby(templates, lambda tx: tx.canvas): ct = list(canvas_ts) msg = await check_canvas(ctx, ct, canvas, msg=msg) # Delete temp msg and send final report await msg.delete() ts = [t for t in templates if t.errors != 0] if only_errors else templates if sort == "name_az" or sort == "name_za": ts = sorted(ts, key=lambda t: t.name, reverse=(sort == "name_za")) elif sort == "errors_az" or sort == "errors_za": ts = sorted(ts, key=lambda t: t.errors, reverse=(sort == "errors_za")) elif sort == "percent_az" or sort == "percent_za": ts = sorted(ts, key=lambda t: (t.size - t.errors) / t.size, reverse=(sort == "percent_za")) ts = sorted(ts, key=lambda t: t.canvas) # Find number of pages given there are 25 templates per page. pages = int(math.ceil(len(ts) / 25)) await build_template_report(ctx, ts, None, pages)