From dd7c31395d8ec7f28e98af9aed76d722a11482b9 Mon Sep 17 00:00:00 2001 From: Ahmad Ansori Palembani Date: Thu, 27 Jul 2023 11:28:21 +0700 Subject: [PATCH] feat: Add shortcut `prefixes` for `prefix ls` --- src/zibot/exts/meta/meta.py | 72 +++++++++++++++++++++++++++++-------- 1 file changed, 58 insertions(+), 14 deletions(-) diff --git a/src/zibot/exts/meta/meta.py b/src/zibot/exts/meta/meta.py index d8a5b848..c8fbebaa 100644 --- a/src/zibot/exts/meta/meta.py +++ b/src/zibot/exts/meta/meta.py @@ -18,6 +18,7 @@ from ...core import commands as cmds from ...core.context import Context from ...core.embed import ZEmbed, ZEmbedBuilder +from ...core.errors import DefaultError from ...core.menus import ZMenuPagesView from ...utils import utcnow from ...utils.format import cleanifyPrefix @@ -25,7 +26,6 @@ from ._pages import PrefixesPageSource from .subcogs import MetaCustomCommands - if TYPE_CHECKING: from ...core.bot import ziBot @@ -78,7 +78,12 @@ def __init__(self, bot: ziBot): async def source(self, ctx): await ctx.try_reply(_("source-message", link=self.bot.links["Source Code"])) - @cmds.command(name=_("about"), aliases=("botinfo", "bi"), description=_("about-desc"), hybrid=True) + @cmds.command( + name=_("about"), + aliases=("botinfo", "bi"), + description=_("about-desc"), + hybrid=True, + ) @commands.cooldown(1, 5, commands.BucketType.user) async def about(self, ctx: Context): # Z3R0 Banner @@ -87,12 +92,17 @@ async def about(self, ctx: Context): e = ZEmbedBuilder( description=_(ctx.bot.description + "-extended", license=ctx.bot.license), ) - e.setAuthor(name=ctx.bot.requireUser().name, iconUrl=ctx.bot.requireUser().display_avatar.url) + e.setAuthor( + name=ctx.bot.requireUser().name, + iconUrl=ctx.bot.requireUser().display_avatar.url, + ) e.setImage(url="attachment://banner.png") e.addField(name=_("about-author-title"), value=ctx.bot.author, inline=True) e.addField( name=_("about-library-title"), - value="[`discord.py`](https://github.com/Rapptz/discord.py) - `v{}`".format(discord.__version__), + value="[`discord.py`](https://github.com/Rapptz/discord.py) - `v{}`".format( + discord.__version__ + ), inline=True, ) e.addField(name=_("about-version-title"), value=ctx.bot.version, inline=True) @@ -100,7 +110,11 @@ async def about(self, ctx: Context): for k, v in ctx.bot.links.items(): if k and v: view.add_item(discord.ui.Button(label=k, url=v)) - await ctx.try_reply(file=f, embed=await e.build(ctx, autoGenerateDT=True, addRequester=True), view=view) + await ctx.try_reply( + file=f, + embed=await e.build(ctx, autoGenerateDT=True, addRequester=True), + view=view, + ) @cmds.command(name=_("stats"), description=_("stats-desc"), hybrid=True) @commands.cooldown(1, 5, commands.BucketType.user) @@ -110,7 +124,9 @@ async def stats(self, ctx: Context): return uptime = utcnow() - self.bot.uptime e = ZEmbedBuilder() - e.setAuthor(name=_("stats-title", bot=botUser.name), iconUrl=botUser.display_avatar.url) + e.setAuthor( + name=_("stats-title", bot=botUser.name), iconUrl=botUser.display_avatar.url + ) e.addField(name=_("stats-uptime-title"), value=humanize.precisedelta(uptime)) e.addField( name=await ctx.translate(_("stats-command-title")), @@ -122,7 +138,19 @@ async def stats(self, ctx: Context): ) ), ) - await ctx.try_reply(embed=await e.build(ctx, autoGenerateDT=True, addRequester=True)) + await ctx.try_reply( + embed=await e.build(ctx, autoGenerateDT=True, addRequester=True) + ) + + @commands.command( + name="prefixes", + description="Shortcut for `prefix ls` command", + invoke_without_command=True, + with_app_command=False, + ) + @commands.cooldown(1, 5, commands.BucketType.user) + async def prefixes(self, ctx): + await ctx.try_invoke(self.prefList) @commands.group( aliases=("pref",), @@ -149,9 +177,15 @@ async def prefix(self, ctx): exemple=("prefix ls", "pref list"), ) @commands.cooldown(1, 5, commands.BucketType.user) - async def prefList(self, ctx): + async def prefList(self, ctx: Context): + if not ctx.guild: + raise DefaultError( + "Custom prefix for user is not yet implemented! Ping me to check my default prefixes" + ) prefixes = await ctx.guild.getPrefixes() - menu = ZMenuPagesView(ctx, source=PrefixesPageSource(ctx, ["placeholder"] * 2 + prefixes)) + menu = ZMenuPagesView( + ctx, source=PrefixesPageSource(ctx, ["placeholder"] * 2 + prefixes) + ) await menu.start() @prefix.command( @@ -175,7 +209,9 @@ async def prefAdd(self, ctx: Context, *prefix: str): try: await ctx.requireGuild().addPrefix(_prefix) - await ctx.success(title=_("prefix-added", prefix=cleanifyPrefix(self.bot, _prefix))) + await ctx.success( + title=_("prefix-added", prefix=cleanifyPrefix(self.bot, _prefix)) + ) except Exception as exc: await ctx.error(str(exc)) @@ -200,11 +236,15 @@ async def prefRm(self, ctx: Context, *prefix: str): try: await ctx.requireGuild().rmPrefix(_prefix.lstrip()) - await ctx.success(title=_("prefix-removed", prefix=cleanifyPrefix(self.bot, _prefix))) + await ctx.success( + title=_("prefix-removed", prefix=cleanifyPrefix(self.bot, _prefix)) + ) except Exception as exc: await ctx.error(str(exc)) - @cmds.command(name=_("ping"), aliases=("p",), description=_("ping-desc"), hybrid=True) + @cmds.command( + name=_("ping"), aliases=("p",), description=_("ping-desc"), hybrid=True + ) async def ping(self, ctx): start = time.perf_counter() msgPing = 0 @@ -215,7 +255,9 @@ async def ping(self, ctx): e = ZEmbedBuilder(title=_("ping-title")) - botLatency = f"{round(self.bot.latency*1000)}ms" if not ctx.bot.config.test else "∞" + botLatency = ( + f"{round(self.bot.latency*1000)}ms" if not ctx.bot.config.test else "∞" + ) e.addField( name=_("ping-websocket-title"), @@ -228,7 +270,9 @@ async def ping(self, ctx): value=f"{round(msgPing)}ms", ) - await ctx.try_reply(embed=await e.build(ctx, autoGenerateDT=True, addRequester=True)) + await ctx.try_reply( + embed=await e.build(ctx, autoGenerateDT=True, addRequester=True) + ) @cmds.command(name=_("invite"), description=_("invite-desc"), hybrid=True) async def invite(self, ctx: Context):